games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.git] / games / number / number.c
blob629aed155fff4fcfd516af9daf2944412e2465b7
1 /*-
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)number.c 8.3 (Berkeley) 5/4/95
31 * $FreeBSD: src/games/number/number.c,v 1.12 1999/12/12 03:22:35 billf Exp $
32 * $DragonFly: src/games/number/number.c,v 1.5 2008/06/05 18:06:30 swildner Exp $
35 #include <sys/types.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #define MAXNUM 65 /* Biggest number we handle. */
46 static const char *name1[] = {
47 "", "one", "two", "three",
48 "four", "five", "six", "seven",
49 "eight", "nine", "ten", "eleven",
50 "twelve", "thirteen", "fourteen", "fifteen",
51 "sixteen", "seventeen", "eighteen", "nineteen",
53 *name2[] = {
54 "", "ten", "twenty", "thirty",
55 "forty", "fifty", "sixty", "seventy",
56 "eighty", "ninety",
58 *name3[] = {
59 "hundred", "thousand", "million", "billion",
60 "trillion", "quadrillion", "quintillion", "sextillion",
61 "septillion", "octillion", "nonillion", "decillion",
62 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
63 "quindecillion", "sexdecillion",
64 "septendecillion", "octodecillion",
65 "novemdecillion", "vigintillion",
68 void convert (char *);
69 int number (char *, int);
70 void pfract (int);
71 void toobig (void);
72 int unit (int, char *);
73 void usage (void);
75 int lflag;
77 int
78 main(int argc, char **argv)
80 int ch, first;
81 char line[256];
83 lflag = 0;
84 while ((ch = getopt(argc, argv, "l")) != -1)
85 switch (ch) {
86 case 'l':
87 lflag = 1;
88 break;
89 case '?':
90 default:
91 usage();
93 argc -= optind;
94 argv += optind;
96 if (*argv == NULL)
97 for (first = 1;
98 fgets(line, sizeof(line), stdin) != NULL; first = 0) {
99 if (strchr(line, '\n') == NULL)
100 errx(1, "line too long.");
101 if (!first)
102 (void)printf("...\n");
103 convert(line);
105 else
106 for (first = 1; *argv != NULL; first = 0, ++argv) {
107 if (!first)
108 (void)printf("...\n");
109 convert(*argv);
111 exit(0);
114 void
115 convert(char *line)
117 int flen, len, rval;
118 char *p, *fraction;
120 flen = 0;
121 fraction = NULL;
122 for (p = line; *p != '\0' && *p != '\n'; ++p) {
123 if (isblank(*p)) {
124 if (p == line) {
125 ++line;
126 continue;
128 goto badnum;
130 if (isdigit(*p))
131 continue;
132 switch (*p) {
133 case '.':
134 if (fraction != NULL)
135 goto badnum;
136 fraction = p + 1;
137 *p = '\0';
138 break;
139 case '-':
140 if (p == line)
141 break;
142 /* FALLTHROUGH */
143 default:
144 badnum: errx(1, "illegal number: %s", line);
145 break;
148 *p = '\0';
150 if ((len = strlen(line)) > MAXNUM ||
151 (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
152 errx(1, "number too large, max %d digits.", MAXNUM);
154 if (*line == '-') {
155 (void)printf("minus%s", lflag ? " " : "\n");
156 ++line;
157 --len;
160 rval = len > 0 ? unit(len, line) : 0;
161 if (fraction != NULL && flen != 0)
162 for (p = fraction; *p != '\0'; ++p)
163 if (*p != '0') {
164 if (rval)
165 (void)printf("%sand%s",
166 lflag ? " " : "",
167 lflag ? " " : "\n");
168 if (unit(flen, fraction)) {
169 if (lflag)
170 (void)printf(" ");
171 pfract(flen);
172 rval = 1;
174 break;
176 if (!rval)
177 (void)printf("zero%s", lflag ? "" : ".\n");
178 if (lflag)
179 (void)printf("\n");
183 unit(int len, char *p)
185 int off, rval;
187 rval = 0;
188 if (len > 3) {
189 if (len % 3) {
190 off = len % 3;
191 len -= off;
192 if (number(p, off)) {
193 rval = 1;
194 (void)printf(" %s%s",
195 name3[len / 3], lflag ? " " : ".\n");
197 p += off;
199 for (; len > 3; p += 3) {
200 len -= 3;
201 if (number(p, 3)) {
202 rval = 1;
203 (void)printf(" %s%s",
204 name3[len / 3], lflag ? " " : ".\n");
208 if (number(p, len)) {
209 if (!lflag)
210 (void)printf(".\n");
211 rval = 1;
213 return (rval);
217 number(char *p, int len)
219 int val, rval;
221 rval = 0;
222 switch (len) {
223 case 3:
224 if (*p != '0') {
225 rval = 1;
226 (void)printf("%s hundred", name1[*p - '0']);
228 ++p;
229 /* FALLTHROUGH */
230 case 2:
231 val = (p[1] - '0') + (p[0] - '0') * 10;
232 if (val) {
233 if (rval)
234 (void)printf(" ");
235 if (val < 20)
236 (void)printf("%s", name1[val]);
237 else {
238 (void)printf("%s", name2[val / 10]);
239 if (val % 10)
240 (void)printf("-%s", name1[val % 10]);
242 rval = 1;
244 break;
245 case 1:
246 if (*p != '0') {
247 rval = 1;
248 (void)printf("%s", name1[*p - '0']);
251 return (rval);
254 void
255 pfract(int len)
257 static const char *pref[] = { "", "ten-", "hundred-" };
259 switch(len) {
260 case 1:
261 (void)printf("tenths.\n");
262 break;
263 case 2:
264 (void)printf("hundredths.\n");
265 break;
266 default:
267 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
268 break;
272 void
273 usage(void)
275 (void)fprintf(stderr, "usage: number [# ...]\n");
276 exit(1);