split.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* split.c - split a file into smaller files
  2. *
  3. * Copyright 2013 Rob Landley <rob@landley.net>
  4. *
  5. * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/split.html
  6. *
  7. * Standard does not cover:
  8. * - should splitting an empty file produce an empty outfile? (Went with "no".)
  9. * - permissions on output file
  10. USE_SPLIT(NEWTOY(split, ">2a#<1=2>9b#<1l#<1n#<1[!bl][!bn][!ln]", TOYFLAG_USR|TOYFLAG_BIN))
  11. config SPLIT
  12. bool "split"
  13. default y
  14. help
  15. usage: split [-a SUFFIX_LEN] [-b BYTES] [-l LINES] [-n PARTS] [INPUT [OUTPUT]]
  16. Copy INPUT (or stdin) data to a series of OUTPUT (or "x") files with
  17. alphabetically increasing suffix (aa, ab, ac... az, ba, bb...).
  18. -a Suffix length (default 2)
  19. -b BYTES/file (10, 10k, 10m, 10g...)
  20. -l LINES/file (default 1000)
  21. -n PARTS many equal length files
  22. */
  23. #define FOR_split
  24. #include "toys.h"
  25. GLOBALS(
  26. long n, l, b, a;
  27. char *outfile;
  28. )
  29. static void do_split(int infd, char *in)
  30. {
  31. unsigned long bytesleft, linesleft, filenum, len, pos;
  32. int outfd = -1;
  33. struct stat st;
  34. // posix doesn't cover permissions on output file, so copy input (or 0777)
  35. st.st_mode = 0777;
  36. st.st_size = 0;
  37. fstat(infd, &st);
  38. if (TT.n && (TT.b = st.st_size/TT.n)<1) return error_msg("%s: no size", in);
  39. len = pos = filenum = bytesleft = linesleft = 0;
  40. for (;;) {
  41. int i, j;
  42. // Refill toybuf?
  43. if (len == pos) {
  44. if (!(len = xread(infd, toybuf, sizeof(toybuf)))) break;
  45. pos = 0;
  46. }
  47. // Start new output file?
  48. if ((TT.b && !bytesleft) || (TT.l && !linesleft)) {
  49. char *s = TT.outfile + strlen(TT.outfile);
  50. j = filenum++;
  51. for (i = 0; i<TT.a; i++) {
  52. *(--s) = 'a'+(j%26);
  53. j /= 26;
  54. }
  55. if (j) error_exit("bad suffix");
  56. bytesleft = TT.b + ((filenum == TT.n) ? st.st_size%TT.n : 0);
  57. linesleft = TT.l;
  58. xclose(outfd);
  59. outfd = xcreate(TT.outfile, O_RDWR|O_CREAT|O_TRUNC, st.st_mode & 0777);
  60. }
  61. // Write next chunk of output.
  62. if (TT.l) {
  63. for (i = pos; i < len; ) {
  64. if (toybuf[i++] == '\n' && !--linesleft) break;
  65. if (!--bytesleft) break;
  66. }
  67. j = i - pos;
  68. } else {
  69. j = len - pos;
  70. if (j > bytesleft) j = bytesleft;
  71. bytesleft -= j;
  72. }
  73. xwrite(outfd, toybuf+pos, j);
  74. pos += j;
  75. }
  76. if (CFG_TOYBOX_FREE) {
  77. xclose(outfd);
  78. if (infd) close(infd);
  79. free(TT.outfile);
  80. }
  81. xexit();
  82. }
  83. void split_main(void)
  84. {
  85. if (!TT.b && !TT.l && !TT.n) TT.l = 1000;
  86. // Allocate template for output filenames
  87. TT.outfile = xmprintf("%s%*c", (toys.optc == 2) ? toys.optargs[1] : "x",
  88. (int)TT.a, ' ');
  89. // We only ever use one input, but this handles '-' or no input for us.
  90. loopfiles(toys.optargs, do_split);
  91. }