split.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /* BB_AUDIT: SUSv3 compliant
  9. * SUSv3 requirements:
  10. * http://www.opengroup.org/onlinepubs/009695399/utilities/split.html
  11. */
  12. //usage:#define split_trivial_usage
  13. //usage: "[OPTIONS] [INPUT [PREFIX]]"
  14. //usage:#define split_full_usage "\n\n"
  15. //usage: " -b N[k|m] Split by N (kilo|mega)bytes"
  16. //usage: "\n -l N Split by N lines"
  17. //usage: "\n -a N Use N letters as suffix"
  18. //usage:
  19. //usage:#define split_example_usage
  20. //usage: "$ split TODO foo\n"
  21. //usage: "$ cat TODO | split -a 2 -l 2 TODO_\n"
  22. #include "libbb.h"
  23. static const struct suffix_mult split_suffices[] = {
  24. #if ENABLE_FEATURE_SPLIT_FANCY
  25. { "b", 512 },
  26. #endif
  27. { "k", 1024 },
  28. { "m", 1024*1024 },
  29. #if ENABLE_FEATURE_SPLIT_FANCY
  30. { "g", 1024*1024*1024 },
  31. #endif
  32. { "", 0 }
  33. };
  34. /* Increment the suffix part of the filename.
  35. * Returns NULL if we are out of filenames.
  36. */
  37. static char *next_file(char *old, unsigned suffix_len)
  38. {
  39. size_t end = strlen(old);
  40. unsigned i = 1;
  41. char *curr;
  42. while (1) {
  43. curr = old + end - i;
  44. if (*curr < 'z') {
  45. *curr += 1;
  46. break;
  47. }
  48. i++;
  49. if (i > suffix_len) {
  50. return NULL;
  51. }
  52. *curr = 'a';
  53. }
  54. return old;
  55. }
  56. #define read_buffer bb_common_bufsiz1
  57. enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
  58. #define SPLIT_OPT_l (1<<0)
  59. #define SPLIT_OPT_b (1<<1)
  60. #define SPLIT_OPT_a (1<<2)
  61. int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  62. int split_main(int argc UNUSED_PARAM, char **argv)
  63. {
  64. unsigned suffix_len = 2;
  65. char *pfx;
  66. char *count_p;
  67. const char *sfx;
  68. off_t cnt = 1000;
  69. off_t remaining = 0;
  70. unsigned opt;
  71. ssize_t bytes_read, to_write;
  72. char *src;
  73. opt_complementary = "?2:a+"; /* max 2 args; -a N */
  74. opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
  75. if (opt & SPLIT_OPT_l)
  76. cnt = XATOOFF(count_p);
  77. if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
  78. cnt = xatoull_sfx(count_p, split_suffices);
  79. sfx = "x";
  80. argv += optind;
  81. if (argv[0]) {
  82. int fd;
  83. if (argv[1])
  84. sfx = argv[1];
  85. fd = xopen_stdin(argv[0]);
  86. xmove_fd(fd, STDIN_FILENO);
  87. } else {
  88. argv[0] = (char *) bb_msg_standard_input;
  89. }
  90. if (NAME_MAX < strlen(sfx) + suffix_len)
  91. bb_error_msg_and_die("suffix too long");
  92. {
  93. char *char_p = xzalloc(suffix_len + 1);
  94. memset(char_p, 'a', suffix_len);
  95. pfx = xasprintf("%s%s", sfx, char_p);
  96. if (ENABLE_FEATURE_CLEAN_UP)
  97. free(char_p);
  98. }
  99. while (1) {
  100. bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
  101. if (!bytes_read)
  102. break;
  103. if (bytes_read < 0)
  104. bb_simple_perror_msg_and_die(argv[0]);
  105. src = read_buffer;
  106. do {
  107. if (!remaining) {
  108. if (!pfx)
  109. bb_error_msg_and_die("suffixes exhausted");
  110. xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
  111. pfx = next_file(pfx, suffix_len);
  112. remaining = cnt;
  113. }
  114. if (opt & SPLIT_OPT_b) {
  115. /* split by bytes */
  116. to_write = (bytes_read < remaining) ? bytes_read : remaining;
  117. remaining -= to_write;
  118. } else {
  119. /* split by lines */
  120. /* can be sped up by using _memrchr_
  121. * and writing many lines at once... */
  122. char *end = memchr(src, '\n', bytes_read);
  123. if (end) {
  124. --remaining;
  125. to_write = end - src + 1;
  126. } else {
  127. to_write = bytes_read;
  128. }
  129. }
  130. xwrite(STDOUT_FILENO, src, to_write);
  131. bytes_read -= to_write;
  132. src += to_write;
  133. } while (bytes_read);
  134. }
  135. return EXIT_SUCCESS;
  136. }