1 /* vi: set sw=4 ts=4: */
3 * paste.c - implementation of the posix paste command
5 * Written by Maxime Coste <mawww@kakoune.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "paste"
13 //config: paste is used to paste lines of different files together
14 //config: and write the result to stdout
16 //applet:IF_PASTE(APPLET_NOEXEC(paste, paste, BB_DIR_USR_BIN, BB_SUID_DROP, paste))
18 //kbuild:lib-$(CONFIG_PASTE) += paste.o
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"
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"
35 static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt)
40 int active_files = file_cnt;
43 while (active_files > 0) {
44 for (i = 0; i < file_cnt; ++i) {
48 line = xmalloc_fgetline(files[i]);
50 fclose_if_not_stdin(files[i]);
58 if (i != file_cnt - 1) {
59 delim = delims[del_idx++];
60 if (del_idx == del_cnt)
69 static void paste_files_separate(FILE** files, char* delims, int del_cnt)
71 char *line, *next_line;
76 for (i = 0; files[i]; ++i) {
78 while ((next_line = xmalloc_fgetline(files[i])) != NULL) {
82 delim = delims[del_idx++];
83 if (del_idx == del_cnt)
91 /* coreutils adds \n even if this is a final line
92 * of the last file and it was not \n-terminated.
97 fclose_if_not_stdin(files[i]);
101 #define PASTE_OPT_DELIMITERS (1 << 0)
102 #define PASTE_OPT_SEPARATE (1 << 1)
104 int paste_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int paste_main(int argc UNUSED_PARAM, char **argv)
107 char *delims = (char*)"\t";
112 opt = getopt32(argv, "d:s", &delims);
115 if (opt & PASTE_OPT_DELIMITERS) {
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' */
125 (--argv)[0] = (char*) "-";
126 for (i = 0; argv[i]; ++i) {
127 argv[i] = (void*) fopen_or_warn_stdin(argv[i]);
132 if (opt & PASTE_OPT_SEPARATE)
133 paste_files_separate((FILE**)argv, delims, del_cnt);
135 paste_files((FILE**)argv, i, delims, del_cnt);
137 fflush_stdout_and_exit(0);