Lines 67-72
static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
Link Here
|
67 |
|
67 |
|
68 |
static off_t bytecnt; /* Byte count to split on. */ |
68 |
static off_t bytecnt; /* Byte count to split on. */ |
69 |
static off_t chunks = 0; /* Chunks count to split into. */ |
69 |
static off_t chunks = 0; /* Chunks count to split into. */ |
|
|
70 |
static bool clobber = true; /* Whether to overwrite existing output files. */ |
70 |
static long numlines; /* Line count to split on. */ |
71 |
static long numlines; /* Line count to split on. */ |
71 |
static int file_open; /* If a file open. */ |
72 |
static int file_open; /* If a file open. */ |
72 |
static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ |
73 |
static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ |
Lines 92-98
main(int argc, char **argv)
Link Here
|
92 |
setlocale(LC_ALL, ""); |
93 |
setlocale(LC_ALL, ""); |
93 |
|
94 |
|
94 |
dflag = false; |
95 |
dflag = false; |
95 |
while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1) |
96 |
while ((ch = getopt(argc, argv, "0123456789a:b:cdl:n:p:")) != -1) |
96 |
switch (ch) { |
97 |
switch (ch) { |
97 |
case '0': case '1': case '2': case '3': case '4': |
98 |
case '0': case '1': case '2': case '3': case '4': |
98 |
case '5': case '6': case '7': case '8': case '9': |
99 |
case '5': case '6': case '7': case '8': case '9': |
Lines 123-128
main(int argc, char **argv)
Link Here
|
123 |
if (error == -1) |
124 |
if (error == -1) |
124 |
errx(EX_USAGE, "%s: offset too large", optarg); |
125 |
errx(EX_USAGE, "%s: offset too large", optarg); |
125 |
break; |
126 |
break; |
|
|
127 |
case 'c': /* Continue, don't overwrite output files. */ |
128 |
clobber = false; |
129 |
break; |
126 |
case 'd': /* Decimal suffix */ |
130 |
case 'd': /* Decimal suffix */ |
127 |
dflag = true; |
131 |
dflag = true; |
128 |
break; |
132 |
break; |
Lines 345-350
newfile(void)
Link Here
|
345 |
static char *fpnt; |
349 |
static char *fpnt; |
346 |
char beg, end; |
350 |
char beg, end; |
347 |
int pattlen; |
351 |
int pattlen; |
|
|
352 |
int flags = O_WRONLY | O_CREAT | O_TRUNC; |
353 |
|
354 |
if (!clobber) { |
355 |
flags |= O_EXCL; |
356 |
} |
348 |
|
357 |
|
349 |
if (ofd == -1) { |
358 |
if (ofd == -1) { |
350 |
if (fname[0] == '\0') { |
359 |
if (fname[0] == '\0') { |
Lines 353-361
newfile(void)
Link Here
|
353 |
} else { |
362 |
} else { |
354 |
fpnt = fname + strlen(fname); |
363 |
fpnt = fname + strlen(fname); |
355 |
} |
364 |
} |
356 |
ofd = fileno(stdout); |
365 |
} else if (close(ofd) != 0) |
357 |
} |
366 |
err(1, "%s", fname); |
358 |
|
367 |
|
|
|
368 |
again: |
359 |
if (dflag) { |
369 |
if (dflag) { |
360 |
beg = '0'; |
370 |
beg = '0'; |
361 |
end = '9'; |
371 |
end = '9'; |
Lines 386-393
newfile(void)
Link Here
|
386 |
fpnt[sufflen] = '\0'; |
396 |
fpnt[sufflen] = '\0'; |
387 |
|
397 |
|
388 |
++fnum; |
398 |
++fnum; |
389 |
if (!freopen(fname, "w", stdout)) |
399 |
if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) { |
|
|
400 |
if (!clobber && errno == EEXIST) { |
401 |
goto again; |
402 |
} |
390 |
err(EX_IOERR, "%s", fname); |
403 |
err(EX_IOERR, "%s", fname); |
|
|
404 |
} |
391 |
file_open = 1; |
405 |
file_open = 1; |
392 |
} |
406 |
} |
393 |
|
407 |
|
Lines 395-401
static void
Link Here
|
395 |
usage(void) |
409 |
usage(void) |
396 |
{ |
410 |
{ |
397 |
(void)fprintf(stderr, |
411 |
(void)fprintf(stderr, |
398 |
"usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n" |
412 |
"usage: split [-c] [-l line_count] [-a suffix_length] [file [prefix]]\n" |
399 |
" split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" |
413 |
" split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" |
400 |
" split -n chunk_count [-a suffix_length] [file [prefix]]\n" |
414 |
" split -n chunk_count [-a suffix_length] [file [prefix]]\n" |
401 |
" split -p pattern [-a suffix_length] [file [prefix]]\n"); |
415 |
" split -p pattern [-a suffix_length] [file [prefix]]\n"); |