xref: /original-bsd/games/pig/pig.c (revision 0f81f0ee)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1992, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)pig.c	8.2 (Berkeley) 05/04/95";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 void pigout __P((char *, int));
27 void usage __P((void));
28 
29 int
30 main(argc, argv)
31 	int argc;
32 	char *argv[];
33 {
34 	register int len;
35 	int ch;
36 	char buf[1024];
37 
38 	while ((ch = getopt(argc, argv, "")) != EOF)
39 		switch(ch) {
40 		case '?':
41 		default:
42 			usage();
43 		}
44 	argc -= optind;
45 	argv += optind;
46 
47 	for (len = 0; (ch = getchar()) != EOF;) {
48 		if (isalpha(ch)) {
49 			if (len >= sizeof(buf)) {
50 				(void)fprintf(stderr, "pig: ate too much!\n");
51 				exit(1);
52 			}
53 			buf[len++] = ch;
54 			continue;
55 		}
56 		if (len != 0) {
57 			pigout(buf, len);
58 			len = 0;
59 		}
60 		(void)putchar(ch);
61 	}
62 	exit(0);
63 }
64 
65 void
66 pigout(buf, len)
67 	char *buf;
68 	int len;
69 {
70 	register int ch, start;
71 	int olen;
72 
73 	/*
74 	 * If the word starts with a vowel, append "way".  Don't treat 'y'
75 	 * as a vowel if it appears first.
76 	 */
77 	if (index("aeiouAEIOU", buf[0]) != NULL) {
78 		(void)printf("%.*sway", len, buf);
79 		return;
80 	}
81 
82 	/*
83 	 * Copy leading consonants to the end of the word.  The unit "qu"
84 	 * isn't treated as a vowel.
85 	 */
86 	for (start = 0, olen = len;
87 	    !index("aeiouyAEIOUY", buf[start]) && start < olen;) {
88 		ch = buf[len++] = buf[start++];
89 		if ((ch == 'q' || ch == 'Q') && start < olen &&
90 		    (buf[start] == 'u' || buf[start] == 'U'))
91 			buf[len++] = buf[start++];
92 	}
93 	(void)printf("%.*say", olen, buf + start);
94 }
95 
96 void
97 usage()
98 {
99 	(void)fprintf(stderr, "usage: pig\n");
100 	exit(1);
101 }
102