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 <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"
|
|
|
|
|
2015-02-01 13:00:47 +00:00
|
|
|
static int base = 26, start = 'a';
|
|
|
|
|
2015-03-07 12:33:39 +00:00
|
|
|
static int
|
2015-02-01 13:00:47 +00:00
|
|
|
itostr(char *str, int x, int n)
|
|
|
|
{
|
|
|
|
str[n] = '\0';
|
|
|
|
while (n-- > 0) {
|
|
|
|
str[n] = start + (x % base);
|
|
|
|
x /= base;
|
|
|
|
}
|
2015-03-17 21:59:48 +00:00
|
|
|
|
|
|
|
return x ? -1 : 0;
|
2015-02-01 13:00:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 12:33:39 +00:00
|
|
|
static FILE *
|
2015-02-01 13:00:47 +00:00
|
|
|
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);
|
2015-03-17 21:59:48 +00:00
|
|
|
|
2015-02-01 13:00:47 +00:00
|
|
|
return f;
|
|
|
|
}
|
2014-04-22 12:13:51 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-17 21:59:48 +00:00
|
|
|
eprintf("usage: %s [-a num] [-b num[k|m|g] | -l num] [-d] "
|
|
|
|
"[file [prefix]]\n", argv0);
|
2014-04-22 12:13:51 +00:00
|
|
|
}
|
2013-06-14 16:55:25 +00:00
|
|
|
|
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
|
|
|
{
|
2015-02-01 13:00:47 +00:00
|
|
|
FILE *in = stdin, *out = NULL;
|
2015-02-01 00:24:03 +00:00
|
|
|
size_t size = 1000, scale = 1, n;
|
2015-02-01 13:00:47 +00:00
|
|
|
long l;
|
2015-03-17 21:59:48 +00:00
|
|
|
int ch, plen, slen = 2, always = 0;
|
|
|
|
char name[NAME_MAX + 1], *prefix = "x", *file = NULL, *tmp, *end;
|
2013-06-14 16:55:25 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2015-02-01 13:00:47 +00:00
|
|
|
case 'a':
|
|
|
|
slen = estrtonum(EARGF(usage()), 0, INT_MAX);
|
|
|
|
break;
|
2013-06-14 16:55:25 +00:00
|
|
|
case 'b':
|
|
|
|
always = 1;
|
2015-02-01 13:00:47 +00:00
|
|
|
tmp = EARGF(usage());
|
|
|
|
l = strtol(tmp, &end, 10);
|
|
|
|
if (l <= 0)
|
|
|
|
eprintf("invalid number of bytes: %s\n", tmp);
|
|
|
|
size = (size_t)l;
|
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
|
|
|
}
|
2015-02-01 13:00:47 +00:00
|
|
|
if (size > (SIZE_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 'd':
|
|
|
|
base = 10;
|
|
|
|
start = '0';
|
|
|
|
break;
|
2015-02-01 13:00:47 +00:00
|
|
|
case 'l':
|
|
|
|
always = 0;
|
|
|
|
tmp = EARGF(usage());
|
|
|
|
size = estrtonum(tmp, 1, MIN(LLONG_MAX, SIZE_MAX));
|
|
|
|
break;
|
2013-06-14 16:55:25 +00:00
|
|
|
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);
|
2015-02-01 13:00:47 +00:00
|
|
|
if (plen + slen > NAME_MAX)
|
2014-01-30 13:51:47 +00:00
|
|
|
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
|
2015-03-17 10:24:49 +00:00
|
|
|
estrlcpy(name, prefix, sizeof(name));
|
2013-06-14 16:55:25 +00:00
|
|
|
|
2015-03-17 21:59:48 +00:00
|
|
|
if (file && strcmp(file, "-")) {
|
2015-02-01 13:00:47 +00:00
|
|
|
if (!(in = fopen(file, "r")))
|
2015-03-17 21:59:48 +00:00
|
|
|
eprintf("fopen %s:", file);
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 13:00:47 +00:00
|
|
|
n = 0;
|
|
|
|
while ((ch = getc(in)) != EOF) {
|
|
|
|
if (!out || n >= size) {
|
|
|
|
if (!(out = nextfile(out, name, plen, slen)))
|
|
|
|
eprintf("fopen: %s:", name);
|
|
|
|
n = 0;
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
2015-02-01 13:00:47 +00:00
|
|
|
n += (always || ch == '\n');
|
|
|
|
putc(ch, out);
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
2015-03-17 21:59:48 +00:00
|
|
|
|
|
|
|
fclose(in);
|
2015-02-01 13:00:47 +00:00
|
|
|
if (out)
|
|
|
|
fclose(out);
|
2015-03-17 21:59:48 +00:00
|
|
|
|
2013-06-14 16:55:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|