*** split.c.orig Sun Jul 11 07:44:23 2004 --- split.c Sat Aug 4 23:01:08 2007 *************** *** 32,38 **** */ #include ! __FBSDID("$FreeBSD: src/usr.bin/split/split.c,v 1.15 2004/07/11 14:44:23 tjr Exp $"); #ifndef lint static const char copyright[] = --- 32,38 ---- */ #include ! __FBSDID("$FreeBSD: src/usr.bin/split/split.c,v 1.15+ 2004/07/11 14:44:23 tjr Exp $"); #ifndef lint static const char copyright[] = *************** *** 61,66 **** --- 61,69 ---- #include #include + #include + #include + #define DEFLINE 1000 /* Default num lines per file. */ off_t bytecnt; /* Byte count to split on. */ *************** *** 70,77 **** char bfr[MAXBSIZE]; /* I/O buffer. */ char fname[MAXPATHLEN]; /* File name prefix. */ regex_t rgx; ! int pflag; ! long sufflen = 2; /* File name suffix length. */ void newfile(void); void split1(void); --- 73,80 ---- char bfr[MAXBSIZE]; /* I/O buffer. */ char fname[MAXPATHLEN]; /* File name prefix. */ regex_t rgx; ! int pflag, Bflag; ! long sufflen = -2; /* File name suffix length, -2 => unspecified. */ void newfile(void); void split1(void); *************** *** 85,94 **** long scale; int ch; char *ep, *p; setlocale(LC_ALL, ""); ! while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': --- 88,98 ---- long scale; int ch; char *ep, *p; + struct stat istat; setlocale(LC_ALL, ""); ! while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:B:")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': *************** *** 114,123 **** --- 118,134 ---- ifd = 0; break; case 'a': /* Suffix length */ + if (Bflag) + errx(EX_USAGE, "-a is incompatible with -B"); if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep) errx(EX_USAGE, "%s: illegal suffix length", optarg); break; + case 'B': /* Byte count & compute sufflen. */ + if (sufflen != -2) + errx(EX_USAGE, "-B is incompatible with -a"); + Bflag = 1; + /* fall through */ case 'b': /* Byte count. */ errno = 0; if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 || *************** *** 153,164 **** --- 164,185 ---- argv += optind; argc -= optind; + if (sufflen == -2) + sufflen = 2; + if (*argv != NULL) if (ifd == -1) { /* Input file. */ if (strcmp(*argv, "-") == 0) ifd = STDIN_FILENO; else if ((ifd = open(*argv, O_RDONLY, 0)) < 0) err(EX_NOINPUT, "%s", *argv); + else if (Bflag && fstat(ifd, &istat) == 0 && + istat.st_size > 0) { + off_t nfiles = + (istat.st_size + bytecnt - 1) / bytecnt; + for (sufflen = 1; nfiles > 26; nfiles /= 26) + ++sufflen; + } ++argv; } if (*argv != NULL) /* File name prefix. */ *************** *** 349,355 **** usage(void) { (void)fprintf(stderr, ! "usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n"); (void)fprintf(stderr, " [file [prefix]]\n"); exit(EX_USAGE); --- 370,376 ---- usage(void) { (void)fprintf(stderr, ! "usage: split [-a sufflen] [-{b|B} byte_count] [-l line_count] [-p pattern]\n"); (void)fprintf(stderr, " [file [prefix]]\n"); exit(EX_USAGE); --- split.c.diff ends here ---