Skip to content

Commit f04a652

Browse files
lib/: Use getline(3) instead of its pattern
Signed-off-by: Alejandro Colomar <[email protected]>
1 parent e4a2302 commit f04a652

File tree

2 files changed

+14
-53
lines changed

2 files changed

+14
-53
lines changed

lib/commonio.c

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <utime.h>
2525

2626
#include "alloc/malloc.h"
27-
#include "alloc/reallocf.h"
2827
#include "atoi/getnum.h"
2928
#include "commonio.h"
3029
#include "defines.h"
@@ -568,11 +567,9 @@ static void add_one_entry_nis (struct commonio_db *db,
568567
}
569568
#endif /* KEEP_NIS_AT_END */
570569

571-
/* Initial buffer size, as well as increment if not sufficient
572-
(for reading very long lines in group files). */
573-
#define BUFLEN 4096
574570

575-
int commonio_open (struct commonio_db *db, int mode)
571+
int
572+
commonio_open(struct commonio_db *db, int mode)
576573
{
577574
char *buf;
578575
char *line;
@@ -634,28 +631,12 @@ int commonio_open (struct commonio_db *db, int mode)
634631
return 0;
635632
}
636633

637-
buflen = BUFLEN;
638-
buf = MALLOC(buflen, char);
639-
if (NULL == buf)
640-
goto cleanup_errno;
641-
642-
while (fgets(buf, buflen, db->fp) != NULL) {
634+
buf = NULL;
635+
while (getline(&buf, &buflen, db->fp) != -1) {
643636
struct commonio_entry *p;
644637

645-
while ( (strrchr (buf, '\n') == NULL)
646-
&& (feof (db->fp) == 0)) {
647-
size_t len;
648-
649-
buflen += BUFLEN;
650-
buf = REALLOCF(buf, buflen, char);
651-
if (NULL == buf)
652-
goto cleanup_errno;
653-
654-
len = strlen (buf);
655-
if (fgets(buf + len, buflen - len, db->fp) == NULL)
656-
goto cleanup_buf;
657-
}
658-
stpsep(buf, "\n");
638+
if (stpsep(buf, "\n") == NULL)
639+
goto cleanup_buf;
659640

660641
line = strdup (buf);
661642
if (NULL == line) {
@@ -717,6 +698,7 @@ int commonio_open (struct commonio_db *db, int mode)
717698
return 0;
718699
}
719700

701+
720702
/*
721703
* Sort given db according to cmp function (usually compares uids)
722704
*/

lib/gshadow.c

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,54 +93,33 @@ sgetsgent(const char *s)
9393
return &sgroup;
9494
}
9595

96+
9697
/*
9798
* fgetsgent - convert next line in stream to (struct sgrp)
9899
*
99100
* fgetsgent() reads the next line from the provided stream and
100101
* converts it to a (struct sgrp). NULL is returned on EOF.
101102
*/
102103

103-
/*@observer@*//*@null@*/struct sgrp *fgetsgent (/*@null@*/FILE * fp)
104+
/*@observer@*//*@null@*/struct sgrp *
105+
fgetsgent(/*@null@*/FILE *fp)
104106
{
105107
static size_t buflen = 0;
106108
static char *buf = NULL;
107109

108-
char *cp;
109-
110-
if (0 == buflen) {
111-
buf = MALLOC(BUFSIZ, char);
112-
if (NULL == buf) {
113-
return NULL;
114-
}
115-
buflen = BUFSIZ;
116-
}
117-
118110
if (NULL == fp) {
119111
return NULL;
120112
}
121113

122-
if (fgets(buf, buflen, fp) == NULL)
114+
if (getline(&buf, &buflen, fp) == -1)
115+
return NULL;
116+
if (stpsep(buf, "\n") == NULL)
123117
return NULL;
124118

125-
while ( (strrchr(buf, '\n') == NULL)
126-
&& (feof (fp) == 0)) {
127-
size_t len;
128-
129-
cp = REALLOC(buf, buflen * 2, char);
130-
if (NULL == cp) {
131-
return NULL;
132-
}
133-
buf = cp;
134-
buflen *= 2;
135-
136-
len = strlen (buf);
137-
if (fgets(&buf[len], buflen - len, fp) == NULL)
138-
return NULL;
139-
}
140-
stpsep(buf, "\n");
141119
return (sgetsgent (buf));
142120
}
143121

122+
144123
/*
145124
* getsgent - get a single shadow group entry
146125
*/

0 commit comments

Comments
 (0)