ack/lang/cem/libcc.ansi/stdio/fgets.c

33 lines
419 B
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* fgets.c - get a string from a file
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-05-30 13:34:25 +00:00
2018-06-21 20:33:47 +00:00
#include <stdio.h>
1989-05-30 13:34:25 +00:00
2018-06-21 20:33:47 +00:00
char* fgets(char* s, register int n, register FILE* stream)
1989-05-30 13:34:25 +00:00
{
register int ch;
2018-06-21 20:33:47 +00:00
register char* ptr;
1989-05-30 13:34:25 +00:00
ptr = s;
2018-06-21 20:33:47 +00:00
while (--n > 0 && (ch = getc(stream)) != EOF)
{
1989-05-30 13:34:25 +00:00
*ptr++ = ch;
2018-06-21 20:33:47 +00:00
if (ch == '\n')
1989-05-30 13:34:25 +00:00
break;
}
2018-06-21 20:33:47 +00:00
if (ch == EOF)
{
if (feof(stream))
{
if (ptr == s)
return NULL;
}
else
return NULL;
1989-12-18 15:04:14 +00:00
}
1989-05-30 13:34:25 +00:00
*ptr = '\0';
1989-06-26 10:37:05 +00:00
return s;
1989-05-30 13:34:25 +00:00
}