split.c (c4f7198f47c15eece849d06e8fdd1fb46ed43bba) | split.c (ac17fc816e67a4e5e2e481b5001577a8d589f8b6) |
---|---|
1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1987, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 53 unchanged lines hidden (view full) --- 62#include <unistd.h> 63#include <regex.h> 64#include <sysexits.h> 65 66#define DEFLINE 1000 /* Default num lines per file. */ 67 68static off_t bytecnt; /* Byte count to split on. */ 69static off_t chunks = 0; /* Chunks count to split into. */ | 1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1987, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 53 unchanged lines hidden (view full) --- 62#include <unistd.h> 63#include <regex.h> 64#include <sysexits.h> 65 66#define DEFLINE 1000 /* Default num lines per file. */ 67 68static off_t bytecnt; /* Byte count to split on. */ 69static off_t chunks = 0; /* Chunks count to split into. */ |
70static bool clobber = true; /* Whether to overwrite existing output files. */ |
|
70static long numlines; /* Line count to split on. */ 71static int file_open; /* If a file open. */ 72static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 73static char fname[MAXPATHLEN]; /* File name prefix. */ 74static regex_t rgx; 75static int pflag; 76static bool dflag; 77static long sufflen = 2; /* File name suffix length. */ --- 10 unchanged lines hidden (view full) --- 88{ 89 int ch; 90 int error; 91 char *ep, *p; 92 93 setlocale(LC_ALL, ""); 94 95 dflag = false; | 71static long numlines; /* Line count to split on. */ 72static int file_open; /* If a file open. */ 73static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 74static char fname[MAXPATHLEN]; /* File name prefix. */ 75static regex_t rgx; 76static int pflag; 77static bool dflag; 78static long sufflen = 2; /* File name suffix length. */ --- 10 unchanged lines hidden (view full) --- 89{ 90 int ch; 91 int error; 92 char *ep, *p; 93 94 setlocale(LC_ALL, ""); 95 96 dflag = false; |
96 while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1) | 97 while ((ch = getopt(argc, argv, "0123456789a:b:cdl:n:p:")) != -1) |
97 switch (ch) { 98 case '0': case '1': case '2': case '3': case '4': 99 case '5': case '6': case '7': case '8': case '9': 100 /* 101 * Undocumented kludge: split was originally designed 102 * to take a number after a dash. 103 */ 104 if (numlines == 0) { --- 15 unchanged lines hidden (view full) --- 120 autosfx = 0; 121 break; 122 case 'b': /* Byte count. */ 123 errno = 0; 124 error = expand_number(optarg, &bytecnt); 125 if (error == -1) 126 errx(EX_USAGE, "%s: offset too large", optarg); 127 break; | 98 switch (ch) { 99 case '0': case '1': case '2': case '3': case '4': 100 case '5': case '6': case '7': case '8': case '9': 101 /* 102 * Undocumented kludge: split was originally designed 103 * to take a number after a dash. 104 */ 105 if (numlines == 0) { --- 15 unchanged lines hidden (view full) --- 121 autosfx = 0; 122 break; 123 case 'b': /* Byte count. */ 124 errno = 0; 125 error = expand_number(optarg, &bytecnt); 126 if (error == -1) 127 errx(EX_USAGE, "%s: offset too large", optarg); 128 break; |
129 case 'c': /* Continue, don't overwrite output files. */ 130 clobber = false; 131 break; |
|
128 case 'd': /* Decimal suffix */ 129 dflag = true; 130 break; 131 case 'l': /* Line count. */ 132 if (numlines != 0) 133 usage(); 134 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep) 135 errx(EX_USAGE, --- 206 unchanged lines hidden (view full) --- 342static void 343newfile(void) 344{ 345 long i, maxfiles, tfnum; 346 static long fnum; 347 static char *fpnt; 348 char beg, end; 349 int pattlen; | 132 case 'd': /* Decimal suffix */ 133 dflag = true; 134 break; 135 case 'l': /* Line count. */ 136 if (numlines != 0) 137 usage(); 138 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep) 139 errx(EX_USAGE, --- 206 unchanged lines hidden (view full) --- 346static void 347newfile(void) 348{ 349 long i, maxfiles, tfnum; 350 static long fnum; 351 static char *fpnt; 352 char beg, end; 353 int pattlen; |
354 int flags = O_WRONLY | O_CREAT | O_TRUNC; |
|
350 | 355 |
356 if (!clobber) 357 flags |= O_EXCL; 358 |
|
351 if (ofd == -1) { 352 if (fname[0] == '\0') { 353 fname[0] = 'x'; 354 fpnt = fname + 1; 355 } else { 356 fpnt = fname + strlen(fname); 357 } | 359 if (ofd == -1) { 360 if (fname[0] == '\0') { 361 fname[0] = 'x'; 362 fpnt = fname + 1; 363 } else { 364 fpnt = fname + strlen(fname); 365 } |
358 ofd = fileno(stdout); 359 } | 366 } else if (close(ofd) != 0) 367 err(1, "%s", fname); |
360 | 368 |
369 again: |
|
361 if (dflag) { 362 beg = '0'; 363 end = '9'; 364 } 365 else { 366 beg = 'a'; 367 end = 'z'; 368 } --- 43 unchanged lines hidden (view full) --- 412 i = sufflen - 1; 413 do { 414 fpnt[i] = tfnum % pattlen + beg; 415 tfnum /= pattlen; 416 } while (i-- > 0); 417 fpnt[sufflen] = '\0'; 418 419 ++fnum; | 370 if (dflag) { 371 beg = '0'; 372 end = '9'; 373 } 374 else { 375 beg = 'a'; 376 end = 'z'; 377 } --- 43 unchanged lines hidden (view full) --- 421 i = sufflen - 1; 422 do { 423 fpnt[i] = tfnum % pattlen + beg; 424 tfnum /= pattlen; 425 } while (i-- > 0); 426 fpnt[sufflen] = '\0'; 427 428 ++fnum; |
420 if (!freopen(fname, "w", stdout)) | 429 if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) { 430 if (!clobber && errno == EEXIST) 431 goto again; |
421 err(EX_IOERR, "%s", fname); | 432 err(EX_IOERR, "%s", fname); |
433 } |
|
422 file_open = 1; 423} 424 425static void 426usage(void) 427{ 428 (void)fprintf(stderr, | 434 file_open = 1; 435} 436 437static void 438usage(void) 439{ 440 (void)fprintf(stderr, |
429"usage: split [-d] [-l line_count] [-a suffix_length] [file [prefix]]\n" 430" split [-d] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" 431" split [-d] -n chunk_count [-a suffix_length] [file [prefix]]\n" 432" split [-d] -p pattern [-a suffix_length] [file [prefix]]\n"); | 441"usage: split [-cd] [-l line_count] [-a suffix_length] [file [prefix]]\n" 442" split [-cd] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" 443" split [-cd] -n chunk_count [-a suffix_length] [file [prefix]]\n" 444" split [-cd] -p pattern [-a suffix_length] [file [prefix]]\n"); |
433 exit(EX_USAGE); 434} | 445 exit(EX_USAGE); 446} |