Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / translit.c
blob79f6992d83c618463ec2c5be514870a04370a932
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* translit 3
6 /* SUMMARY
7 /* transliterate characters
8 /* SYNOPSIS
9 /* #include <stringops.h>
11 /* char *translit(buf, original, replacement)
12 /* char *buf;
13 /* char *original;
14 /* char *replacement;
15 /* DESCRIPTION
16 /* translit() takes a null-terminated string, and replaces characters
17 /* given in its \fIoriginal\fR argument by the corresponding characters
18 /* in the \fIreplacement\fR string. The result value is the \fIbuf\fR
19 /* argument.
20 /* BUGS
21 /* Cannot replace null characters.
22 /* LICENSE
23 /* .ad
24 /* .fi
25 /* The Secure Mailer license must be distributed with this software.
26 /* AUTHOR(S)
27 /* Wietse Venema
28 /* IBM T.J. Watson Research
29 /* P.O. Box 704
30 /* Yorktown Heights, NY 10598, USA
31 /*--*/
33 /* System library. */
35 #include "sys_defs.h"
36 #include <string.h>
38 /* Utility library. */
40 #include "stringops.h"
42 char *translit(char *string, const char *original, const char *replacement)
44 char *cp;
45 const char *op;
48 * For large inputs, should use a lookup table.
50 for (cp = string; *cp != 0; cp++) {
51 for (op = original; *op != 0; op++) {
52 if (*cp == *op) {
53 *cp = replacement[op - original];
54 break;
58 return (string);
61 #ifdef TEST
64 * Usage: translit string1 string2
66 * test program to perform the most basic operation of the UNIX tr command.
68 #include <msg.h>
69 #include <vstring.h>
70 #include <vstream.h>
71 #include <vstring_vstream.h>
73 #define STR vstring_str
75 int main(int argc, char **argv)
77 VSTRING *buf = vstring_alloc(100);
79 if (argc != 3)
80 msg_fatal("usage: %s string1 string2", argv[0]);
81 while (vstring_fgets(buf, VSTREAM_IN))
82 vstream_fputs(translit(STR(buf), argv[1], argv[2]), VSTREAM_OUT);
83 vstream_fflush(VSTREAM_OUT);
84 vstring_free(buf);
85 return (0);
88 #endif