summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-07-29 23:42:00 +0000
committertjr <tjr@FreeBSD.org>2002-07-29 23:42:00 +0000
commit6d20181e60acb8a5d5183649e50030bb368ddbbd (patch)
tree0db642bd53cbf43d71e0154e5db12e794f073036 /usr.bin
parent478c0ba990961936269a6d44bccdbf9891a19977 (diff)
downloadFreeBSD-src-6d20181e60acb8a5d5183649e50030bb368ddbbd.zip
FreeBSD-src-6d20181e60acb8a5d5183649e50030bb368ddbbd.tar.gz
When translating and -C is specified, behave as if the complemented set was
in the locale collating order as required by SUSv3.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/tr/tr.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/usr.bin/tr/tr.c b/usr.bin/tr/tr.c
index ff623cd..1fb5fd2 100644
--- a/usr.bin/tr/tr.c
+++ b/usr.bin/tr/tr.c
@@ -101,6 +101,7 @@ static int string1[NCHARS] = {
STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
+static int charcoll(const void *, const void *);
static void setup(int *, char *, STR *, int, int);
static void usage(void);
@@ -109,6 +110,7 @@ main(argc, argv)
int argc;
char **argv;
{
+ static int collorder[NCHARS], tmpmap[NCHARS];
int ch, cnt, lastch, *p;
int Cflag, cflag, dflag, sflag, isstring2;
@@ -249,6 +251,19 @@ main(argc, argv)
*p = cnt;
}
}
+ if (Cflag) {
+ /*
+ * Generate a table for locale single-byte collating element
+ * ordering and use it to reorder string1 as required by
+ * IEEE Std. 1003.1-2001.
+ */
+ for (ch = 0; ch < NCHARS; ch++)
+ collorder[ch] = ch;
+ mergesort(collorder, NCHARS, sizeof(*collorder), charcoll);
+ for (ch = 0; ch < NCHARS; ch++)
+ tmpmap[ch] = string1[collorder[ch]];
+ memcpy(string1, tmpmap, sizeof(tmpmap));
+ }
if (sflag)
for (lastch = OOBCH; (ch = getchar()) != EOF;) {
@@ -286,6 +301,17 @@ setup(string, arg, str, cflag, Cflag)
string[cnt] = !string[cnt] && ISCHAR(cnt);
}
+static int
+charcoll(const void *a, const void *b)
+{
+ char sa[2], sb[2];
+
+ sa[0] = *(const int *)a;
+ sb[0] = *(const int *)b;
+ sa[1] = sb[1] = '\0';
+ return (strcoll(sa, sb));
+}
+
static void
usage()
{
OpenPOWER on IntegriCloud