33 lines
507 B
C
33 lines
507 B
C
|
|
#include <stdio.h>
|
||
|
|
|
||
|
|
char *fgets(char *restrict str, int count, FILE *restrict fp)
|
||
|
|
{
|
||
|
|
if (count < 1) {
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (count == 1) {
|
||
|
|
str[0] = '\0';
|
||
|
|
return str;
|
||
|
|
}
|
||
|
|
|
||
|
|
int r = 0;
|
||
|
|
|
||
|
|
while (1) {
|
||
|
|
int c = fgetc(fp);
|
||
|
|
if (c == EOF) {
|
||
|
|
str[r] = '\0';
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
str[r++] = c;
|
||
|
|
|
||
|
|
if (r == count - 1 || c == '\n') {
|
||
|
|
str[r] = '\0';
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return r > 0 ? str : NULL;
|
||
|
|
}
|