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 Jan 18, 2025
1 parent 02cdc1c commit cff254d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 53 deletions.
32 changes: 7 additions & 25 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <utime.h>

#include "alloc/malloc.h"
#include "alloc/reallocf.h"
#include "atoi/getnum.h"
#include "commonio.h"
#include "defines.h"
Expand Down Expand Up @@ -573,11 +572,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;
Expand Down Expand Up @@ -639,28 +636,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 Expand Up @@ -722,6 +703,7 @@ int commonio_open (struct commonio_db *db, int mode)
return 0;
}


/*
* Sort given db according to cmp function (usually compares uids)
*/
Expand Down
35 changes: 7 additions & 28 deletions lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,54 +119,33 @@ sgetsgent(const char *string)
return &sgroup;
}


/*
* fgetsgent - convert next line in stream to (struct sgrp)
*
* fgetsgent() reads the next line from the provided stream and
* converts it to a (struct sgrp). NULL is returned on EOF.
*/

/*@observer@*//*@null@*/struct sgrp *fgetsgent (/*@null@*/FILE * fp)
/*@observer@*//*@null@*/struct sgrp *
fgetsgent(/*@null@*/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));
}


/*
* getsgent - get a single shadow group entry
*/
Expand Down

0 comments on commit cff254d

Please sign in to comment.