Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/tr Make tr -c work. Fixes PR 48113.
details: https://anonhg.NetBSD.org/src/rev/06830c98dc5f
branches: trunk
changeset: 789194:06830c98dc5f
user: dholland <dholland%NetBSD.org@localhost>
date: Sun Aug 11 00:28:46 2013 +0000
description:
Make tr -c work. Fixes PR 48113.
When -c is in effect, scan string1 first, complement it, and then
iterate over the results while scanning string2. Otherwise, scan
string1 and string2 together as before.
diffstat:
usr.bin/tr/tr.c | 42 +++++++++++++++++++++++++++++-------------
1 files changed, 29 insertions(+), 13 deletions(-)
diffs (90 lines):
diff -r 2113247a94e7 -r 06830c98dc5f usr.bin/tr/tr.c
--- a/usr.bin/tr/tr.c Sun Aug 11 00:12:47 2013 +0000
+++ b/usr.bin/tr/tr.c Sun Aug 11 00:28:46 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tr.c,v 1.14 2013/08/11 00:12:47 dholland Exp $ */
+/* $NetBSD: tr.c,v 1.15 2013/08/11 00:28:46 dholland Exp $ */
/*
* Copyright (c) 1988, 1993
@@ -39,7 +39,7 @@
#if 0
static char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95";
#endif
-__RCSID("$NetBSD: tr.c,v 1.14 2013/08/11 00:12:47 dholland Exp $");
+__RCSID("$NetBSD: tr.c,v 1.15 2013/08/11 00:28:46 dholland Exp $");
#endif /* not lint */
#include <sys/types.h>
@@ -93,7 +93,7 @@
int
main(int argc, char **argv)
{
- int ch, ch2, cnt, lastch, *p;
+ int ch, ch2, lastch;
int cflag, dflag, sflag, isstring2;
STR *s1, *s2;
@@ -198,18 +198,37 @@
if (!isstring2)
usage();
- str_setstring(s1, argv[0]);
+ if (cflag) {
+ setup(string1, argv[0], s1, cflag);
+ ch = -1;
+ } else {
+ str_setstring(s1, argv[0]);
+ }
str_setstring(s2, argv[1]);
- if (cflag)
- for (cnt = NCHARS, p = string1; cnt--;)
- *p++ = OOBCH;
-
if (!next(s2, &ch2))
errx(1, "empty string2");
/* If string2 runs out of characters, use the last one specified. */
- while (next(s1, &ch)) {
+ while (1) {
+ if (cflag) {
+ ch++;
+ while (ch < NCHARS && string1[ch] == 0) {
+ if (string1[ch] == 0) {
+ string1[ch] = ch;
+ }
+ ch++;
+ }
+ if (ch == NCHARS) {
+ break;
+ }
+ }
+ else {
+ if (!next(s1, &ch)) {
+ break;
+ }
+ }
+
string1[ch] = ch2;
if (sflag) {
string2[ch2] = 1;
@@ -217,10 +236,6 @@
(void)next(s2, &ch2);
}
- if (cflag)
- for (cnt = 0, p = string1; cnt < NCHARS; ++p, ++cnt)
- *p = *p == OOBCH ? ch2 : cnt;
-
if (sflag)
for (lastch = OOBCH; (ch = getchar()) != EOF;) {
ch = string1[ch];
@@ -232,6 +247,7 @@
else
while ((ch = getchar()) != EOF)
(void)putchar(string1[ch]);
+
str_destroy(s1);
str_destroy(s2);
exit (0);
Home |
Main Index |
Thread Index |
Old Index