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