summaryrefslogtreecommitdiffstats
path: root/usr.bin/nl/nl.c
diff options
context:
space:
mode:
authordas <das@FreeBSD.org>2009-02-28 15:53:36 +0000
committerdas <das@FreeBSD.org>2009-02-28 15:53:36 +0000
commit906af9736aefe86883b3e7ea6d666fc14cdbf364 (patch)
treea0967035bef508970c9b56ca0654a186592fdc6b /usr.bin/nl/nl.c
parent51d425ac0e9404969edd449f9538d8d5b5d6541b (diff)
downloadFreeBSD-src-906af9736aefe86883b3e7ea6d666fc14cdbf364.zip
FreeBSD-src-906af9736aefe86883b3e7ea6d666fc14cdbf364.tar.gz
Use getline() instead of fgets(). This enables nl(1) to handle
arbitrarily long lines and embedded NULs. The new functionality is nugatory, but adding it is a simple way to improve the exposure of getline() in -CURRENT.
Diffstat (limited to 'usr.bin/nl/nl.c')
-rw-r--r--usr.bin/nl/nl.c47
1 files changed, 21 insertions, 26 deletions
diff --git a/usr.bin/nl/nl.c b/usr.bin/nl/nl.c
index afc3c860..172eb6c 100644
--- a/usr.bin/nl/nl.c
+++ b/usr.bin/nl/nl.c
@@ -42,6 +42,7 @@ __COPYRIGHT(
__RCSID("$FreeBSD$");
#endif
+#define _WITH_GETLINE
#include <sys/types.h>
#include <err.h>
@@ -98,12 +99,6 @@ static void parse_numbering(const char *, int);
static void usage(void);
/*
- * Pointer to dynamically allocated input line buffer, and its size.
- */
-static char *buffer;
-static size_t buffersize;
-
-/*
* Dynamically allocated buffer suitable for string representation of ints.
*/
static char *intbuffer;
@@ -269,14 +264,6 @@ main(argc, argv)
memcpy(delim + delim1len, delim2, delim2len);
delimlen = delim1len + delim2len;
- /* Determine the maximum input line length to operate on. */
- if ((val = sysconf(_SC_LINE_MAX)) == -1) /* ignore errno */
- val = LINE_MAX;
- /* Allocate sufficient buffer space (including the terminating NUL). */
- buffersize = (size_t)val + 1;
- if ((buffer = malloc(buffersize)) == NULL)
- err(EXIT_FAILURE, "cannot allocate input line buffer");
-
/* Allocate a buffer suitable for preformatting line number. */
intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
if ((intbuffer = malloc(intbuffersize)) == NULL)
@@ -292,6 +279,9 @@ main(argc, argv)
static void
filter()
{
+ char *buffer;
+ size_t buffersize;
+ ssize_t linelen;
int line; /* logical line number */
int section; /* logical page section */
unsigned int adjblank; /* adjacent blank lines */
@@ -302,21 +292,23 @@ filter()
line = startnum;
section = BODY;
- while (fgets(buffer, (int)buffersize, stdin) != NULL) {
+ buffer = NULL;
+ buffersize = 0;
+ while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
for (idx = FOOTER; idx <= NP_LAST; idx++) {
/* Does it look like a delimiter? */
+ if (delimlen * (idx + 1) > linelen)
+ break;
if (memcmp(buffer + delimlen * idx, delim,
- delimlen) == 0) {
- /* Was this the whole line? */
- if (buffer[delimlen * (idx + 1)] == '\n') {
- section = idx;
- adjblank = 0;
- if (restart)
- line = startnum;
- goto nextline;
- }
- } else {
+ delimlen) != 0)
break;
+ /* Was this the whole line? */
+ if (buffer[delimlen * (idx + 1)] == '\n') {
+ section = idx;
+ adjblank = 0;
+ if (restart)
+ line = startnum;
+ goto nextline;
}
}
@@ -354,7 +346,8 @@ filter()
} else {
(void)printf("%*s", width, "");
}
- (void)printf("%s%s", sep, buffer);
+ (void)fputs(sep, stdout);
+ (void)fwrite(buffer, linelen, 1, stdout);
if (ferror(stdout))
err(EXIT_FAILURE, "output error");
@@ -364,6 +357,8 @@ nextline:
if (ferror(stdin))
err(EXIT_FAILURE, "input error");
+
+ free(buffer);
}
/*
OpenPOWER on IntegriCloud