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.
This commit is contained in:
Hiltjo Posthuma 2015-02-01 14:00:47 +01:00
parent 5552db75ba
commit 949f930280
2 changed files with 100 additions and 106 deletions

2
README
View File

@ -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

120
split.c
View File

@ -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;
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)) {
@ -59,19 +87,15 @@ main(int argc, char *argv[])
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;
@ -89,53 +113,23 @@ main(int argc, char *argv[])
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);
if (!out || n >= size) {
if (!(out = nextfile(out, name, plen, slen)))
eprintf("fopen: %s:", name);
n = 0;
}
n += (always || ch == '\n');
if (n >= size)
goto Nextfile;
putc(ch, out);
}
if (in != stdin)
fclose(in);
if (out)
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;
}
if (x)
return -1;
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;
}