2013-10-07 16:03:26 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
2013-06-14 16:55:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-06-14 16:55:25 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int itostr(char *, int, int);
|
|
|
|
static FILE *nextfile(FILE *, char *, int, int);
|
2014-04-22 12:13:51 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: split [-d] [-a len] [-b [bytes[k|m|g]]] [-l [lines]] [input [prefix]]\n");
|
|
|
|
}
|
2013-06-14 16:55:25 +00:00
|
|
|
|
|
|
|
static int base = 26, start = 'a';
|
|
|
|
|
2013-07-20 05:27:42 +00:00
|
|
|
int
|
2014-04-18 10:51:18 +00:00
|
|
|
main(int argc, char *argv[])
|
2013-06-14 16:55:25 +00:00
|
|
|
{
|
|
|
|
int plen, slen = 2;
|
|
|
|
int ch;
|
|
|
|
char name[NAME_MAX+1];
|
|
|
|
char *prefix = "x";
|
|
|
|
char *file = NULL;
|
|
|
|
char *tmp, *end;
|
2014-05-12 11:01:47 +00:00
|
|
|
uint64_t size = 1000, scale = 1, n;
|
2013-06-14 16:55:25 +00:00
|
|
|
int always = 0;
|
2014-05-12 11:01:47 +00:00
|
|
|
FILE *in = stdin, *out = NULL;
|
2013-06-14 16:55:25 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'b':
|
|
|
|
always = 1;
|
|
|
|
tmp = ARGF();
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!tmp)
|
2013-06-14 16:55:25 +00:00
|
|
|
break;
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2013-06-14 16:55:25 +00:00
|
|
|
size = strtoull(tmp, &end, 10);
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!*end)
|
2013-06-14 16:55:25 +00:00
|
|
|
break;
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (toupper((int)*end)) {
|
2014-04-01 13:30:17 +00:00
|
|
|
case 'K':
|
|
|
|
scale = 1024;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
scale = 1024L * 1024L;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
scale = 1024L * 1024L * 1024L;
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-14 16:55:25 +00:00
|
|
|
usage();
|
2014-04-01 13:30:17 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (size > (UINT64_MAX/scale))
|
2014-01-30 13:51:47 +00:00
|
|
|
eprintf("'%s': out of range\n", tmp);
|
2013-06-14 16:55:25 +00:00
|
|
|
size *= scale;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
always = 0;
|
|
|
|
tmp = ARGF();
|
2014-11-13 17:29:30 +00:00
|
|
|
if (tmp)
|
2015-01-30 15:52:44 +00:00
|
|
|
size = estrtonum(tmp, 0, LLONG_MAX);
|
2013-06-14 16:55:25 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2015-01-30 15:52:44 +00:00
|
|
|
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
|
2013-06-14 16:55:25 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
base = 10;
|
|
|
|
start = '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*argv)
|
2013-06-14 16:55:25 +00:00
|
|
|
file = *argv++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*argv)
|
2013-06-14 16:55:25 +00:00
|
|
|
prefix = *argv++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*argv)
|
2013-06-14 16:55:25 +00:00
|
|
|
usage();
|
|
|
|
|
|
|
|
plen = strlen(prefix);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (plen+slen > NAME_MAX)
|
2014-01-30 13:51:47 +00:00
|
|
|
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
|
2014-03-23 18:33:06 +00:00
|
|
|
strlcpy(name, prefix, sizeof(name));
|
2013-06-14 16:55:25 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (file && strcmp(file, "-") != 0) {
|
2013-06-14 16:55:25 +00:00
|
|
|
in = fopen(file, "r");
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!in)
|
2014-01-30 13:51:47 +00:00
|
|
|
eprintf("'%s':", file);
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Nextfile:
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((out = nextfile(out, name, plen, slen))) {
|
2013-06-14 16:55:25 +00:00
|
|
|
n = 0;
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((ch = getc(in)) != EOF) {
|
2013-06-14 16:55:25 +00:00
|
|
|
putc(ch, out);
|
|
|
|
n += (always || ch == '\n');
|
2014-11-13 17:29:30 +00:00
|
|
|
if (n >= size)
|
2013-06-14 16:55:25 +00:00
|
|
|
goto Nextfile;
|
|
|
|
}
|
|
|
|
fclose(out);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
itostr(char *str, int x, int n)
|
|
|
|
{
|
|
|
|
str[n] = '\0';
|
2014-11-13 17:29:30 +00:00
|
|
|
while (n-- > 0) {
|
2013-06-14 16:55:25 +00:00
|
|
|
str[n] = start + (x % base);
|
|
|
|
x /= base;
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (x)
|
2013-06-14 16:55:25 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
nextfile(FILE *f, char *buf, int plen, int slen)
|
|
|
|
{
|
|
|
|
static int n = 0;
|
|
|
|
int s;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (f)
|
2013-06-14 16:55:25 +00:00
|
|
|
fclose(f);
|
|
|
|
s = itostr(buf+plen, n++, slen);
|
2014-11-19 19:59:37 +00:00
|
|
|
if (s < 0)
|
2013-06-14 16:55:25 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
f = fopen(buf, "w");
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!f)
|
2014-01-30 13:51:47 +00:00
|
|
|
eprintf("'%s':", buf);
|
2013-06-14 16:55:25 +00:00
|
|
|
return f;
|
|
|
|
}
|