Hello, I suggest this simplification in the "tr [-Ccs] string1 string2" section of main. It makes it easier to understand what's happening in the -C case.
Luka diff --git a/usr.bin/tr/tr.c b/usr.bin/tr/tr.c index ab78898a986..a47fb409f34 100644 --- a/usr.bin/tr/tr.c +++ b/usr.bin/tr/tr.c @@ -167,8 +167,9 @@ main(int argc, char *argv[]) /* * tr [-Ccs] string1 string2 * Replace all characters (or complemented characters) in string1 with - * the character in the same position in string2. If the -s option is - * specified, squeeze all the characters in string2. + * the character in the same position in string2. If string2 runs out + * of characters (or with -Cc option), use the last one specified. If + * the -s option is specified, squeeze all the characters in string2. */ if (argc != 2) usage(); @@ -183,23 +184,21 @@ main(int argc, char *argv[]) if (!next(&s2)) errx(1, "empty string2"); - /* If string2 runs out of characters, use the last one specified. */ - ch = s2.lastch; if (sflag) while (next(&s1)) { - translate[s1.lastch] = ch = s2.lastch; - squeeze[ch] = 1; + translate[s1.lastch] = s2.lastch; + squeeze[s2.lastch] = 1; (void)next(&s2); } else while (next(&s1)) { - translate[s1.lastch] = ch = s2.lastch; + translate[s1.lastch] = s2.lastch; (void)next(&s2); } if (cflag) for (cnt = 0, p = translate; cnt < NCHARS; ++p, ++cnt) - *p = *p == OOBCH ? ch : cnt; + *p = *p == OOBCH ? s2.lastch : cnt; if (sflag) for (lastch = OOBCH; (ch = getchar()) != EOF;) {