paste.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 (5.1 kb)"
  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. //applet:IF_PASTE(APPLET_NOEXEC(paste, paste, BB_DIR_USR_BIN, BB_SUID_DROP, paste))
  16. //kbuild:lib-$(CONFIG_PASTE) += paste.o
  17. //usage:#define paste_trivial_usage
  18. //usage: "[-d LIST] [-s] [FILE]..."
  19. //usage:#define paste_full_usage "\n\n"
  20. //usage: "Paste lines from each input file, separated with tab\n"
  21. //usage: "\n -d LIST Use delimiters from LIST, not tab"
  22. //usage: "\n -s Serial: one file at a time"
  23. //usage:
  24. //usage:#define paste_example_usage
  25. //usage: "# write out directory in four columns\n"
  26. //usage: "$ ls | paste - - - -\n"
  27. //usage: "# combine pairs of lines from a file into single lines\n"
  28. //usage: "$ paste -s -d '\\t\\n' file\n"
  29. #include "libbb.h"
  30. static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt)
  31. {
  32. char *line;
  33. char delim;
  34. int active_files = file_cnt;
  35. int i;
  36. while (active_files > 0) {
  37. int del_idx = 0;
  38. for (i = 0; i < file_cnt; ++i) {
  39. if (files[i] == NULL)
  40. continue;
  41. line = xmalloc_fgetline(files[i]);
  42. if (!line) {
  43. fclose_if_not_stdin(files[i]);
  44. files[i] = NULL;
  45. --active_files;
  46. continue;
  47. }
  48. fputs_stdout(line);
  49. free(line);
  50. delim = '\n';
  51. if (i != file_cnt - 1) {
  52. delim = delims[del_idx++];
  53. if (del_idx == del_cnt)
  54. del_idx = 0;
  55. }
  56. if (delim != '\0')
  57. fputc(delim, stdout);
  58. }
  59. }
  60. }
  61. static void paste_files_separate(FILE** files, char* delims, int del_cnt)
  62. {
  63. char *line, *next_line;
  64. char delim;
  65. int i;
  66. for (i = 0; files[i]; ++i) {
  67. int del_idx = 0;
  68. line = NULL;
  69. while ((next_line = xmalloc_fgetline(files[i])) != NULL) {
  70. if (line) {
  71. fputs_stdout(line);
  72. free(line);
  73. delim = delims[del_idx++];
  74. if (del_idx == del_cnt)
  75. del_idx = 0;
  76. if (delim != '\0')
  77. fputc(delim, stdout);
  78. }
  79. line = next_line;
  80. }
  81. if (line) {
  82. /* coreutils adds \n even if this is a final line
  83. * of the last file and it was not \n-terminated.
  84. */
  85. printf("%s\n", line);
  86. free(line);
  87. }
  88. fclose_if_not_stdin(files[i]);
  89. }
  90. }
  91. #define PASTE_OPT_DELIMITERS (1 << 0)
  92. #define PASTE_OPT_SEPARATE (1 << 1)
  93. int paste_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  94. int paste_main(int argc UNUSED_PARAM, char **argv)
  95. {
  96. char *delims = (char*)"\t";
  97. int del_cnt = 1;
  98. unsigned opt;
  99. int i;
  100. opt = getopt32(argv, "d:s", &delims);
  101. argv += optind;
  102. if (opt & PASTE_OPT_DELIMITERS) {
  103. if (!delims[0])
  104. bb_simple_error_msg_and_die("-d '' is not supported");
  105. /* unknown mappings are not changed: "\z" -> '\\' 'z' */
  106. /* trailing backslash, if any, is preserved */
  107. del_cnt = strcpy_and_process_escape_sequences(delims, delims) - delims;
  108. /* note: handle NUL properly (do not stop at it!): try -d'\t\0\t' */
  109. }
  110. if (!argv[0])
  111. (--argv)[0] = (char*) "-";
  112. for (i = 0; argv[i]; ++i) {
  113. argv[i] = (void*) fopen_or_warn_stdin(argv[i]);
  114. if (!argv[i])
  115. xfunc_die();
  116. }
  117. if (opt & PASTE_OPT_SEPARATE)
  118. paste_files_separate((FILE**)argv, delims, del_cnt);
  119. else
  120. paste_files((FILE**)argv, i, delims, del_cnt);
  121. fflush_stdout_and_exit(0);
  122. }