diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2007-09-05 21:07:54 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2007-09-05 21:07:54 +0000 |
commit | 9cf0d3cede315d0264ef0a34c7bff8cf2c301af1 (patch) | |
tree | 665ccff4a0285b1f7569f5ca1ba0ae8385e17ac3 /usr.bin/split/split.c | |
parent | 22558d4a6ecad2bbe96e8e6d9bae65b81dc90159 (diff) |
The rewritten newfile() algorithm introduced in rev 1.12 was
incorrect. This simpler version from Pete Honeyman passes all the
tests I thow at it. OK deraadt@ some time ago.
Diffstat (limited to 'usr.bin/split/split.c')
-rw-r--r-- | usr.bin/split/split.c | 50 |
1 files changed, 19 insertions, 31 deletions
diff --git a/usr.bin/split/split.c b/usr.bin/split/split.c index a6233400ecb..4f4a9653b13 100644 --- a/usr.bin/split/split.c +++ b/usr.bin/split/split.c @@ -1,4 +1,4 @@ -/* $OpenBSD: split.c,v 1.13 2006/08/10 22:44:17 millert Exp $ */ +/* $OpenBSD: split.c,v 1.14 2007/09/05 21:07:53 millert Exp $ */ /* $NetBSD: split.c,v 1.5 1995/08/31 22:22:05 jtc Exp $ */ /* @@ -40,7 +40,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)split.c 8.3 (Berkeley) 4/25/94"; #else -static char rcsid[] = "$OpenBSD: split.c,v 1.13 2006/08/10 22:44:17 millert Exp $"; +static char rcsid[] = "$OpenBSD: split.c,v 1.14 2007/09/05 21:07:53 millert Exp $"; #endif #endif /* not lint */ @@ -286,44 +286,32 @@ void newfile(void) { static char *suffix, *sufftail; - static int defname; + char *sptr; if (ofd == -1) { - if (fname[0] == '\0') { - fname[0] = 'x'; - suffix = fname + 1; - defname = 1; + ofd = fileno(stdout); + if (*fname == '\0') { + *fname = 'x'; /* no name specified, use 'x' */ + memset(fname + 1, 'a', sufflen); + suffix = fname; + sufflen++; /* treat 'x' as part of suffix */ } else { suffix = fname + strlen(fname); - defname = 0; + memset(suffix, 'a', sufflen); } - memset(suffix, 'a', sufflen); suffix[sufflen] = '\0'; sufftail = suffix + sufflen - 1; - --sufftail[0]; /* incremented later */ - ofd = fileno(stdout); - } - - if (sufftail[0] == 'z') { - int i; - - /* Increment the non-tail portion of the suffix. */ - for (i = sufflen - 2; i >= 0; i--) { - if (suffix[i] != 'z') { - suffix[i]++; + } else { + for (sptr = sufftail; sptr >= suffix; sptr--) { + if (*sptr != 'z') { + (*sptr)++; break; - } + } else + *sptr = 'a'; } - if (i < 0) { - /* Hack to support y and z prefix if no name spec'd. */ - if (!defname || fname[0] == 'z') - errx(EX_DATAERR, "too many files"); - ++fname[0]; - memset(suffix, 'a', sufflen); - } else - sufftail[0] = 'a'; /* reset tail */ - } else - ++sufftail[0]; + if (sptr < suffix) + errx(EX_DATAERR, "too many files"); + } if (!freopen(fname, "w", stdout)) err(EX_IOERR, "%s", fname); |