aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/cat.c2
-rw-r--r--coreutils/nl.c2
-rw-r--r--libbb/print_numbered_lines.c3
-rwxr-xr-xtestsuite/cat.tests24
-rwxr-xr-xtestsuite/nl.tests39
5 files changed, 67 insertions, 3 deletions
diff --git a/coreutils/cat.c b/coreutils/cat.c
index 65f0648f9..dae6089bd 100644
--- a/coreutils/cat.c
+++ b/coreutils/cat.c
@@ -201,7 +201,7 @@ int cat_main(int argc UNUSED_PARAM, char **argv)
201 ns.start = 1; 201 ns.start = 1;
202 ns.inc = 1; 202 ns.inc = 1;
203 ns.sep = "\t"; 203 ns.sep = "\t";
204 ns.empty_str = "\n"; 204 ns.empty_str = NULL;
205 ns.all = !(opts & CAT_OPT_b); /* -n without -b */ 205 ns.all = !(opts & CAT_OPT_b); /* -n without -b */
206 ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */ 206 ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */
207 exitcode = EXIT_SUCCESS; 207 exitcode = EXIT_SUCCESS;
diff --git a/coreutils/nl.c b/coreutils/nl.c
index 800b73c26..d06673881 100644
--- a/coreutils/nl.c
+++ b/coreutils/nl.c
@@ -68,7 +68,7 @@ int nl_main(int argc UNUSED_PARAM, char **argv)
68 &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b); 68 &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b);
69 ns.all = (opt_b[0] == 'a'); 69 ns.all = (opt_b[0] == 'a');
70 ns.nonempty = (opt_b[0] == 't'); 70 ns.nonempty = (opt_b[0] == 't');
71 ns.empty_str = xasprintf("%*s\n", ns.width + (int)strlen(ns.sep), ""); 71 ns.empty_str = xasprintf("%*s", ns.width + (int)strlen(ns.sep), "");
72 72
73 argv += optind; 73 argv += optind;
74 if (!*argv) 74 if (!*argv)
diff --git a/libbb/print_numbered_lines.c b/libbb/print_numbered_lines.c
index d6459d7c3..4758068a4 100644
--- a/libbb/print_numbered_lines.c
+++ b/libbb/print_numbered_lines.c
@@ -22,10 +22,11 @@ int FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename
22 if (ns->all 22 if (ns->all
23 || (ns->nonempty && line[0]) 23 || (ns->nonempty && line[0])
24 ) { 24 ) {
25 printf("%*u%s%s\n", ns->width, N, ns->sep, line); 25 printf("%*u%s", ns->width, N, ns->sep);
26 N += ns->inc; 26 N += ns->inc;
27 } else if (ns->empty_str) 27 } else if (ns->empty_str)
28 fputs(ns->empty_str, stdout); 28 fputs(ns->empty_str, stdout);
29 puts(line);
29 free(line); 30 free(line);
30 } 31 }
31 ns->start = N; 32 ns->start = N;
diff --git a/testsuite/cat.tests b/testsuite/cat.tests
index 10970dc90..cf924ab5b 100755
--- a/testsuite/cat.tests
+++ b/testsuite/cat.tests
@@ -22,4 +22,28 @@ testing 'cat -v' \
22 'foo\n' 22 'foo\n'
23SKIP= 23SKIP=
24 24
25optional FEATURE_CATN
26testing 'cat -n' \
27 'cat -n' \
28"\
29 1 line 1
30 2
31 3 line 3
32" \
33 '' \
34 'line 1\n\nline 3\n'
35SKIP=
36
37optional FEATURE_CATN
38testing 'cat -b' \
39 'cat -b' \
40"\
41 1 line 1
42
43 2 line 3
44" \
45 '' \
46 'line 1\n\nline 3\n'
47SKIP=
48
25exit $FAILCOUNT 49exit $FAILCOUNT
diff --git a/testsuite/nl.tests b/testsuite/nl.tests
new file mode 100755
index 000000000..95e7abb58
--- /dev/null
+++ b/testsuite/nl.tests
@@ -0,0 +1,39 @@
1#!/bin/sh
2# Copyright 2021 by Ron Yorston
3# Licensed under GPLv2, see file LICENSE in this source tree.
4
5. ./testing.sh
6
7# testing "test name" "commands" "expected result" "file input" "stdin"
8
9testing "nl numbers all lines" \
10 "nl -b a input" \
11"\
12 1 line 1
13 2
14 3 line 3
15" \
16 "line 1\n\nline 3\n" \
17 ""
18
19testing "nl numbers non-empty lines" \
20 "nl -b t input" \
21"\
22 1 line 1
23
24 2 line 3
25" \
26 "line 1\n\nline 3\n" \
27 ""
28
29testing "nl numbers no lines" \
30 "nl -b n input" \
31"\
32 line 1
33
34 line 3
35" \
36 "line 1\n\nline 3\n" \
37 ""
38
39exit $FAILCOUNT