split.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * split - split a file into pieces
  4. * Copyright (c) 2007 Bernhard Reutner-Fischer
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //config:config SPLIT
  9. //config: bool "split (5 kb)"
  10. //config: default y
  11. //config: help
  12. //config: Split a file into pieces.
  13. //config:
  14. //config:config FEATURE_SPLIT_FANCY
  15. //config: bool "Fancy extensions"
  16. //config: default y
  17. //config: depends on SPLIT
  18. //config: help
  19. //config: Add support for features not required by SUSv3.
  20. //config: Supports additional suffixes 'b' for 512 bytes,
  21. //config: 'g' for 1GiB for the -b option.
  22. //applet:IF_SPLIT(APPLET(split, BB_DIR_USR_BIN, BB_SUID_DROP))
  23. //kbuild:lib-$(CONFIG_SPLIT) += split.o
  24. /* BB_AUDIT: SUSv3 compliant
  25. * SUSv3 requirements:
  26. * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
  27. */
  28. //usage:#define split_trivial_usage
  29. //usage: "[OPTIONS] [INPUT [PREFIX]]"
  30. //usage:#define split_full_usage "\n\n"
  31. //usage: " -b N[k|m] Split by N (kilo|mega)bytes"
  32. //usage: "\n -l N Split by N lines"
  33. //usage: "\n -a N Use N letters as suffix"
  34. //usage:
  35. //usage:#define split_example_usage
  36. //usage: "$ split TODO foo\n"
  37. //usage: "$ cat TODO | split -a 2 -l 2 TODO_\n"
  38. #include "libbb.h"
  39. #include "common_bufsiz.h"
  40. #if ENABLE_FEATURE_SPLIT_FANCY
  41. static const struct suffix_mult split_suffixes[] ALIGN_SUFFIX = {
  42. { "b", 512 },
  43. { "k", 1024 },
  44. { "m", 1024*1024 },
  45. { "g", 1024*1024*1024 },
  46. { "", 0 }
  47. };
  48. #endif
  49. /* Increment the suffix part of the filename.
  50. * Returns NULL if we are out of filenames.
  51. */
  52. static char *next_file(char *old, unsigned suffix_len)
  53. {
  54. size_t end = strlen(old);
  55. unsigned i = 1;
  56. char *curr;
  57. while (1) {
  58. curr = old + end - i;
  59. if (*curr < 'z') {
  60. *curr += 1;
  61. break;
  62. }
  63. i++;
  64. if (i > suffix_len) {
  65. return NULL;
  66. }
  67. *curr = 'a';
  68. }
  69. return old;
  70. }
  71. #define read_buffer bb_common_bufsiz1
  72. enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
  73. #define SPLIT_OPT_l (1<<0)
  74. #define SPLIT_OPT_b (1<<1)
  75. #define SPLIT_OPT_a (1<<2)
  76. int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  77. int split_main(int argc UNUSED_PARAM, char **argv)
  78. {
  79. unsigned suffix_len = 2;
  80. char *pfx;
  81. char *count_p;
  82. const char *sfx;
  83. off_t cnt = 1000;
  84. off_t remaining = 0;
  85. unsigned opt;
  86. ssize_t bytes_read, to_write;
  87. char *src;
  88. setup_common_bufsiz();
  89. opt = getopt32(argv, "^"
  90. "l:b:a:+" /* -a N */
  91. "\0" "?2"/*max 2 args*/,
  92. &count_p, &count_p, &suffix_len
  93. );
  94. if (opt & SPLIT_OPT_l)
  95. cnt = XATOOFF(count_p);
  96. if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
  97. cnt = xatoull_sfx(count_p,
  98. IF_FEATURE_SPLIT_FANCY(split_suffixes)
  99. IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
  100. );
  101. sfx = "x";
  102. argv += optind;
  103. if (argv[0]) {
  104. int fd;
  105. if (argv[1])
  106. sfx = argv[1];
  107. fd = xopen_stdin(argv[0]);
  108. xmove_fd(fd, STDIN_FILENO);
  109. } else {
  110. argv[0] = (char *) bb_msg_standard_input;
  111. }
  112. if (NAME_MAX < strlen(sfx) + suffix_len)
  113. bb_simple_error_msg_and_die("suffix too long");
  114. {
  115. char *char_p = xzalloc(suffix_len + 1);
  116. memset(char_p, 'a', suffix_len);
  117. pfx = xasprintf("%s%s", sfx, char_p);
  118. if (ENABLE_FEATURE_CLEAN_UP)
  119. free(char_p);
  120. }
  121. while (1) {
  122. bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
  123. if (!bytes_read)
  124. break;
  125. if (bytes_read < 0)
  126. bb_simple_perror_msg_and_die(argv[0]);
  127. src = read_buffer;
  128. do {
  129. if (!remaining) {
  130. if (!pfx)
  131. bb_simple_error_msg_and_die("suffixes exhausted");
  132. xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
  133. pfx = next_file(pfx, suffix_len);
  134. remaining = cnt;
  135. }
  136. if (opt & SPLIT_OPT_b) {
  137. /* split by bytes */
  138. to_write = (bytes_read < remaining) ? bytes_read : remaining;
  139. remaining -= to_write;
  140. } else {
  141. /* split by lines */
  142. /* can be sped up by using _memrchr_
  143. * and writing many lines at once... */
  144. char *end = memchr(src, '\n', bytes_read);
  145. if (end) {
  146. --remaining;
  147. to_write = end - src + 1;
  148. } else {
  149. to_write = bytes_read;
  150. }
  151. }
  152. xwrite(STDOUT_FILENO, src, to_write);
  153. bytes_read -= to_write;
  154. src += to_write;
  155. } while (bytes_read);
  156. }
  157. return EXIT_SUCCESS;
  158. }