Added fgets(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2012-03-06 13:15:02 +01:00
parent 6bcb3d7384
commit 065ceae509
2 changed files with 21 additions and 1 deletions

View File

@ -225,3 +225,23 @@ int fputs(const char* str, FILE* fp)
return result;
}
char* fgets(char* dest, int size, FILE* fp)
{
if ( size <= 0 ) { errno = EINVAL; return NULL; }
int i;
for ( i = 0; i < size-1; i++ )
{
int c = getc(fp);
if ( c == EOF )
{
if ( ferror(fp) ) { return NULL; }
else { i++; break; } /* EOF */
}
dest[i] = c;
if ( c == '\n' ) { i++; break; }
}
dest[i] = '\0';
return dest;
}

View File

@ -78,6 +78,7 @@ extern int ferror(FILE* stream);
extern int fflush(FILE* stream);
extern int fileno(FILE* stream);
extern int fgetc(FILE* stream);
extern char* fgets(char* restrict s, int n, FILE* restrict stream);
extern FILE* fopen(const char* restrict filename, const char* restrict mode);
extern int fprintf(FILE* restrict stream, const char* restrict format, ...);
extern int fputc(int c, FILE* stream);
@ -111,7 +112,6 @@ extern int vsprintf(char* restrict s, const char* restrict format, va_list ap);
/* TODO: These are not implemented in libmaxsi/sortix yet. */
#if defined(__SORTIX_SHOW_UNIMPLEMENTED)
extern char* ctermid(char* s);
extern char* fgets(char* restrict s, int n, FILE* restrict stream);
extern FILE *fmemopen(void* restrict buf, size_t size, const char* restrict mode);
extern FILE* freopen(const char* restrict filename, const char *restrict mode, FILE* restrict stream);
extern FILE* open_memstream(char** bufp, size_t* sizep);