split.c 3.5 KB

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