1 /* $OpenBSD: pig.c,v 1.11 2009/10/27 23:59:26 deraadt Exp $ */ 2 /* $NetBSD: pig.c,v 1.2 1995/03/23 08:41:40 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 void pigout(char *, int); 43 void usage(void); 44 45 int 46 main(int argc, char *argv[]) 47 { 48 int len; 49 int ch; 50 char buf[1024]; 51 52 while ((ch = getopt(argc, argv, "")) != -1) 53 switch(ch) { 54 case '?': 55 default: 56 usage(); 57 } 58 argc -= optind; 59 argv += optind; 60 61 for (len = 0; (ch = getchar()) != EOF;) { 62 if (isalpha(ch)) { 63 if (len >= sizeof(buf)) 64 errx(1, "ate too much!"); 65 buf[len++] = ch; 66 continue; 67 } 68 if (len != 0) { 69 pigout(buf, len); 70 len = 0; 71 } 72 (void)putchar(ch); 73 } 74 exit(0); 75 } 76 77 void 78 pigout(char *buf, int len) 79 { 80 int ch, start, i; 81 int olen, allupper, firstupper; 82 83 /* See if the word is all upper case */ 84 allupper = firstupper = isupper(buf[0]); 85 for (i = 1; i < len && allupper; i++) 86 allupper = allupper && isupper(buf[i]); 87 88 /* 89 * If the word starts with a vowel, append "way". Don't treat 'y' 90 * as a vowel if it appears first. 91 */ 92 if (strchr("aeiouAEIOU", buf[0]) != NULL) { 93 (void)printf("%.*s%s", len, buf, 94 allupper ? "WAY" : "way"); 95 return; 96 } 97 98 /* 99 * Copy leading consonants to the end of the word. The unit "qu" 100 * isn't treated as a vowel. 101 */ 102 if (!allupper) 103 buf[0] = tolower(buf[0]); 104 for (start = 0, olen = len; 105 !strchr("aeiouyAEIOUY", buf[start]) && start < olen;) { 106 ch = buf[len++] = buf[start++]; 107 if ((ch == 'q' || ch == 'Q') && start < olen && 108 (buf[start] == 'u' || buf[start] == 'U')) 109 buf[len++] = buf[start++]; 110 } 111 if (firstupper) 112 buf[start] = toupper(buf[start]); 113 (void)printf("%.*s%s", olen, buf + start, allupper ? "AY" : "ay"); 114 } 115 116 void 117 usage(void) 118 { 119 (void)fprintf(stderr, "usage: pig\n"); 120 exit(1); 121 } 122