aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-07-19 09:48:04 -0700
committerRob Landley <rob@landley.net>2019-07-22 16:42:07 -0500
commitb30674681b9d72430a029ba7bd3e16a3139244f0 (patch)
treef4b9b66e5c1537ea2a73d606f6b8aa3fe9e102c2 /lib
parent43d398ad5d7b14fb344fc2e5338177761b9a199a (diff)
downloadtoybox-b30674681b9d72430a029ba7bd3e16a3139244f0.tar.gz
Start replacing get_line() with getline().
I started this last night, but thought I'd aim to send multiple small patches rather than work through all the callers and send one big patch. I've deliberately chosen the ugly name `allocated_length` because we've had historical bugs where folks think this a line length in the sense of the return value. I do wonder whether we should actually have some kind of getline() wrapper that hides the `char *`/`size_t` pair in lib/, which makes the function easier to use in most cases but does add the less common gotcha that you wouldn't be able to getline() through multiple files at once (which does happen in at least one toy). But maybe the real fix is to look harder for places where we can just use loopfiles_lines? Speaking of which, should we actually add two more arguments to that? Specifically: switch it to getdelim() rather than getline() behind the scenes, and also add a way to have the trailing '\n' automatically removed, since that seems to be what most callers want? Anyway, that seemed like enough questions that it was time to send this initial patch out before doing too much more...
Diffstat (limited to 'lib')
-rw-r--r--lib/password.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/password.c b/lib/password.c
index b9cc1346..432905cc 100644
--- a/lib/password.c
+++ b/lib/password.c
@@ -110,8 +110,9 @@ int update_password(char *filename, char* username, char* entry)
char *filenamesfx = NULL, *namesfx = NULL, *shadow = NULL,
*sfx = NULL, *line = NULL;
FILE *exfp, *newfp;
- int ret = -1, found = 0;
+ int ret = -1, found = 0, n;
struct flock lock;
+ size_t allocated_length;
shadow = strstr(filename, "shadow");
filenamesfx = xmprintf("%s+", filename);
@@ -149,8 +150,8 @@ int update_password(char *filename, char* username, char* entry)
ret = 0;
namesfx = xmprintf("%s:",username);
- while ((line = get_line(fileno(exfp))) != NULL)
- {
+ while ((n = getline(&line, &allocated_length, exfp)) > 0) {
+ line[n-1] = 0;
if (strncmp(line, namesfx, strlen(namesfx)))
fprintf(newfp, "%s\n", line);
else if (entry) {
@@ -175,8 +176,8 @@ int update_password(char *filename, char* username, char* entry)
fprintf(newfp, "%s\n", entry);
}
}
- free(line);
}
+ free(line);
free(namesfx);
if (!found && entry) fprintf(newfp, "%s\n", entry);
fcntl(fileno(exfp), F_SETLK, &lock);