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