Skip to content

Commit

Permalink
lib/: Use getline(3) instead of its pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jul 22, 2024
1 parent cd64d53 commit e8036d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 47 deletions.
24 changes: 4 additions & 20 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,28 +638,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) {
Expand Down
30 changes: 3 additions & 27 deletions lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,39 +141,15 @@ void endsgent (void)
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));
}

Expand Down

0 comments on commit e8036d0

Please sign in to comment.