diff --git a/init/main.c b/init/main.c index 6a4de21..44a3d52 100644 --- a/init/main.c +++ b/init/main.c @@ -130,4 +130,8 @@ int main() { serial_print("Writing to screen\n"); puts("Puts test"); printf("Printf test with file opened to %s\n","/dev/vga"); + FILE* file=fopen("/initrd/hi","r"); + char str[64]; + fgets(str,64,file); + printf("Read %s from /initrd/hi\n", str); } diff --git a/libc/stdio.c b/libc/stdio.c index a2b2fb5..2d3e4b3 100644 --- a/libc/stdio.c +++ b/libc/stdio.c @@ -82,7 +82,22 @@ char* gets(char* s) { } char* fgets(char* str,int count,FILE* stream) { - fread(str,1,count,stream); + count=fread(str,1,count-1,stream)+1; + if (count==0) { + return NULL; + } + str[count]='\0'; + int newlinepos=-1; + for (int i=0;i<(count-1);i++) { + if (str[i]=='\n') { + newlinepos=i; + break; + } + } + if (newlinepos) { + stream->pos-=(count-1); + stream->pos+=newlinepos; + } return str; } @@ -119,9 +134,9 @@ int puts(const char *s) { int fputs(const char* s, FILE* stream) { size_t retval=fwrite((void*)s,strlen(s),1,stream); if (retval==0) { - return 0; - } else { return EOF; + } else { + return 0; } }