Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/split/split.c
Context not available. | |||||
static off_t bytecnt; /* Byte count to split on. */ | static off_t bytecnt; /* Byte count to split on. */ | ||||
static off_t chunks = 0; /* Chunks count to split into. */ | static off_t chunks = 0; /* Chunks count to split into. */ | ||||
static bool clobber = true; /* Whether to overwrite existing output files. */ | |||||
static long numlines; /* Line count to split on. */ | static long numlines; /* Line count to split on. */ | ||||
static int file_open; /* If a file open. */ | static int file_open; /* If a file open. */ | ||||
static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ | static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ | ||||
Context not available. | |||||
setlocale(LC_ALL, ""); | setlocale(LC_ALL, ""); | ||||
dflag = false; | dflag = false; | ||||
while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1) | while ((ch = getopt(argc, argv, "0123456789a:b:cdl:n:p:")) != -1) | ||||
switch (ch) { | switch (ch) { | ||||
case '0': case '1': case '2': case '3': case '4': | case '0': case '1': case '2': case '3': case '4': | ||||
case '5': case '6': case '7': case '8': case '9': | case '5': case '6': case '7': case '8': case '9': | ||||
Context not available. | |||||
if (error == -1) | if (error == -1) | ||||
errx(EX_USAGE, "%s: offset too large", optarg); | errx(EX_USAGE, "%s: offset too large", optarg); | ||||
break; | break; | ||||
case 'c': /* Continue, don't overwrite output files. */ | |||||
clobber = false; | |||||
break; | |||||
case 'd': /* Decimal suffix */ | case 'd': /* Decimal suffix */ | ||||
dflag = true; | dflag = true; | ||||
break; | break; | ||||
Context not available. | |||||
static char *fpnt; | static char *fpnt; | ||||
char beg, end; | char beg, end; | ||||
int pattlen; | int pattlen; | ||||
int flags = O_WRONLY | O_CREAT | O_TRUNC; | |||||
christos: Minor nit: `{}` not really needed. | |||||
if (!clobber) | |||||
flags |= O_EXCL; | |||||
if (ofd == -1) { | if (ofd == -1) { | ||||
if (fname[0] == '\0') { | if (fname[0] == '\0') { | ||||
Context not available. | |||||
} else { | } else { | ||||
fpnt = fname + strlen(fname); | fpnt = fname + strlen(fname); | ||||
} | } | ||||
ofd = fileno(stdout); | } else if (close(ofd) != 0) | ||||
} | err(1, "%s", fname); | ||||
again: | |||||
if (dflag) { | if (dflag) { | ||||
beg = '0'; | beg = '0'; | ||||
end = '9'; | end = '9'; | ||||
Context not available. | |||||
fpnt[sufflen] = '\0'; | fpnt[sufflen] = '\0'; | ||||
++fnum; | ++fnum; | ||||
if (!freopen(fname, "w", stdout)) | if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) { | ||||
Not Done Inline ActionsAgain, {} not needed. christos: Again, `{}` not needed. | |||||
if (!clobber && errno == EEXIST) | |||||
goto again; | |||||
err(EX_IOERR, "%s", fname); | err(EX_IOERR, "%s", fname); | ||||
} | |||||
file_open = 1; | file_open = 1; | ||||
} | } | ||||
Context not available. | |||||
usage(void) | usage(void) | ||||
{ | { | ||||
(void)fprintf(stderr, | (void)fprintf(stderr, | ||||
"usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n" | "usage: split [-c] [-l line_count] [-a suffix_length] [file [prefix]]\n" | ||||
" split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" | " split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" | ||||
" split -n chunk_count [-a suffix_length] [file [prefix]]\n" | " split -n chunk_count [-a suffix_length] [file [prefix]]\n" | ||||
" split -p pattern [-a suffix_length] [file [prefix]]\n"); | " split -p pattern [-a suffix_length] [file [prefix]]\n"); | ||||
Context not available. |
Minor nit: {} not really needed.