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"
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-06 18:04:07 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* Code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* __fastcall__ fgets (char* s, unsigned size, FILE* f)
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2004-05-13 21:17:58 +00:00
|
|
|
unsigned i;
|
2002-12-29 20:19:37 +00:00
|
|
|
int c;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-12-29 20:19:37 +00:00
|
|
|
if (size == 0) {
|
|
|
|
/* Invalid size */
|
2010-06-03 20:21:23 +00:00
|
|
|
return (char*) _seterrno (EINVAL);
|
2002-12-29 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|