split.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "common_bufsiz.h"
  24. #if ENABLE_FEATURE_SPLIT_FANCY
  25. static const struct suffix_mult split_suffixes[] = {
  26. { "b", 512 },
  27. { "k", 1024 },
  28. { "m", 1024*1024 },
  29. { "g", 1024*1024*1024 },
  30. { "", 0 }
  31. };
  32. #endif
  33. /* Increment the suffix part of the filename.
  34. * Returns NULL if we are out of filenames.
  35. */
  36. static char *next_file(char *old, unsigned suffix_len)
  37. {
  38. size_t end = strlen(old);
  39. unsigned i = 1;
  40. char *curr;
  41. while (1) {
  42. curr = old + end - i;
  43. if (*curr < 'z') {
  44. *curr += 1;
  45. break;
  46. }
  47. i++;
  48. if (i > suffix_len) {
  49. return NULL;
  50. }
  51. *curr = 'a';
  52. }
  53. return old;
  54. }
  55. #define read_buffer bb_common_bufsiz1
  56. enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
  57. #define SPLIT_OPT_l (1<<0)
  58. #define SPLIT_OPT_b (1<<1)
  59. #define SPLIT_OPT_a (1<<2)
  60. int split_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  61. int split_main(int argc UNUSED_PARAM, char **argv)
  62. {
  63. unsigned suffix_len = 2;
  64. char *pfx;
  65. char *count_p;
  66. const char *sfx;
  67. off_t cnt = 1000;
  68. off_t remaining = 0;
  69. unsigned opt;
  70. ssize_t bytes_read, to_write;
  71. char *src;
  72. setup_common_bufsiz();
  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,
  79. IF_FEATURE_SPLIT_FANCY(split_suffixes)
  80. IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
  81. );
  82. sfx = "x";
  83. argv += optind;
  84. if (argv[0]) {
  85. int fd;
  86. if (argv[1])
  87. sfx = argv[1];
  88. fd = xopen_stdin(argv[0]);
  89. xmove_fd(fd, STDIN_FILENO);
  90. } else {
  91. argv[0] = (char *) bb_msg_standard_input;
  92. }
  93. if (NAME_MAX < strlen(sfx) + suffix_len)
  94. bb_error_msg_and_die("suffix too long");
  95. {
  96. char *char_p = xzalloc(suffix_len + 1);
  97. memset(char_p, 'a', suffix_len);
  98. pfx = xasprintf("%s%s", sfx, char_p);
  99. if (ENABLE_FEATURE_CLEAN_UP)
  100. free(char_p);
  101. }
  102. while (1) {
  103. bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
  104. if (!bytes_read)
  105. break;
  106. if (bytes_read < 0)
  107. bb_simple_perror_msg_and_die(argv[0]);
  108. src = read_buffer;
  109. do {
  110. if (!remaining) {
  111. if (!pfx)
  112. bb_error_msg_and_die("suffixes exhausted");
  113. xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
  114. pfx = next_file(pfx, suffix_len);
  115. remaining = cnt;
  116. }
  117. if (opt & SPLIT_OPT_b) {
  118. /* split by bytes */
  119. to_write = (bytes_read < remaining) ? bytes_read : remaining;
  120. remaining -= to_write;
  121. } else {
  122. /* split by lines */
  123. /* can be sped up by using _memrchr_
  124. * and writing many lines at once... */
  125. char *end = memchr(src, '\n', bytes_read);
  126. if (end) {
  127. --remaining;
  128. to_write = end - src + 1;
  129. } else {
  130. to_write = bytes_read;
  131. }
  132. }
  133. xwrite(STDOUT_FILENO, src, to_write);
  134. bytes_read -= to_write;
  135. src += to_write;
  136. } while (bytes_read);
  137. }
  138. return EXIT_SUCCESS;
  139. }