Skip to content

Commit 7e3d5d5

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

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"
@@ -571,11 +570,9 @@ static void add_one_entry_nis (struct commonio_db *db,
571570
}
572571
#endif /* KEEP_NIS_AT_END */
573572

574-
/* Initial buffer size, as well as increment if not sufficient
575-
(for reading very long lines in group files). */
576-
#define BUFLEN 4096
577573

578-
int commonio_open (struct commonio_db *db, int mode)
574+
int
575+
commonio_open(struct commonio_db *db, int mode)
579576
{
580577
char *buf;
581578
char *line;
@@ -637,28 +634,12 @@ int commonio_open (struct commonio_db *db, int mode)
637634
return 0;
638635
}
639636

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

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

663644
line = strdup (buf);
664645
if (NULL == line) {
@@ -720,6 +701,7 @@ int commonio_open (struct commonio_db *db, int mode)
720701
return 0;
721702
}
722703

704+
723705
/*
724706
* Sort given db according to cmp function (usually compares uids)
725707
*/

lib/gshadow.c

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,54 +129,33 @@ void endsgent (void)
129129
return &sgroup;
130130
}
131131

132+
132133
/*
133134
* fgetsgent - convert next line in stream to (struct sgrp)
134135
*
135136
* fgetsgent() reads the next line from the provided stream and
136137
* converts it to a (struct sgrp). NULL is returned on EOF.
137138
*/
138139

139-
/*@observer@*//*@null@*/struct sgrp *fgetsgent (/*@null@*/FILE * fp)
140+
/*@observer@*//*@null@*/struct sgrp *
141+
fgetsgent(/*@null@*/FILE *fp)
140142
{
141143
static size_t buflen = 0;
142144
static char *buf = NULL;
143145

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-
154146
if (NULL == fp) {
155147
return NULL;
156148
}
157149

158-
if (fgets(buf, buflen, fp) == NULL)
150+
if (getline(&buf, &buflen, fp) == -1)
151+
return NULL;
152+
if (stpsep(buf, "\n") == NULL)
159153
return NULL;
160154

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");
177155
return (sgetsgent (buf));
178156
}
179157

158+
180159
/*
181160
* getsgent - get a single shadow group entry
182161
*/

0 commit comments

Comments
 (0)