1 /* This file is part of GDBM test suite.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 GDBM is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 GDBM is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GDBM. If not, see <http://www.gnu.org/licenses/>.
26 const char *nstr[][10] = {
62 const char *short_scale[] = {
67 /* End of range for 32-bit unsigned long */
69 size_t short_scale_max = sizeof (short_scale) / sizeof (short_scale[0]);
72 size_t bufsize = sizeof(buffer);
77 copy (const char *str, int dch)
79 size_t len = strlen (str);
80 if (len + !!dch > bufoff)
83 buffer[--bufoff] = dch;
85 memcpy (buffer + bufoff, str, len);
90 format_100 (unsigned long num)
95 copy (nstr[0][num], delim);
97 copy (nstr[1][num-10], delim);
100 unsigned long tens = num / 10;
104 copy (nstr[0][num], delim);
106 copy (nstr[2][tens], 0);
109 copy (nstr[2][tens], delim);
114 format_1000 (unsigned long num, int more)
116 size_t n = num % 100;
124 copy ("hundred", delim);
125 copy (nstr[0][num], delim);
131 format_number (unsigned long num)
137 buffer[--bufoff] = 0;
143 unsigned long n = num % 1000;
147 if (s > 0 && ((n && off > bufoff) || num == 0))
148 copy (short_scale[s], delim);
151 if (s > short_scale_max)
154 format_1000 (n, num != 0);
158 if (bufoff + 1 == bufsize)
159 copy (nstr[0][0], 0);
164 print_number (unsigned long num)
167 printf ("%lu\t%s\n", num, buffer + bufoff);
171 print_range (unsigned long num, unsigned long to)
173 for (; num <= to; num++)
178 xstrtoul (char *arg, char **endp)
184 num = strtoul (arg, &p, 10);
187 fprintf (stderr, "%s: invalid number: ", progname);
195 fprintf (stderr, "%s: invalid number (near %s)\n",
204 main (int argc, char **argv)
208 if (argc == 1 || strcmp (argv[1], "-h") == 0)
210 printf ("usage: %s NUM [NUM...]\n", progname);
211 printf ("where NUM is a decimal number, NUM:COUNT or NUM-NUM\n");
218 unsigned long num, num2;
221 num = xstrtoul (arg, &p);
227 num2 = xstrtoul (p, NULL);
230 fprintf (stderr, "%s: invalid count\n", progname);
233 print_range (num, num + num2 - 1);
238 num2 = xstrtoul (p, NULL);
241 fprintf (stderr, "%s: invalid range: %lu-%lu\n",
242 progname, num, num2);
246 print_range (num, num2);
250 fprintf (stderr, "%s: invalid argument\n", progname);