Get fgets working and fix fputs
This commit is contained in:
parent
edfb52936d
commit
df630c1422
@ -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);
|
||||
}
|
||||
|
21
libc/stdio.c
21
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user