diff options
author | dd <dd@FreeBSD.org> | 2005-06-15 10:13:04 +0000 |
---|---|---|
committer | dd <dd@FreeBSD.org> | 2005-06-15 10:13:04 +0000 |
commit | f531cb9c12903b71c9be2328df21ae307192a0a6 (patch) | |
tree | 853f0d282f9e7f08f32f4b2b98319676deefb039 /usr.sbin/pwd_mkdb | |
parent | d5a287bf04a18d7f33d2f2dd80777ad484277fe3 (diff) | |
download | FreeBSD-src-f531cb9c12903b71c9be2328df21ae307192a0a6.zip FreeBSD-src-f531cb9c12903b71c9be2328df21ae307192a0a6.tar.gz |
Correctly handle an input file without a newline on the last line (and
avoid the confusing error message about the line being too long). This
change uses fgetln to detect the right conditions, but the fixed-width
line buffer is kept because too many other places in the program make
assumptions about its maximum width.
Approved by: re (scottl)
Diffstat (limited to 'usr.sbin/pwd_mkdb')
-rw-r--r-- | usr.sbin/pwd_mkdb/pwd_mkdb.c | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.c b/usr.sbin/pwd_mkdb/pwd_mkdb.c index d164d34..3c8ca25 100644 --- a/usr.sbin/pwd_mkdb/pwd_mkdb.c +++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c @@ -631,12 +631,14 @@ main(int argc, char *argv[]) } int -scan(FILE * fp, struct passwd *pw) +scan(FILE *fp, struct passwd *pw) { static int lcnt; + size_t len; char *p; - if (!fgets(line, sizeof(line), fp)) + p = fgetln(fp, &len); + if (p == NULL) return (0); ++lcnt; /* @@ -644,16 +646,14 @@ scan(FILE * fp, struct passwd *pw) * throat...'' * -- The Who */ - if (!(p = strchr(line, '\n'))) { - /* - * XXX: This may also happen if the last line in a - * file does not have a trailing newline. - */ + if (len > 0 && p[len - 1] == '\n') + len--; + if (len >= sizeof(line) - 1) { warnx("line #%d too long", lcnt); goto fmt; - } - *p = '\0'; + memcpy(line, p, len); + line[len] = '\0'; /* * Ignore comments: ^[ \t]*# |