3
0

split.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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"
  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[] = {
  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_complementary = "?2"; /* max 2 args; -a N */
  90. opt = getopt32(argv, "l:b:a:+", &count_p, &count_p, &suffix_len);
  91. if (opt & SPLIT_OPT_l)
  92. cnt = XATOOFF(count_p);
  93. if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
  94. cnt = xatoull_sfx(count_p,
  95. IF_FEATURE_SPLIT_FANCY(split_suffixes)
  96. IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
  97. );
  98. sfx = "x";
  99. argv += optind;
  100. if (argv[0]) {
  101. int fd;
  102. if (argv[1])
  103. sfx = argv[1];
  104. fd = xopen_stdin(argv[0]);
  105. xmove_fd(fd, STDIN_FILENO);
  106. } else {
  107. argv[0] = (char *) bb_msg_standard_input;
  108. }
  109. if (NAME_MAX < strlen(sfx) + suffix_len)
  110. bb_error_msg_and_die("suffix too long");
  111. {
  112. char *char_p = xzalloc(suffix_len + 1);
  113. memset(char_p, 'a', suffix_len);
  114. pfx = xasprintf("%s%s", sfx, char_p);
  115. if (ENABLE_FEATURE_CLEAN_UP)
  116. free(char_p);
  117. }
  118. while (1) {
  119. bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
  120. if (!bytes_read)
  121. break;
  122. if (bytes_read < 0)
  123. bb_simple_perror_msg_and_die(argv[0]);
  124. src = read_buffer;
  125. do {
  126. if (!remaining) {
  127. if (!pfx)
  128. bb_error_msg_and_die("suffixes exhausted");
  129. xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
  130. pfx = next_file(pfx, suffix_len);
  131. remaining = cnt;
  132. }
  133. if (opt & SPLIT_OPT_b) {
  134. /* split by bytes */
  135. to_write = (bytes_read < remaining) ? bytes_read : remaining;
  136. remaining -= to_write;
  137. } else {
  138. /* split by lines */
  139. /* can be sped up by using _memrchr_
  140. * and writing many lines at once... */
  141. char *end = memchr(src, '\n', bytes_read);
  142. if (end) {
  143. --remaining;
  144. to_write = end - src + 1;
  145. } else {
  146. to_write = bytes_read;
  147. }
  148. }
  149. xwrite(STDOUT_FILENO, src, to_write);
  150. bytes_read -= to_write;
  151. src += to_write;
  152. } while (bytes_read);
  153. }
  154. return EXIT_SUCCESS;
  155. }