diff options
-rw-r--r-- | AUTHORS | 3 | ||||
-rw-r--r-- | coreutils/paste.c | 138 | ||||
-rw-r--r-- | docs/posix_conformance.txt | 8 | ||||
-rw-r--r-- | testsuite/paste/paste | 20 | ||||
-rw-r--r-- | testsuite/paste/paste-back-cuted-lines | 9 | ||||
-rw-r--r-- | testsuite/paste/paste-multi-stdin | 16 | ||||
-rw-r--r-- | testsuite/paste/paste-pairs | 16 | ||||
-rw-r--r-- | testsuite/paste/paste-separate | 19 |
8 files changed, 228 insertions, 1 deletions
@@ -178,3 +178,6 @@ Mike Frysinger <vapier@gentoo.org> | |||
178 | 178 | ||
179 | Jie Zhang <jie.zhang@analog.com> | 179 | Jie Zhang <jie.zhang@analog.com> |
180 | fixed two bugs in msh and hush (exitcode of killed processes) | 180 | fixed two bugs in msh and hush (exitcode of killed processes) |
181 | |||
182 | Maxime Coste <mawww@kakoune.org> | ||
183 | paste implementation | ||
diff --git a/coreutils/paste.c b/coreutils/paste.c new file mode 100644 index 000000000..4eab13839 --- /dev/null +++ b/coreutils/paste.c | |||
@@ -0,0 +1,138 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * paste.c - implementation of the posix paste command | ||
4 | * | ||
5 | * Written by Maxime Coste <mawww@kakoune.org> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
8 | */ | ||
9 | //config:config PASTE | ||
10 | //config: bool "paste" | ||
11 | //config: default y | ||
12 | //config: help | ||
13 | //config: paste is used to paste lines of different files together | ||
14 | //config: and write the result to stdout | ||
15 | |||
16 | //applet:IF_PASTE(APPLET_NOEXEC(paste, paste, BB_DIR_USR_BIN, BB_SUID_DROP, paste)) | ||
17 | |||
18 | //kbuild:lib-$(CONFIG_PASTE) += paste.o | ||
19 | |||
20 | //usage:#define paste_trivial_usage | ||
21 | //usage: "[OPTIONS] [FILE]..." | ||
22 | //usage:#define paste_full_usage "\n\n" | ||
23 | //usage: "Paste lines from each input file, seperated with tab\n" | ||
24 | //usage: "\n -d LIST Use delimiters from LIST, not tab" | ||
25 | //usage: "\n -s Serial: one file at a time" | ||
26 | //usage: | ||
27 | //usage:#define paste_example_usage | ||
28 | //usage: "# write out directory in four columns\n" | ||
29 | //usage: "$ ls | paste - - - -\n" | ||
30 | //usage: "# combine pairs of lines from a file into single lines\n" | ||
31 | //usage: "$ paste -s -d '\\t\\n' file\n" | ||
32 | |||
33 | #include "libbb.h" | ||
34 | |||
35 | static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt) | ||
36 | { | ||
37 | char *line; | ||
38 | char delim; | ||
39 | int del_idx = 0; | ||
40 | int active_files = file_cnt; | ||
41 | int i; | ||
42 | |||
43 | while (active_files > 0) { | ||
44 | for (i = 0; i < file_cnt; ++i) { | ||
45 | if (files[i] == NULL) | ||
46 | continue; | ||
47 | |||
48 | line = xmalloc_fgetline(files[i]); | ||
49 | if (!line) { | ||
50 | fclose_if_not_stdin(files[i]); | ||
51 | files[i] = NULL; | ||
52 | --active_files; | ||
53 | continue; | ||
54 | } | ||
55 | fputs(line, stdout); | ||
56 | free(line); | ||
57 | delim = '\n'; | ||
58 | if (i != file_cnt - 1) { | ||
59 | delim = delims[del_idx++]; | ||
60 | if (del_idx == del_cnt) | ||
61 | del_idx = 0; | ||
62 | } | ||
63 | if (delim != '\0') | ||
64 | fputc(delim, stdout); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | static void paste_files_separate(FILE** files, char* delims, int del_cnt) | ||
70 | { | ||
71 | char *line, *next_line; | ||
72 | char delim; | ||
73 | int del_idx = 0; | ||
74 | int i; | ||
75 | |||
76 | for (i = 0; files[i]; ++i) { | ||
77 | line = NULL; | ||
78 | while ((next_line = xmalloc_fgetline(files[i])) != NULL) { | ||
79 | if (line) { | ||
80 | fputs(line, stdout); | ||
81 | free(line); | ||
82 | delim = delims[del_idx++]; | ||
83 | if (del_idx == del_cnt) | ||
84 | del_idx = 0; | ||
85 | if (delim != '\0') | ||
86 | fputc(delim, stdout); | ||
87 | } | ||
88 | line = next_line; | ||
89 | } | ||
90 | if (line) { | ||
91 | /* coreutils adds \n even if this is a final line | ||
92 | * of the last file and it was not \n-terminated. | ||
93 | */ | ||
94 | printf("%s\n", line); | ||
95 | free(line); | ||
96 | } | ||
97 | fclose_if_not_stdin(files[i]); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | #define PASTE_OPT_DELIMITERS (1 << 0) | ||
102 | #define PASTE_OPT_SEPARATE (1 << 1) | ||
103 | |||
104 | int paste_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
105 | int paste_main(int argc UNUSED_PARAM, char **argv) | ||
106 | { | ||
107 | char *delims = (char*)"\t"; | ||
108 | int del_cnt = 1; | ||
109 | unsigned opt; | ||
110 | int i; | ||
111 | |||
112 | opt = getopt32(argv, "d:s", &delims); | ||
113 | argv += optind; | ||
114 | |||
115 | if (opt & PASTE_OPT_DELIMITERS) { | ||
116 | if (!delims[0]) | ||
117 | bb_error_msg_and_die("-d '' is not supported"); | ||
118 | /* unknown mappings are not changed: "\z" -> '\\' 'z' */ | ||
119 | /* trailing backslash, if any, is preserved */ | ||
120 | del_cnt = strcpy_and_process_escape_sequences(delims, delims) - delims; | ||
121 | /* note: handle NUL properly (do not stop at it!): try -d'\t\0\t' */ | ||
122 | } | ||
123 | |||
124 | if (!argv[0]) | ||
125 | (--argv)[0] = (char*) "-"; | ||
126 | for (i = 0; argv[i]; ++i) { | ||
127 | argv[i] = (void*) fopen_or_warn_stdin(argv[i]); | ||
128 | if (!argv[i]) | ||
129 | xfunc_die(); | ||
130 | } | ||
131 | |||
132 | if (opt & PASTE_OPT_SEPARATE) | ||
133 | paste_files_separate((FILE**)argv, delims, del_cnt); | ||
134 | else | ||
135 | paste_files((FILE**)argv, i, delims, del_cnt); | ||
136 | |||
137 | fflush_stdout_and_exit(0); | ||
138 | } | ||
diff --git a/docs/posix_conformance.txt b/docs/posix_conformance.txt index c0582dc23..8b9112020 100644 --- a/docs/posix_conformance.txt +++ b/docs/posix_conformance.txt | |||
@@ -22,7 +22,7 @@ POSIX Tools supported only as shell built-ins (ash shell): | |||
22 | POSIX Tools not supported: | 22 | POSIX Tools not supported: |
23 | asa, at, batch, bc, c99, command, compress, csplit, ex, fc, file, | 23 | asa, at, batch, bc, c99, command, compress, csplit, ex, fc, file, |
24 | gencat, getconf, iconv, join, link, locale, localedef, lp, m4, | 24 | gencat, getconf, iconv, join, link, locale, localedef, lp, m4, |
25 | mailx, newgrp, nl, paste, pathchk, pax, pr, qalter, qdel, qhold, qmove, | 25 | mailx, newgrp, nl, pathchk, pax, pr, qalter, qdel, qhold, qmove, |
26 | qmsg, qrerun, qrls, qselect, qsig, qstat, qsub, tabs, talk, tput, | 26 | qmsg, qrerun, qrls, qselect, qsig, qstat, qsub, tabs, talk, tput, |
27 | tsort, unlink, uucp, uustat, uux | 27 | tsort, unlink, uucp, uustat, uux |
28 | 28 | ||
@@ -469,6 +469,12 @@ od POSIX options | |||
469 | -x | no | no | | 469 | -x | no | no | |
470 | od Busybox specific options: None | 470 | od Busybox specific options: None |
471 | 471 | ||
472 | paste POSIX options | ||
473 | option | exists | compliant | remarks | ||
474 | -d list | yes | yes | | ||
475 | -s | yes | yes | | ||
476 | paste Busybox specific options: None | ||
477 | |||
472 | patch POSIX options | 478 | patch POSIX options |
473 | option | exists | compliant | remarks | 479 | option | exists | compliant | remarks |
474 | -D define | no | no | | 480 | -D define | no | no | |
diff --git a/testsuite/paste/paste b/testsuite/paste/paste new file mode 100644 index 000000000..349b49d49 --- /dev/null +++ b/testsuite/paste/paste | |||
@@ -0,0 +1,20 @@ | |||
1 | cat > foo <<EOF | ||
2 | foo1 | ||
3 | foo2 | ||
4 | foo3 | ||
5 | EOF | ||
6 | |||
7 | cat > bar <<EOF | ||
8 | bar1 | ||
9 | bar2 | ||
10 | bar3 | ||
11 | EOF | ||
12 | |||
13 | cat > baz <<EOF | ||
14 | foo1 bar1 | ||
15 | foo2 bar2 | ||
16 | foo3 bar3 | ||
17 | EOF | ||
18 | |||
19 | busybox paste foo bar > qux | ||
20 | diff -u baz qux | ||
diff --git a/testsuite/paste/paste-back-cuted-lines b/testsuite/paste/paste-back-cuted-lines new file mode 100644 index 000000000..a8171bf1e --- /dev/null +++ b/testsuite/paste/paste-back-cuted-lines | |||
@@ -0,0 +1,9 @@ | |||
1 | cat > foo <<EOF | ||
2 | this is the first line | ||
3 | this is the second line | ||
4 | this is the third line | ||
5 | EOF | ||
6 | cut -b 1-13 -n foo > foo1 | ||
7 | cut -b 14- -n foo > foo2 | ||
8 | busybox paste -d '\0' foo1 foo2 > bar | ||
9 | cmp foo bar | ||
diff --git a/testsuite/paste/paste-multi-stdin b/testsuite/paste/paste-multi-stdin new file mode 100644 index 000000000..fee543058 --- /dev/null +++ b/testsuite/paste/paste-multi-stdin | |||
@@ -0,0 +1,16 @@ | |||
1 | cat > foo <<EOF | ||
2 | line1 | ||
3 | line2 | ||
4 | line3 | ||
5 | line4 | ||
6 | line5 | ||
7 | line6 | ||
8 | EOF | ||
9 | |||
10 | cat > bar <<EOF | ||
11 | line1 line2 line3 | ||
12 | line4 line5 line6 | ||
13 | EOF | ||
14 | |||
15 | busybox paste - - - < foo > baz | ||
16 | cmp bar baz | ||
diff --git a/testsuite/paste/paste-pairs b/testsuite/paste/paste-pairs new file mode 100644 index 000000000..90725fa87 --- /dev/null +++ b/testsuite/paste/paste-pairs | |||
@@ -0,0 +1,16 @@ | |||
1 | cat > foo <<EOF | ||
2 | foo1 | ||
3 | bar1 | ||
4 | foo2 | ||
5 | bar2 | ||
6 | foo3 | ||
7 | EOF | ||
8 | |||
9 | cat > bar <<EOF | ||
10 | foo1 bar1 | ||
11 | foo2 bar2 | ||
12 | foo3 | ||
13 | EOF | ||
14 | |||
15 | busybox paste -s -d "\t\n" foo > baz | ||
16 | cmp bar baz | ||
diff --git a/testsuite/paste/paste-separate b/testsuite/paste/paste-separate new file mode 100644 index 000000000..40793fb31 --- /dev/null +++ b/testsuite/paste/paste-separate | |||
@@ -0,0 +1,19 @@ | |||
1 | cat > foo <<EOF | ||
2 | foo1 | ||
3 | foo2 | ||
4 | foo3 | ||
5 | EOF | ||
6 | |||
7 | cat > bar <<EOF | ||
8 | bar1 | ||
9 | bar2 | ||
10 | bar3 | ||
11 | EOF | ||
12 | |||
13 | cat > baz <<EOF | ||
14 | foo1 foo2 foo3 | ||
15 | bar1 bar2 bar3 | ||
16 | EOF | ||
17 | |||
18 | busybox paste -s foo bar > qux | ||
19 | cmp baz qux | ||