Discussion:
[PATCH 1/1] teach head -v and -q
(too old to reply)
Ilya Kuzmich
2017-05-28 14:03:26 UTC
Permalink
Raw Message
Not POSIX, but implemented in coreutils and busybox.
Tests use sed to compensate for the stdin naming difference.

Signed-off-by: Ilya Kuzmich <***@gmail.com>
---
tests/head.test | 10 ++++++++++
toys/posix/head.c | 10 +++++++---
2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/tests/head.test b/tests/head.test
index 83d9399..6ed027c 100755
--- a/tests/head.test
+++ b/tests/head.test
@@ -11,9 +11,19 @@ testing "-number" "head -2 input && echo yes" "one\ntwo\nyes\n" \
"one\ntwo\nthree\nfour" ""
testing "head, default lines" "head" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12"

+# coreutils & busybox name stdin as "standard input", toybox uses "-"
+testing "-v file" "head -v -n 1 input" "==> input <==\none\n" "one\ntwo\n" ""
+testing "-v stdin" "head -v -n 1 | sed 's/==> standard input <==/==> - <==/'" \
+ "==> - <==\none\n" "" "one\ntwo\n"
+
+testing "file and stdin" "head -n 1 input - | sed 's/==> standard input <==/==> - <==/'" \
+ "==> input <==\none\n\n==> - <==\nfoo\n" "one\ntwo\n" "foo\nbar\n"
+
echo "foo
bar
baz" > file1
testing "head, multiple files" "head -n 2 input file1" "==> input <==\none\ntwo\n\n==> file1 <==\nfoo\nbar\n" "one\ntwo\nthree\n" ""
+testing "-q, multiple files" "head -q -n 2 input file1" "one\ntwo\nfoo\nbar\n" \
+ "one\ntwo\nthree\n" ""
rm file1

diff --git a/toys/posix/head.c b/toys/posix/head.c
index e3d7886..63eb85b 100644
--- a/toys/posix/head.c
+++ b/toys/posix/head.c
@@ -4,7 +4,7 @@
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/head.html

-USE_HEAD(NEWTOY(head, "?n#<0=10", TOYFLAG_USR|TOYFLAG_BIN))
+USE_HEAD(NEWTOY(head, "?n#<0=10qv", TOYFLAG_USR|TOYFLAG_BIN))

config HEAD
bool "head"
@@ -16,6 +16,8 @@ config HEAD
stdin. Filename "-" is a synonym for stdin.

-n Number of lines to copy
+ -q Never print headers
+ -v Always print headers
*/

#define FOR_head
@@ -30,9 +32,9 @@ static void do_head(int fd, char *name)
{
int i, len, lines=TT.lines, size=sizeof(toybuf);

- if (toys.optc > 1) {
+ if ((toys.optc > 1 && !(toys.optflags & FLAG_q)) || toys.optflags & FLAG_v) {
// Print an extra newline for all but the first file
- if (TT.file_no++) xprintf("\n");
+ if (TT.file_no) xprintf("\n");
xprintf("==> %s <==\n", name);
xflush();
}
@@ -46,6 +48,8 @@ static void do_head(int fd, char *name)

xwrite(1, toybuf, i);
}
+
+ TT.file_no++;
}

void head_main(void)
--
2.7.4
Ilya Kuzmich
2017-05-28 15:16:13 UTC
Permalink
Raw Message
Not POSIX, but implemented in coreutils and busybox.
Tests use sed to compensate for the stdin naming difference.

Signed-off-by: Ilya Kuzmich <***@gmail.com>
---
tests/head.test | 10 ++++++++++
toys/posix/head.c | 6 ++++--
2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/tests/head.test b/tests/head.test
index 83d9399..6ed027c 100755
--- a/tests/head.test
+++ b/tests/head.test
@@ -11,9 +11,19 @@ testing "-number" "head -2 input && echo yes" "one\ntwo\nyes\n" \
"one\ntwo\nthree\nfour" ""
testing "head, default lines" "head" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12"

+# coreutils & busybox name stdin as "standard input", toybox uses "-"
+testing "-v file" "head -v -n 1 input" "==> input <==\none\n" "one\ntwo\n" ""
+testing "-v stdin" "head -v -n 1 | sed 's/==> standard input <==/==> - <==/'" \
+ "==> - <==\none\n" "" "one\ntwo\n"
+
+testing "file and stdin" "head -n 1 input - | sed 's/==> standard input <==/==> - <==/'" \
+ "==> input <==\none\n\n==> - <==\nfoo\n" "one\ntwo\n" "foo\nbar\n"
+
echo "foo
bar
baz" > file1
testing "head, multiple files" "head -n 2 input file1" "==> input <==\none\ntwo\n\n==> file1 <==\nfoo\nbar\n" "one\ntwo\nthree\n" ""
+testing "-q, multiple files" "head -q -n 2 input file1" "one\ntwo\nfoo\nbar\n" \
+ "one\ntwo\nthree\n" ""
rm file1

diff --git a/toys/posix/head.c b/toys/posix/head.c
index e3d7886..f5fd281 100644
--- a/toys/posix/head.c
+++ b/toys/posix/head.c
@@ -4,7 +4,7 @@
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/head.html

-USE_HEAD(NEWTOY(head, "?n#<0=10", TOYFLAG_USR|TOYFLAG_BIN))
+USE_HEAD(NEWTOY(head, "?n#<0=10qv", TOYFLAG_USR|TOYFLAG_BIN))

config HEAD
bool "head"
@@ -16,6 +16,8 @@ config HEAD
stdin. Filename "-" is a synonym for stdin.

-n Number of lines to copy
+ -q Never print headers
+ -v Always print headers
*/

#define FOR_head
@@ -30,7 +32,7 @@ static void do_head(int fd, char *name)
{
int i, len, lines=TT.lines, size=sizeof(toybuf);

- if (toys.optc > 1) {
+ if ((toys.optc > 1 && !(toys.optflags & FLAG_q)) || toys.optflags & FLAG_v) {
// Print an extra newline for all but the first file
if (TT.file_no++) xprintf("\n");
xprintf("==> %s <==\n", name);
--
2.7.4
Rob Landley
2017-05-28 22:25:26 UTC
Permalink
Raw Message
Post by Ilya Kuzmich
Not POSIX, but implemented in coreutils and busybox.
Tests use sed to compensate for the stdin naming difference.
I applied v1, what's different about this?

Rob
Ilya Kuzmich
2017-05-28 23:08:04 UTC
Permalink
Raw Message
V2 is one line shorter.
Post by Rob Landley
Post by Ilya Kuzmich
Not POSIX, but implemented in coreutils and busybox.
Tests use sed to compensate for the stdin naming difference.
I applied v1, what's different about this?
Rob
Rob Landley
2017-05-30 18:55:24 UTC
Permalink
Raw Message
Post by Ilya Kuzmich
V2 is one line shorter.
I already committed and pushed it, could you send me a second patch on
top of the first?

Thanks,

Rob
Ilya Kuzmich
2017-05-30 20:33:48 UTC
Permalink
Raw Message
Sure.
Post by Rob Landley
Post by Ilya Kuzmich
V2 is one line shorter.
I already committed and pushed it, could you send me a second patch on
top of the first?
Thanks,
Rob
Rob Landley
2017-05-28 22:05:05 UTC
Permalink
Raw Message
For future reference, if you do a "git format-patch -1 $COMMIT" and then
attach the file, I can just "git am" it with proper attribution.

Thanks,

Rob
Post by Ilya Kuzmich
Not POSIX, but implemented in coreutils and busybox.
Tests use sed to compensate for the stdin naming difference.
---
tests/head.test | 10 ++++++++++
toys/posix/head.c | 10 +++++++---
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/tests/head.test b/tests/head.test
index 83d9399..6ed027c 100755
--- a/tests/head.test
+++ b/tests/head.test
@@ -11,9 +11,19 @@ testing "-number" "head -2 input && echo yes" "one\ntwo\nyes\n" \
"one\ntwo\nthree\nfour" ""
testing "head, default lines" "head" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12"
+# coreutils & busybox name stdin as "standard input", toybox uses "-"
+testing "-v file" "head -v -n 1 input" "==> input <==\none\n" "one\ntwo\n" ""
+testing "-v stdin" "head -v -n 1 | sed 's/==> standard input <==/==> - <==/'" \
+ "==> - <==\none\n" "" "one\ntwo\n"
+
+testing "file and stdin" "head -n 1 input - | sed 's/==> standard input <==/==> - <==/'" \
+ "==> input <==\none\n\n==> - <==\nfoo\n" "one\ntwo\n" "foo\nbar\n"
+
echo "foo
bar
baz" > file1
testing "head, multiple files" "head -n 2 input file1" "==> input <==\none\ntwo\n\n==> file1 <==\nfoo\nbar\n" "one\ntwo\nthree\n" ""
+testing "-q, multiple files" "head -q -n 2 input file1" "one\ntwo\nfoo\nbar\n" \
+ "one\ntwo\nthree\n" ""
rm file1
diff --git a/toys/posix/head.c b/toys/posix/head.c
index e3d7886..63eb85b 100644
--- a/toys/posix/head.c
+++ b/toys/posix/head.c
@@ -4,7 +4,7 @@
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/head.html
-USE_HEAD(NEWTOY(head, "?n#<0=10", TOYFLAG_USR|TOYFLAG_BIN))
+USE_HEAD(NEWTOY(head, "?n#<0=10qv", TOYFLAG_USR|TOYFLAG_BIN))
config HEAD
bool "head"
@@ -16,6 +16,8 @@ config HEAD
stdin. Filename "-" is a synonym for stdin.
-n Number of lines to copy
+ -q Never print headers
+ -v Always print headers
*/
#define FOR_head
@@ -30,9 +32,9 @@ static void do_head(int fd, char *name)
{
int i, len, lines=TT.lines, size=sizeof(toybuf);
- if (toys.optc > 1) {
+ if ((toys.optc > 1 && !(toys.optflags & FLAG_q)) || toys.optflags & FLAG_v) {
// Print an extra newline for all but the first file
- if (TT.file_no++) xprintf("\n");
+ if (TT.file_no) xprintf("\n");
xprintf("==> %s <==\n", name);
xflush();
}
@@ -46,6 +48,8 @@ static void do_head(int fd, char *name)
xwrite(1, toybuf, i);
}
+
+ TT.file_no++;
}
void head_main(void)
Loading...