2000-05-28 13:40:48 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** Ullrich von Bassewitz, 11.08.1998
|
|
|
|
**
|
|
|
|
** char* fgets (char* s, int size, FILE* f);
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "_file.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-06 18:04:07 +00:00
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2003-11-06 18:04:07 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-10 19:02:54 +00:00
|
|
|
char* __fastcall__ fgets (char* s, unsigned size, register FILE* f)
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2012-06-10 19:02:54 +00:00
|
|
|
register char* p = s;
|
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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Get next character */
|
|
|
|
if ((c = fgetc (f)) == EOF) {
|
|
|
|
/* Error or EOF */
|
|
|
|
if ((f->f_flags & _FERROR) != 0 || i == 0) {
|
|
|
|
/* ERROR or EOF on first char */
|
2012-06-10 19:02:54 +00:00
|
|
|
*p = '\0';
|
2013-05-09 11:56:54 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* EOF with data already read */
|
2002-12-29 20:19:37 +00:00
|
|
|
break;
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* One char more */
|
|
|
|
*p = c;
|
2012-06-10 19:02:54 +00:00
|
|
|
++p;
|
|
|
|
++i;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Stop at end of line */
|
|
|
|
if ((char)c == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2002-12-29 20:19:37 +00:00
|
|
|
/* Terminate the string */
|
2012-06-10 19:02:54 +00:00
|
|
|
*p = '\0';
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|