split.c (f1d37c20c0a0bda9163303275492d834ee898333) | split.c (4185049582c7cea327a0571f8d3c2a3afb4a3e34) |
---|---|
1/* 2 * Copyright (c) 1987, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 53 unchanged lines hidden (view full) --- 62int bytecnt; /* Byte count to split on. */ 63long numlines; /* Line count to split on. */ 64int file_open; /* If a file open. */ 65int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 66char bfr[MAXBSIZE]; /* I/O buffer. */ 67char fname[MAXPATHLEN]; /* File name prefix. */ 68regex_t rgx; 69int pflag; | 1/* 2 * Copyright (c) 1987, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 53 unchanged lines hidden (view full) --- 62int bytecnt; /* Byte count to split on. */ 63long numlines; /* Line count to split on. */ 64int file_open; /* If a file open. */ 65int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 66char bfr[MAXBSIZE]; /* I/O buffer. */ 67char fname[MAXPATHLEN]; /* File name prefix. */ 68regex_t rgx; 69int pflag; |
70long sufflen = 2; /* File name suffix length. */ |
|
70 71void newfile __P((void)); 72void split1 __P((void)); 73void split2 __P((void)); 74static void usage __P((void)); 75 76int 77main(argc, argv) 78 int argc; 79 char *argv[]; 80{ 81 int ch; 82 char *ep, *p; 83 | 71 72void newfile __P((void)); 73void split1 __P((void)); 74void split2 __P((void)); 75static void usage __P((void)); 76 77int 78main(argc, argv) 79 int argc; 80 char *argv[]; 81{ 82 int ch; 83 char *ep, *p; 84 |
84 while ((ch = getopt(argc, argv, "-0123456789b:l:p:")) != -1) | 85 while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:")) != -1) |
85 switch (ch) { 86 case '0': case '1': case '2': case '3': case '4': 87 case '5': case '6': case '7': case '8': case '9': 88 /* 89 * Undocumented kludge: split was originally designed 90 * to take a number after a dash. 91 */ 92 if (numlines == 0) { --- 8 unchanged lines hidden (view full) --- 101 "%s: illegal line count", optarg); 102 } 103 break; 104 case '-': /* Undocumented: historic stdin flag. */ 105 if (ifd != -1) 106 usage(); 107 ifd = 0; 108 break; | 86 switch (ch) { 87 case '0': case '1': case '2': case '3': case '4': 88 case '5': case '6': case '7': case '8': case '9': 89 /* 90 * Undocumented kludge: split was originally designed 91 * to take a number after a dash. 92 */ 93 if (numlines == 0) { --- 8 unchanged lines hidden (view full) --- 102 "%s: illegal line count", optarg); 103 } 104 break; 105 case '-': /* Undocumented: historic stdin flag. */ 106 if (ifd != -1) 107 usage(); 108 ifd = 0; 109 break; |
110 case 'a': /* Suffix length */ 111 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep) 112 errx(EX_USAGE, 113 "%s: illegal suffix length", optarg); 114 break; |
|
109 case 'b': /* Byte count. */ 110 if ((bytecnt = strtoq(optarg, &ep, 10)) <= 0 || 111 (*ep != '\0' && *ep != 'k' && *ep != 'm')) 112 errx(EX_USAGE, 113 "%s: illegal byte count", optarg); 114 if (*ep == 'k') 115 bytecnt *= 1024; 116 else if (*ep == 'm') --- 23 unchanged lines hidden (view full) --- 140 err(EX_NOINPUT, "%s", *argv); 141 ++argv; 142 } 143 if (*argv != NULL) /* File name prefix. */ 144 (void)strcpy(fname, *argv++); 145 if (*argv != NULL) 146 usage(); 147 | 115 case 'b': /* Byte count. */ 116 if ((bytecnt = strtoq(optarg, &ep, 10)) <= 0 || 117 (*ep != '\0' && *ep != 'k' && *ep != 'm')) 118 errx(EX_USAGE, 119 "%s: illegal byte count", optarg); 120 if (*ep == 'k') 121 bytecnt *= 1024; 122 else if (*ep == 'm') --- 23 unchanged lines hidden (view full) --- 146 err(EX_NOINPUT, "%s", *argv); 147 ++argv; 148 } 149 if (*argv != NULL) /* File name prefix. */ 150 (void)strcpy(fname, *argv++); 151 if (*argv != NULL) 152 usage(); 153 |
154 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname)) 155 errx(EX_USAGE, "suffix is too long"); |
|
148 if (pflag && (numlines != 0 || bytecnt != 0)) 149 usage(); 150 151 if (numlines == 0) 152 numlines = DEFLINE; 153 else if (bytecnt != 0) 154 usage(); 155 --- 112 unchanged lines hidden (view full) --- 268 269/* 270 * newfile -- 271 * Open a new output file. 272 */ 273void 274newfile() 275{ | 156 if (pflag && (numlines != 0 || bytecnt != 0)) 157 usage(); 158 159 if (numlines == 0) 160 numlines = DEFLINE; 161 else if (bytecnt != 0) 162 usage(); 163 --- 112 unchanged lines hidden (view full) --- 276 277/* 278 * newfile -- 279 * Open a new output file. 280 */ 281void 282newfile() 283{ |
284 long i, maxfiles, tfnum; |
|
276 static long fnum; 277 static int defname; 278 static char *fpnt; 279 280 if (ofd == -1) { 281 if (fname[0] == '\0') { 282 fname[0] = 'x'; 283 fpnt = fname + 1; 284 defname = 1; 285 } else { 286 fpnt = fname + strlen(fname); 287 defname = 0; 288 } 289 ofd = fileno(stdout); 290 } | 285 static long fnum; 286 static int defname; 287 static char *fpnt; 288 289 if (ofd == -1) { 290 if (fname[0] == '\0') { 291 fname[0] = 'x'; 292 fpnt = fname + 1; 293 defname = 1; 294 } else { 295 fpnt = fname + strlen(fname); 296 defname = 0; 297 } 298 ofd = fileno(stdout); 299 } |
300 301 /* maxfiles = 26^sufflen, but don't use libm. */ 302 for (maxfiles = 1, i = 0; i < sufflen; i++) 303 if ((maxfiles *= 26) <= 0) 304 errx(EX_USAGE, "suffix is too long (max %ld)", i); 305 |
|
291 /* 292 * Hack to increase max files; original code wandered through | 306 /* 307 * Hack to increase max files; original code wandered through |
293 * magic characters. Maximum files is 3 * 26 * 26 == 2028 | 308 * magic characters. |
294 */ | 309 */ |
295#define MAXFILES 676 296 if (fnum == MAXFILES) { | 310 if (fnum == maxfiles) { |
297 if (!defname || fname[0] == 'z') 298 errx(EX_DATAERR, "too many files"); 299 ++fname[0]; 300 fnum = 0; 301 } | 311 if (!defname || fname[0] == 'z') 312 errx(EX_DATAERR, "too many files"); 313 ++fname[0]; 314 fnum = 0; 315 } |
302 fpnt[0] = fnum / 26 + 'a'; 303 fpnt[1] = fnum % 26 + 'a'; | 316 317 /* Generate suffix of sufflen letters */ 318 tfnum = fnum; 319 i = sufflen - 1; 320 do { 321 fpnt[i] = tfnum % 26 + 'a'; 322 tfnum /= 26; 323 } while (i-- > 0); 324 fpnt[sufflen] = '\0'; 325 |
304 ++fnum; 305 if (!freopen(fname, "w", stdout)) 306 err(EX_IOERR, "%s", fname); 307 file_open = 1; 308} 309 310static void 311usage() 312{ 313 (void)fprintf(stderr, | 326 ++fnum; 327 if (!freopen(fname, "w", stdout)) 328 err(EX_IOERR, "%s", fname); 329 file_open = 1; 330} 331 332static void 333usage() 334{ 335 (void)fprintf(stderr, |
314"usage: split [-b byte_count] [-l line_count] [-p pattern] [file [prefix]]\n"); | 336"usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n"); 337 (void)fprintf(stderr, 338" [file [prefix]]\n"); |
315 exit(EX_USAGE); 316} | 339 exit(EX_USAGE); 340} |