2000-05-28 13:40:48 +00:00
|
|
|
/*
|
|
|
|
* Ullrich von Bassewitz, 11.08.1998
|
|
|
|
*
|
|
|
|
* char* fgets (char* s, int size, FILE* f);
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "_file.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* fgets (char* s, unsigned size, FILE* f)
|
|
|
|
{
|
2002-12-29 20:19:37 +00:00
|
|
|
int i = 0;
|
|
|
|
int c;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-12-29 20:19:37 +00:00
|
|
|
if (size == 0) {
|
|
|
|
/* Invalid size */
|
|
|
|
_errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read input */
|
|
|
|
i = 0;
|
|
|
|
while (--size) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get next character */
|
2002-12-29 20:19:37 +00:00
|
|
|
if ((c = fgetc (f)) == EOF) {
|
|
|
|
s[i] = '\0';
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Error or EOF */
|
2002-12-29 20:19:37 +00:00
|
|
|
if ((f->f_flags & _FERROR) != 0 || i == 0) {
|
|
|
|
/* ERROR or EOF on first char */
|
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2002-12-29 20:19:37 +00:00
|
|
|
/* EOF with data already read */
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One char more */
|
2002-12-29 20:19:37 +00:00
|
|
|
s[i++] = c;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-12-29 20:19:37 +00:00
|
|
|
/* Stop at end of line */
|
|
|
|
if (c == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2002-12-29 20:19:37 +00:00
|
|
|
/* Terminate the string */
|
|
|
|
s[i] = '\0';
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|