Skip to content

Commit e8036d0

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

File tree

2 files changed

+7
-47
lines changed

2 files changed

+7
-47
lines changed

lib/commonio.c

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -638,28 +638,12 @@ int commonio_open (struct commonio_db *db, int mode)
638638
return 0;
639639
}
640640

641-
buflen = BUFLEN;
642-
buf = MALLOC(buflen, char);
643-
if (NULL == buf)
644-
goto cleanup_errno;
645-
646-
while (fgets(buf, buflen, db->fp) != NULL) {
641+
buf = NULL;
642+
while (getline(&buf, &buflen, db->fp) != -1) {
647643
struct commonio_entry *p;
648644

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

664648
line = strdup (buf);
665649
if (NULL == line) {

lib/gshadow.c

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,39 +141,15 @@ void endsgent (void)
141141
static size_t buflen = 0;
142142
static char *buf = NULL;
143143

144-
char *cp;
145-
146-
if (0 == buflen) {
147-
buf = MALLOC(BUFSIZ, char);
148-
if (NULL == buf) {
149-
return NULL;
150-
}
151-
buflen = BUFSIZ;
152-
}
153-
154144
if (NULL == fp) {
155145
return NULL;
156146
}
157147

158-
if (fgets(buf, buflen, fp) == NULL)
148+
if (getline(&buf, &buflen, fp) == -1)
149+
return NULL;
150+
if (stpsep(buf, "\n") == NULL)
159151
return NULL;
160152

161-
while ( (strrchr(buf, '\n') == NULL)
162-
&& (feof (fp) == 0)) {
163-
size_t len;
164-
165-
cp = REALLOC(buf, buflen * 2, char);
166-
if (NULL == cp) {
167-
return NULL;
168-
}
169-
buf = cp;
170-
buflen *= 2;
171-
172-
len = strlen (buf);
173-
if (fgets(&buf[len], buflen - len, fp) == NULL)
174-
return NULL;
175-
}
176-
stpsep(buf, "\n");
177153
return (sgetsgent (buf));
178154
}
179155

0 commit comments

Comments
 (0)