split.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 tarball for details.
  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 = open_or_warn_stdin(argv[0]);
  76. if (fd == -1)
  77. return EXIT_FAILURE;
  78. xmove_fd(fd, STDIN_FILENO);
  79. } else {
  80. argv[0] = (char *) bb_msg_standard_input;
  81. }
  82. if (NAME_MAX < strlen(sfx) + suffix_len)
  83. bb_error_msg_and_die("suffix too long");
  84. {
  85. char *char_p = xzalloc(suffix_len + 1);
  86. memset(char_p, 'a', suffix_len);
  87. pfx = xasprintf("%s%s", sfx, char_p);
  88. if (ENABLE_FEATURE_CLEAN_UP)
  89. free(char_p);
  90. }
  91. while (1) {
  92. bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE);
  93. if (!bytes_read)
  94. break;
  95. if (bytes_read < 0)
  96. bb_simple_perror_msg_and_die(argv[0]);
  97. src = read_buffer;
  98. do {
  99. if (!remaining) {
  100. if (!pfx)
  101. bb_error_msg_and_die("suffixes exhausted");
  102. xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
  103. pfx = next_file(pfx, suffix_len);
  104. remaining = cnt;
  105. }
  106. if (opt & SPLIT_OPT_b) {
  107. /* split by bytes */
  108. to_write = (bytes_read < remaining) ? bytes_read : remaining;
  109. remaining -= to_write;
  110. } else {
  111. /* split by lines */
  112. /* can be sped up by using _memrchr_
  113. * and writing many lines at once... */
  114. char *end = memchr(src, '\n', bytes_read);
  115. if (end) {
  116. --remaining;
  117. to_write = end - src + 1;
  118. } else {
  119. to_write = bytes_read;
  120. }
  121. }
  122. xwrite(STDOUT_FILENO, src, to_write);
  123. bytes_read -= to_write;
  124. src += to_write;
  125. } while (bytes_read);
  126. }
  127. return EXIT_SUCCESS;
  128. }