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. 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. 16 * 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. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1988, 1993, 1994\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95"; 39 #endif 40 #endif /* not lint */ 41 42 #include <sys/types.h> 43 44 #include <ctype.h> 45 #include <err.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #define MAXNUM 65 /* Biggest number we handle. */ 52 53 static const char *name1[] = { 54 "", "one", "two", "three", 55 "four", "five", "six", "seven", 56 "eight", "nine", "ten", "eleven", 57 "twelve", "thirteen", "fourteen", "fifteen", 58 "sixteen", "seventeen", "eighteen", "nineteen", 59 }, 60 *name2[] = { 61 "", "ten", "twenty", "thirty", 62 "forty", "fifty", "sixty", "seventy", 63 "eighty", "ninety", 64 }, 65 *name3[] = { 66 "hundred", "thousand", "million", "billion", 67 "trillion", "quadrillion", "quintillion", "sextillion", 68 "septillion", "octillion", "nonillion", "decillion", 69 "undecillion", "duodecillion", "tredecillion", "quattuordecillion", 70 "quindecillion", "sexdecillion", 71 "septendecillion", "octodecillion", 72 "novemdecillion", "vigintillion", 73 }; 74 75 static void convert(char *); 76 static int number(char *, int); 77 static void pfract(int); 78 static int unit(int, char *); 79 static void usage(void); 80 81 static int lflag; 82 83 int 84 main(int argc, char *argv[]) 85 { 86 int ch, first; 87 char line[256]; 88 89 lflag = 0; 90 while ((ch = getopt(argc, argv, "l")) != -1) 91 switch (ch) { 92 case 'l': 93 lflag = 1; 94 break; 95 case '?': 96 default: 97 usage(); 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (*argv == NULL) 103 for (first = 1; 104 fgets(line, sizeof(line), stdin) != NULL; first = 0) { 105 if (strchr(line, '\n') == NULL) 106 errx(1, "line too long."); 107 if (!first) 108 (void)printf("...\n"); 109 convert(line); 110 } 111 else 112 for (first = 1; *argv != NULL; first = 0, ++argv) { 113 if (!first) 114 (void)printf("...\n"); 115 convert(*argv); 116 } 117 exit(0); 118 } 119 120 static void 121 convert(char *line) 122 { 123 int flen, len, rval; 124 char *p, *fraction; 125 126 flen = 0; 127 fraction = NULL; 128 for (p = line; *p != '\0' && *p != '\n'; ++p) { 129 if (isblank(*p)) { 130 if (p == line) { 131 ++line; 132 continue; 133 } 134 goto badnum; 135 } 136 if (isdigit(*p)) 137 continue; 138 switch (*p) { 139 case '.': 140 if (fraction != NULL) 141 goto badnum; 142 fraction = p + 1; 143 *p = '\0'; 144 break; 145 case '-': 146 if (p == line) 147 break; 148 /* FALLTHROUGH */ 149 default: 150 badnum: errx(1, "illegal number: %s", line); 151 break; 152 } 153 } 154 *p = '\0'; 155 156 if ((len = strlen(line)) > MAXNUM || 157 (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM))) 158 errx(1, "number too large, max %d digits.", MAXNUM); 159 160 if (*line == '-') { 161 (void)printf("minus%s", lflag ? " " : "\n"); 162 ++line; 163 --len; 164 } 165 166 rval = len > 0 ? unit(len, line) : 0; 167 if (fraction != NULL && flen != 0) 168 for (p = fraction; *p != '\0'; ++p) 169 if (*p != '0') { 170 if (rval) 171 (void)printf("%sand%s", 172 lflag ? " " : "", 173 lflag ? " " : "\n"); 174 if (unit(flen, fraction)) { 175 if (lflag) 176 (void)printf(" "); 177 pfract(flen); 178 rval = 1; 179 } 180 break; 181 } 182 if (!rval) 183 (void)printf("zero%s", lflag ? "" : ".\n"); 184 if (lflag) 185 (void)printf("\n"); 186 } 187 188 static int 189 unit(int len, char *p) 190 { 191 int off, rval; 192 193 rval = 0; 194 if (len > 3) { 195 if (len % 3) { 196 off = len % 3; 197 len -= off; 198 if (number(p, off)) { 199 rval = 1; 200 (void)printf(" %s%s", 201 name3[len / 3], lflag ? " " : ".\n"); 202 } 203 p += off; 204 } 205 for (; len > 3; p += 3) { 206 len -= 3; 207 if (number(p, 3)) { 208 rval = 1; 209 (void)printf(" %s%s", 210 name3[len / 3], lflag ? " " : ".\n"); 211 } 212 } 213 } 214 if (number(p, len)) { 215 if (!lflag) 216 (void)printf(".\n"); 217 rval = 1; 218 } 219 return (rval); 220 } 221 222 static int 223 number(char *p, int len) 224 { 225 int val, rval; 226 227 rval = 0; 228 switch (len) { 229 case 3: 230 if (*p != '0') { 231 rval = 1; 232 (void)printf("%s hundred", name1[*p - '0']); 233 } 234 ++p; 235 /* FALLTHROUGH */ 236 case 2: 237 val = (p[1] - '0') + (p[0] - '0') * 10; 238 if (val) { 239 if (rval) 240 (void)printf(" "); 241 if (val < 20) 242 (void)printf("%s", name1[val]); 243 else { 244 (void)printf("%s", name2[val / 10]); 245 if (val % 10) 246 (void)printf("-%s", name1[val % 10]); 247 } 248 rval = 1; 249 } 250 break; 251 case 1: 252 if (*p != '0') { 253 rval = 1; 254 (void)printf("%s", name1[*p - '0']); 255 } 256 } 257 return (rval); 258 } 259 260 static void 261 pfract(int len) 262 { 263 static char const * const pref[] = { "", "ten-", "hundred-" }; 264 265 switch(len) { 266 case 1: 267 (void)printf("tenths.\n"); 268 break; 269 case 2: 270 (void)printf("hundredths.\n"); 271 break; 272 default: 273 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]); 274 break; 275 } 276 } 277 278 static void 279 usage(void) 280 { 281 (void)fprintf(stderr, "usage: number [-l] [# ...]\n"); 282 exit(1); 283 } 284