From 7fd1894ecc4ca6d85cc2bfae33c9c9d14be21920 Mon Sep 17 00:00:00 2001 From: Sagar Ladla Date: Tue, 30 Apr 2024 20:06:48 +0530 Subject: [PATCH] Add support for scanf to accept formatted modified: src/lib/libc/scanf.c bug fix for https://github.com/VisorFolks/cyancore/pull/289#issue-2270495556 - removed usage of macros https://github.com/VisorFolks/cyancore/pull/289#discussion_r1584138333 - removed buffer overflow condition https://github.com/VisorFolks/cyancore/pull/289#discussion_r1584139514 - variable decalaration in new line https://github.com/VisorFolks/cyancore/pull/289#discussion_r1584138879 Issue: #289 --- src/lib/libc/scanf.c | 70 ++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/lib/libc/scanf.c b/src/lib/libc/scanf.c index f8aa0a34..b5b7591c 100644 --- a/src/lib/libc/scanf.c +++ b/src/lib/libc/scanf.c @@ -52,10 +52,10 @@ char getchar() * @brief This function will take string input with spaces * */ -char *gets() +char *gets(char *str) { - char *s = (char *)malloc(sizeof(char)), temp; - int i = 0; + char *p = str; + char temp; do { temp = getch(); @@ -66,10 +66,11 @@ char *gets() } else { - s[i++] = temp; + *str++ = temp; } } while(temp != '\r'); - return s; + str = '\0'; + return p; } /** @@ -80,8 +81,6 @@ char *gets() * */ -#define CURRCHAR (temp) -#define CHARIN() (temp = getch()) int scanf(const char * restrict fmt, ...) { unsigned int count = 0; @@ -105,19 +104,19 @@ int scanf(const char * restrict fmt, ...) case 'c': { /* skip leading spaces */ - while (isSpace(CHARIN())) + while (isSpace(temp = getch())) { - if (CURRCHAR == '\r') + if (temp == '\r') fputc(stdout, '\n'); else - fputc(stdout, CURRCHAR); + fputc(stdout, temp); } - fputc(stdout, CURRCHAR); - c = CURRCHAR; - while (CHARIN()) + fputc(stdout, temp); + c = temp; + while ((temp = getch())) { - fputc(stdout, CURRCHAR); - if (isSpace(CURRCHAR)) + fputc(stdout, temp); + if (isSpace(temp)) break; } fputc(stdout, '\n'); @@ -129,21 +128,21 @@ int scanf(const char * restrict fmt, ...) { char *s = va_arg(ap, char *); /* skip leading spaces */ - while (isSpace(CHARIN())) + while (isSpace(temp = getch())) { - if (CURRCHAR == '\r') + if (temp == '\r') fputc(stdout, '\n'); else - fputc(stdout, CURRCHAR); + fputc(stdout, temp); } - fputc(stdout, CURRCHAR); - *s++ = CURRCHAR; - while (CHARIN()) + fputc(stdout, temp); + *s++ = temp; + while ((temp = getch())) { - fputc(stdout, CURRCHAR); - if (isSpace(CURRCHAR)) + fputc(stdout, temp); + if (isSpace(temp)) break; - *s++ = CURRCHAR; + *s++ = temp; } fputc(stdout, '\n'); *s = '\0'; @@ -155,26 +154,26 @@ int scanf(const char * restrict fmt, ...) { int a = 0; /* skip leading spaces */ - while (isSpace(CHARIN())) + while (isSpace(temp = getch())) { - if (CURRCHAR == '\r') + if (temp == '\r') fputc(stdout, '\n'); else - fputc(stdout, CURRCHAR); + fputc(stdout, temp); } - fputc(stdout, CURRCHAR); - c = CURRCHAR; - while (CHARIN()) + fputc(stdout, temp); + c = temp; + while ((temp = getch())) { a *= 10; if (isDigit(c)) a += (c - '0'); else a += (int)c; - if (isSpace(CURRCHAR)) + if (isSpace(temp)) break; - c = CURRCHAR; - fputc(stdout, CURRCHAR); + c = temp; + fputc(stdout, temp); } fputc(stdout, '\n'); *(int *)va_arg(ap, int *) = a; @@ -183,8 +182,9 @@ int scanf(const char * restrict fmt, ...) } case 'f': { - char *a = gets(); - *(float *)va_arg(ap, float *) = (float)atof(a); + char buff[50]; + gets(buff); + *(float *)va_arg(ap, float *) = (float)atof(buff); count++; break; }