diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-02-08 18:42:39 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-02-08 18:42:39 -0500 |
commit | 7ef8a7379f7f7d09e71ccae2a0b688c3cd80423f (patch) | |
tree | 092ab0aed1769117fd7b28b8592f6f96b0e0d5af /src/stdio/fgets.c | |
parent | 6acf19370e8adff79cd83b257d3f04aeaf2a59dd (diff) |
merge sources into single tree
Diffstat (limited to 'src/stdio/fgets.c')
-rw-r--r-- | src/stdio/fgets.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c new file mode 100644 index 00000000..df110de2 --- /dev/null +++ b/src/stdio/fgets.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include "nonstd/io.h" + +/** read a string of characters from a file stream **/ +char * fgets(char * restrict s, int n, FILE * restrict stream) +{ + flockfile(stream); + + int i = 0; + if (feof(stream)) { + return NULL; + } + + while (i < n-1) { + s[i] = fgetc(stream); + if (s[i] == '\n') { + s[i+1] = '\0'; + i = n; + } else if (s[i] == EOF && feof(stream)) { + s[i] = '\0'; + i = n; + } + i++; + } + + funlockfile(stream); + /* + RETURN_SUCCESS(ARGUMENT(s)); + RETURN_FAILURE(CONSTANT(NULL)); + */ + return s; +} + +/*** +reads a string of characters from ARGUMENT(stream). Up to +ARGUMENT(n)-1 characters will be read into the array at ARGUMENT(s). + +If a newline is read, it will be appended to ARGUMENT(s) and THIS() will return. + +If end-of-file is reached, THIS() will return. + +A CHAR(\0) will be written immediately after the final character is read. + +If end-of-file is reached before any characters are read, the contents of +ARGUMENT(s) will be unchanged. + +If an error occurs, the contents of ARGUMENT(s) are indeterminite. +***/ + +/* +STDC(1) +*/ |