Fix some issues and do some code cleanup

This commit is contained in:
Justine Tunney 2022-05-23 10:15:53 -07:00
parent 1f229e4efc
commit 312ed5c67c
72 changed files with 880 additions and 982 deletions

View file

@ -18,13 +18,35 @@
*/
#include "libc/stdio/stdio.h"
/**
* Retrieves line from stream, e.g.
*
* char *line;
* while ((line = _chomp(fgetln(stdin, 0)))) {
* printf("%s\n", line);
* }
*
* The returned memory is owned by the stream. It'll be reused when
* fgetln() is called again. It's free()'d upon fclose() / fflush()
*
* @param stream specifies non-null open input stream
* @param len optionally receives byte length of line
* @return nul-terminated line string, including the `\n` character
* unless a line happened before EOF without `\n`, otherwise it
* returns `NULL` and feof() and ferror() can examine the state
* @see getdelim()
*/
char *fgetln(FILE *stream, size_t *len) {
char *res;
ssize_t rc;
size_t n = 0;
if ((rc = getdelim(&stream->getln, &n, '\n', stream)) > 0) {
*len = rc;
return stream->getln;
flockfile(stream);
if ((rc = getdelim_unlocked(&stream->getln, &n, '\n', stream)) > 0) {
if (len) *len = rc;
res = stream->getln;
} else {
return 0;
res = 0;
}
funlockfile(stream);
return res;
}