Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "paste (4.9 kb)" |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: paste is used to paste lines of different files together |
| 14 | //config: and write the result to stdout |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | 84d5edd | 2020-12-13 22:34:05 +0100 | [diff] [blame] | 21 | //usage: "[-d LIST] [-s] [FILE]..." |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 22 | //usage:#define paste_full_usage "\n\n" |
Denys Vlasenko | f1db95a | 2017-03-23 17:41:59 +0100 | [diff] [blame] | 23 | //usage: "Paste lines from each input file, separated with tab\n" |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 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; |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 39 | int active_files = file_cnt; |
| 40 | int i; |
| 41 | |
| 42 | while (active_files > 0) { |
Denys Vlasenko | f4a670a | 2017-03-23 17:58:32 +0100 | [diff] [blame] | 43 | int del_idx = 0; |
| 44 | |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 45 | for (i = 0; i < file_cnt; ++i) { |
| 46 | if (files[i] == NULL) |
| 47 | continue; |
| 48 | |
| 49 | line = xmalloc_fgetline(files[i]); |
| 50 | if (!line) { |
| 51 | fclose_if_not_stdin(files[i]); |
| 52 | files[i] = NULL; |
| 53 | --active_files; |
| 54 | continue; |
| 55 | } |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 56 | fputs_stdout(line); |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 57 | free(line); |
| 58 | delim = '\n'; |
| 59 | if (i != file_cnt - 1) { |
| 60 | delim = delims[del_idx++]; |
| 61 | if (del_idx == del_cnt) |
| 62 | del_idx = 0; |
| 63 | } |
| 64 | if (delim != '\0') |
| 65 | fputc(delim, stdout); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void paste_files_separate(FILE** files, char* delims, int del_cnt) |
| 71 | { |
| 72 | char *line, *next_line; |
| 73 | char delim; |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 74 | int i; |
| 75 | |
| 76 | for (i = 0; files[i]; ++i) { |
Denys Vlasenko | f4a670a | 2017-03-23 17:58:32 +0100 | [diff] [blame] | 77 | int del_idx = 0; |
| 78 | |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 79 | line = NULL; |
| 80 | while ((next_line = xmalloc_fgetline(files[i])) != NULL) { |
| 81 | if (line) { |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 82 | fputs_stdout(line); |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 83 | free(line); |
| 84 | delim = delims[del_idx++]; |
| 85 | if (del_idx == del_cnt) |
| 86 | del_idx = 0; |
| 87 | if (delim != '\0') |
| 88 | fputc(delim, stdout); |
| 89 | } |
| 90 | line = next_line; |
| 91 | } |
| 92 | if (line) { |
| 93 | /* coreutils adds \n even if this is a final line |
| 94 | * of the last file and it was not \n-terminated. |
| 95 | */ |
| 96 | printf("%s\n", line); |
| 97 | free(line); |
| 98 | } |
| 99 | fclose_if_not_stdin(files[i]); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #define PASTE_OPT_DELIMITERS (1 << 0) |
| 104 | #define PASTE_OPT_SEPARATE (1 << 1) |
| 105 | |
| 106 | int paste_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 107 | int paste_main(int argc UNUSED_PARAM, char **argv) |
| 108 | { |
| 109 | char *delims = (char*)"\t"; |
| 110 | int del_cnt = 1; |
| 111 | unsigned opt; |
| 112 | int i; |
| 113 | |
| 114 | opt = getopt32(argv, "d:s", &delims); |
| 115 | argv += optind; |
| 116 | |
| 117 | if (opt & PASTE_OPT_DELIMITERS) { |
| 118 | if (!delims[0]) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 119 | bb_simple_error_msg_and_die("-d '' is not supported"); |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 120 | /* unknown mappings are not changed: "\z" -> '\\' 'z' */ |
| 121 | /* trailing backslash, if any, is preserved */ |
| 122 | del_cnt = strcpy_and_process_escape_sequences(delims, delims) - delims; |
| 123 | /* note: handle NUL properly (do not stop at it!): try -d'\t\0\t' */ |
| 124 | } |
| 125 | |
| 126 | if (!argv[0]) |
| 127 | (--argv)[0] = (char*) "-"; |
| 128 | for (i = 0; argv[i]; ++i) { |
| 129 | argv[i] = (void*) fopen_or_warn_stdin(argv[i]); |
Denys Vlasenko | 876c121 | 2017-03-24 15:00:12 +0100 | [diff] [blame] | 130 | if (!argv[i]) |
Maxime Coste | d2383f5 | 2017-03-23 17:35:20 +0100 | [diff] [blame] | 131 | xfunc_die(); |
| 132 | } |
| 133 | |
| 134 | if (opt & PASTE_OPT_SEPARATE) |
| 135 | paste_files_separate((FILE**)argv, delims, del_cnt); |
| 136 | else |
| 137 | paste_files((FILE**)argv, i, delims, del_cnt); |
| 138 | |
| 139 | fflush_stdout_and_exit(0); |
| 140 | } |