Merge from vendor branch GCC:
[dragonfly.git] / games / number / number.c
1 /*
2  * Copyright (c) 1988, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)number.c 8.3 (Berkeley) 5/4/95
35  * $FreeBSD: src/games/number/number.c,v 1.12 1999/12/12 03:22:35 billf Exp $
36  * $DragonFly: src/games/number/number.c,v 1.4 2005/04/25 16:10:24 liamfoy Exp $
37  */
38
39 #include <sys/types.h>
40
41 #include <ctype.h>
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #define MAXNUM          65              /* Biggest number we handle. */
49
50 static const char       *name1[] = {
51         "",             "one",          "two",          "three",
52         "four",         "five",         "six",          "seven",
53         "eight",        "nine",         "ten",          "eleven",
54         "twelve",       "thirteen",     "fourteen",     "fifteen",
55         "sixteen",      "seventeen",    "eighteen",     "nineteen",
56 },
57                 *name2[] = {
58         "",             "ten",          "twenty",       "thirty",
59         "forty",        "fifty",        "sixty",        "seventy",
60         "eighty",       "ninety",
61 },
62                 *name3[] = {
63         "hundred",      "thousand",     "million",      "billion",
64         "trillion",     "quadrillion",  "quintillion",  "sextillion",
65         "septillion",   "octillion",    "nonillion",    "decillion",
66         "undecillion",  "duodecillion", "tredecillion", "quattuordecillion",
67         "quindecillion",                "sexdecillion",
68         "septendecillion",              "octodecillion",
69         "novemdecillion",               "vigintillion",
70 };
71
72 void    convert (char *);
73 int     number (char *, int);
74 void    pfract (int);
75 void    toobig (void);
76 int     unit (int, char *);
77 void    usage (void);
78
79 int lflag;
80
81 int
82 main(int argc, char **argv)
83 {
84         int ch, first;
85         char line[256];
86
87         lflag = 0;
88         while ((ch = getopt(argc, argv, "l")) != -1)
89                 switch (ch) {
90                 case 'l':
91                         lflag = 1;
92                         break;
93                 case '?':
94                 default:
95                         usage();
96                 }
97         argc -= optind;
98         argv += optind;
99
100         if (*argv == NULL)
101                 for (first = 1;
102                     fgets(line, sizeof(line), stdin) != NULL; first = 0) {
103                         if (strchr(line, '\n') == NULL)
104                                 errx(1, "line too long.");
105                         if (!first)
106                                 (void)printf("...\n");
107                         convert(line);
108                 }
109         else
110                 for (first = 1; *argv != NULL; first = 0, ++argv) {
111                         if (!first)
112                                 (void)printf("...\n");
113                         convert(*argv);
114                 }
115         exit(0);
116 }
117
118 void
119 convert(char *line)
120 {
121         int flen, len, rval;
122         char *p, *fraction;
123
124         flen = NULL;
125         fraction = NULL;
126         for (p = line; *p != '\0' && *p != '\n'; ++p) {
127                 if (isblank(*p)) {
128                         if (p == line) {
129                                 ++line;
130                                 continue;
131                         }
132                         goto badnum;
133                 }
134                 if (isdigit(*p))
135                         continue;
136                 switch (*p) {
137                 case '.':
138                         if (fraction != NULL)
139                                 goto badnum;
140                         fraction = p + 1;
141                         *p = '\0';
142                         break;
143                 case '-':
144                         if (p == line)
145                                 break;
146                         /* FALLTHROUGH */
147                 default:
148 badnum:                 errx(1, "illegal number: %s", line);
149                         break;
150                 }
151         }
152         *p = '\0';
153
154         if ((len = strlen(line)) > MAXNUM ||
155             (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
156                 errx(1, "number too large, max %d digits.", MAXNUM);
157
158         if (*line == '-') {
159                 (void)printf("minus%s", lflag ? " " : "\n");
160                 ++line;
161                 --len;
162         }
163
164         rval = len > 0 ? unit(len, line) : 0;
165         if (fraction != NULL && flen != 0)
166                 for (p = fraction; *p != '\0'; ++p)
167                         if (*p != '0') {
168                                 if (rval)
169                                         (void)printf("%sand%s",
170                                             lflag ? " " : "",
171                                             lflag ? " " : "\n");
172                                 if (unit(flen, fraction)) {
173                                         if (lflag)
174                                                 (void)printf(" ");
175                                         pfract(flen);
176                                         rval = 1;
177                                 }
178                                 break;
179                         }
180         if (!rval)
181                 (void)printf("zero%s", lflag ? "" : ".\n");
182         if (lflag)
183                 (void)printf("\n");
184 }
185
186 int
187 unit(int len, char *p)
188 {
189         int off, rval;
190
191         rval = 0;
192         if (len > 3) {
193                 if (len % 3) {
194                         off = len % 3;
195                         len -= off;
196                         if (number(p, off)) {
197                                 rval = 1;
198                                 (void)printf(" %s%s",
199                                     name3[len / 3], lflag ? " " : ".\n");
200                         }
201                         p += off;
202                 }
203                 for (; len > 3; p += 3) {
204                         len -= 3;
205                         if (number(p, 3)) {
206                                 rval = 1;
207                                 (void)printf(" %s%s",
208                                     name3[len / 3], lflag ? " " : ".\n");
209                         }
210                 }
211         }
212         if (number(p, len)) {
213                 if (!lflag)
214                         (void)printf(".\n");
215                 rval = 1;
216         }
217         return (rval);
218 }
219
220 int
221 number(char *p, int len)
222 {
223         int val, rval;
224
225         rval = 0;
226         switch (len) {
227         case 3:
228                 if (*p != '0') {
229                         rval = 1;
230                         (void)printf("%s hundred", name1[*p - '0']);
231                 }
232                 ++p;
233                 /* FALLTHROUGH */
234         case 2:
235                 val = (p[1] - '0') + (p[0] - '0') * 10;
236                 if (val) {
237                         if (rval)
238                                 (void)printf(" ");
239                         if (val < 20)
240                                 (void)printf("%s", name1[val]);
241                         else {
242                                 (void)printf("%s", name2[val / 10]);
243                                 if (val % 10)
244                                         (void)printf("-%s", name1[val % 10]);
245                         }
246                         rval = 1;
247                 }
248                 break;
249         case 1:
250                 if (*p != '0') {
251                         rval = 1;
252                         (void)printf("%s", name1[*p - '0']);
253                 }
254         }
255         return (rval);
256 }
257
258 void
259 pfract(int len)
260 {
261         static const char *pref[] = { "", "ten-", "hundred-" };
262
263         switch(len) {
264         case 1:
265                 (void)printf("tenths.\n");
266                 break;
267         case 2:
268                 (void)printf("hundredths.\n");
269                 break;
270         default:
271                 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
272                 break;
273         }
274 }
275
276 void
277 usage(void)
278 {
279         (void)fprintf(stderr, "usage: number [# ...]\n");
280         exit(1);
281 }