commit 949f9302806590ab65f6e335be1bf22bd95d2327
parent 5552db75baf06b844f874225015dc334980f35d8
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 1 Feb 2015 14:00:47 +0100
split: fixes
- -b argument required, don't allow -b <= 0.
- -l argument required, don't allow -l <= 0.
- usage() and argument parsing: sort flags.
- don't write 0 size file, get rid of goto reproducable with:
printf 'a' | split -b 1.
- close FILE *in if != stdin.
- mark split as finished in README.
- codestyle fixes.
Diffstat:
M | README | | | 2 | +- |
M | split.c | | | 132 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
2 files changed, 64 insertions(+), 70 deletions(-)
diff --git a/README b/README
@@ -60,7 +60,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* setsid non-posix none
=* sleep yes none
sort no -m, -o, -d, -f, -i
- split yes none
+=* split yes none
= sponge non-posix none
strings no -a, -n, -t
= sync non-posix none
diff --git a/split.c b/split.c
@@ -8,38 +8,66 @@
#include "util.h"
-static int itostr(char *, int, int);
-static FILE *nextfile(FILE *, char *, int, int);
+static int base = 26, start = 'a';
+
+int
+itostr(char *str, int x, int n)
+{
+ str[n] = '\0';
+ while (n-- > 0) {
+ str[n] = start + (x % base);
+ x /= base;
+ }
+ if (x)
+ return -1;
+ return 0;
+}
+
+FILE *
+nextfile(FILE *f, char *buf, int plen, int slen)
+{
+ static int filecount = 0;
+
+ if (f)
+ fclose(f);
+ if (itostr(buf + plen, filecount++, slen) < 0)
+ return NULL;
+
+ if (!(f = fopen(buf, "w")))
+ eprintf("'%s':", buf);
+ return f;
+}
static void
usage(void)
{
- eprintf("usage: split [-d] [-a len] [-b [bytes[k|m|g]]] [-l [lines]] [input [prefix]]\n");
+ eprintf("usage: %s [-a len] [-b bytes[k|m|g]] [-d] [-l lines] "
+ "[input [prefix]]\n", argv0);
}
-static int base = 26, start = 'a';
-
int
main(int argc, char *argv[])
{
- int plen, slen = 2;
- int ch;
- char name[NAME_MAX+1];
+ FILE *in = stdin, *out = NULL;
+ char name[NAME_MAX + 1];
char *prefix = "x";
char *file = NULL;
char *tmp, *end;
size_t size = 1000, scale = 1, n;
- int always = 0;
- FILE *in = stdin, *out = NULL;
+ int ch, plen, slen = 2, always = 0;
+ long l;
ARGBEGIN {
+ case 'a':
+ slen = estrtonum(EARGF(usage()), 0, INT_MAX);
+ break;
case 'b':
always = 1;
- tmp = ARGF();
- if (!tmp)
- break;
-
- size = strtoull(tmp, &end, 10);
+ tmp = EARGF(usage());
+ l = strtol(tmp, &end, 10);
+ if (l <= 0)
+ eprintf("invalid number of bytes: %s\n", tmp);
+ size = (size_t)l;
if (!*end)
break;
switch (toupper((int)*end)) {
@@ -55,23 +83,19 @@ main(int argc, char *argv[])
default:
usage();
}
- if (size > (SIZE_MAX/scale))
+ if (size > (SIZE_MAX / scale))
eprintf("'%s': out of range\n", tmp);
size *= scale;
break;
- case 'l':
- always = 0;
- tmp = ARGF();
- if (tmp)
- size = estrtonum(tmp, 0, MIN(LLONG_MAX, SIZE_MAX));
- break;
- case 'a':
- slen = estrtonum(EARGF(usage()), 0, INT_MAX);
- break;
case 'd':
base = 10;
start = '0';
break;
+ case 'l':
+ always = 0;
+ tmp = EARGF(usage());
+ size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX));
+ break;
default:
usage();
} ARGEND;
@@ -84,58 +108,28 @@ main(int argc, char *argv[])
usage();
plen = strlen(prefix);
- if (plen+slen > NAME_MAX)
+ if (plen + slen > NAME_MAX)
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
strlcpy(name, prefix, sizeof(name));
if (file && strcmp(file, "-") != 0) {
- in = fopen(file, "r");
- if (!in)
+ if (!(in = fopen(file, "r")))
eprintf("'%s':", file);
}
-Nextfile:
- while ((out = nextfile(out, name, plen, slen))) {
- n = 0;
- while ((ch = getc(in)) != EOF) {
- putc(ch, out);
- n += (always || ch == '\n');
- if (n >= size)
- goto Nextfile;
+ n = 0;
+ while ((ch = getc(in)) != EOF) {
+ if (!out || n >= size) {
+ if (!(out = nextfile(out, name, plen, slen)))
+ eprintf("fopen: %s:", name);
+ n = 0;
}
- fclose(out);
- break;
- }
- return 0;
-}
-
-int
-itostr(char *str, int x, int n)
-{
- str[n] = '\0';
- while (n-- > 0) {
- str[n] = start + (x % base);
- x /= base;
+ n += (always || ch == '\n');
+ putc(ch, out);
}
- if (x)
- return -1;
+ if (in != stdin)
+ fclose(in);
+ if (out)
+ fclose(out);
return 0;
}
-
-FILE *
-nextfile(FILE *f, char *buf, int plen, int slen)
-{
- static int n = 0;
- int s;
-
- if (f)
- fclose(f);
- s = itostr(buf+plen, n++, slen);
- if (s < 0)
- return NULL;
-
- f = fopen(buf, "w");
- if (!f)
- eprintf("'%s':", buf);
- return f;
-}