mirror of
https://github.com/cc65/cc65.git
synced 2024-11-11 01:05:25 +00:00
63b629b801
git-svn-id: svn://svn.cc65.org/cc65/trunk@4690 b7a2c559-68d2-44c3-8de9-860c34a00d81
66 lines
1.2 KiB
C
66 lines
1.2 KiB
C
/*
|
|
* Ullrich von Bassewitz, 11.08.1998
|
|
*
|
|
* char* fgets (char* s, int size, FILE* f);
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include "_file.h"
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
/* Code */
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
char* __fastcall__ fgets (char* s, unsigned size, FILE* f)
|
|
{
|
|
unsigned i;
|
|
int c;
|
|
|
|
if (size == 0) {
|
|
/* Invalid size */
|
|
return (char*) _seterrno (EINVAL);
|
|
}
|
|
|
|
/* Read input */
|
|
i = 0;
|
|
while (--size) {
|
|
|
|
/* Get next character */
|
|
if ((c = fgetc (f)) == EOF) {
|
|
s[i] = '\0';
|
|
/* Error or EOF */
|
|
if ((f->f_flags & _FERROR) != 0 || i == 0) {
|
|
/* ERROR or EOF on first char */
|
|
return 0;
|
|
} else {
|
|
/* EOF with data already read */
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* One char more */
|
|
s[i++] = c;
|
|
|
|
/* Stop at end of line */
|
|
if (c == '\n') {
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Terminate the string */
|
|
s[i] = '\0';
|
|
|
|
/* Done */
|
|
return s;
|
|
}
|
|
|
|
|
|
|