3
0

split.c 3.0 KB

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