From ff900deda6909db0e25e9418506848d3bde02fbe Mon Sep 17 00:00:00 2001
From: Dima Dorfman
Date: Wed, 15 Jun 2005 10:13:04 +0000
Subject: [PATCH] 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)
---
usr.sbin/pwd_mkdb/pwd_mkdb.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.c b/usr.sbin/pwd_mkdb/pwd_mkdb.c
index d164d340c86..3c8ca252a0c 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]*#