From: Alejandro Colomar Date: Mon, 22 Jul 2024 11:46:32 +0000 (+0200) Subject: lib/: Use getline(3) instead of its pattern X-Git-Tag: 4.19.0-rc1~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6178b722f61861fb0a622ca36b7c7ad83b8d100;p=thirdparty%2Fshadow.git lib/: Use getline(3) instead of its pattern Signed-off-by: Alejandro Colomar --- diff --git a/lib/commonio.c b/lib/commonio.c index cd18560f6..3115acd21 100644 --- a/lib/commonio.c +++ b/lib/commonio.c @@ -24,7 +24,6 @@ #include #include "alloc/malloc.h" -#include "alloc/reallocf.h" #include "atoi/getnum.h" #include "commonio.h" #include "defines.h" @@ -568,11 +567,9 @@ static void add_one_entry_nis (struct commonio_db *db, } #endif /* KEEP_NIS_AT_END */ -/* Initial buffer size, as well as increment if not sufficient - (for reading very long lines in group files). */ -#define BUFLEN 4096 -int commonio_open (struct commonio_db *db, int mode) +int +commonio_open(struct commonio_db *db, int mode) { char *buf; char *line; @@ -634,28 +631,12 @@ int commonio_open (struct commonio_db *db, int mode) return 0; } - buflen = BUFLEN; - buf = MALLOC(buflen, char); - if (NULL == buf) - goto cleanup_errno; - - while (fgets(buf, buflen, db->fp) != NULL) { + buf = NULL; + while (getline(&buf, &buflen, db->fp) != -1) { struct commonio_entry *p; - while ( (strrchr (buf, '\n') == NULL) - && (feof (db->fp) == 0)) { - size_t len; - - buflen += BUFLEN; - buf = REALLOCF(buf, buflen, char); - if (NULL == buf) - goto cleanup_errno; - - len = strlen (buf); - if (fgets(buf + len, buflen - len, db->fp) == NULL) - goto cleanup_buf; - } - stpsep(buf, "\n"); + if (stpsep(buf, "\n") == NULL) + goto cleanup_buf; line = strdup (buf); if (NULL == line) { @@ -716,6 +697,7 @@ int commonio_open (struct commonio_db *db, int mode) return 0; } + /* * Sort given db according to cmp function (usually compares uids) */ diff --git a/lib/shadow/gshadow/fgetsgent.c b/lib/shadow/gshadow/fgetsgent.c index 91cc52000..56345d5ea 100644 --- a/lib/shadow/gshadow/fgetsgent.c +++ b/lib/shadow/gshadow/fgetsgent.c @@ -37,39 +37,15 @@ fgetsgent(FILE *fp) static size_t buflen = 0; static char *buf = NULL; - char *cp; - - if (0 == buflen) { - buf = MALLOC(BUFSIZ, char); - if (NULL == buf) { - return NULL; - } - buflen = BUFSIZ; - } - if (NULL == fp) { return NULL; } - if (fgets(buf, buflen, fp) == NULL) + if (getline(&buf, &buflen, fp) == -1) + return NULL; + if (stpsep(buf, "\n") == NULL) return NULL; - while ( (strrchr(buf, '\n') == NULL) - && (feof (fp) == 0)) { - size_t len; - - cp = REALLOC(buf, buflen * 2, char); - if (NULL == cp) { - return NULL; - } - buf = cp; - buflen *= 2; - - len = strlen (buf); - if (fgets(&buf[len], buflen - len, fp) == NULL) - return NULL; - } - stpsep(buf, "\n"); return sgetsgent(buf); } #endif