pig.c 3.9 KB

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