Get fgets working and fix fputs

This commit is contained in:
pjht 2020-08-23 17:49:43 -05:00
parent edfb52936d
commit df630c1422
2 changed files with 22 additions and 3 deletions

View File

@ -130,4 +130,8 @@ int main() {
serial_print("Writing to screen\n"); serial_print("Writing to screen\n");
puts("Puts test"); puts("Puts test");
printf("Printf test with file opened to %s\n","/dev/vga"); 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);
} }

View File

@ -82,7 +82,22 @@ char* gets(char* s) {
} }
char* fgets(char* str,int count,FILE* stream) { 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; return str;
} }
@ -119,9 +134,9 @@ int puts(const char *s) {
int fputs(const char* s, FILE* stream) { int fputs(const char* s, FILE* stream) {
size_t retval=fwrite((void*)s,strlen(s),1,stream); size_t retval=fwrite((void*)s,strlen(s),1,stream);
if (retval==0) { if (retval==0) {
return 0;
} else {
return EOF; return EOF;
} else {
return 0;
} }
} }