3
0

split.c 3.0 KB

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