.
[coreutils.git] / src / seq.c
blobcdfcd6da3600b4f77c0910620ce356ee4f3bc251
1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 94, 1995 Free Software Foundation, Inc.
4 This program 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)
7 any later version.
9 This program 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 this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Ulrich Drepper. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <math.h>
23 #include <stdio.h>
25 #include "system.h"
26 #include "error.h"
27 #include "version.h"
29 static double scan_double_arg __P ((char *arg));
30 static int check_format __P ((char *format_string));
31 static char *get_width_format __P ((void));
32 static int print_numbers __P ((char *format_str));
34 /* If nonzero print all number with equal width. */
35 static int equal_width;
37 /* The printf(3) format used for output. */
38 static char *format_str;
40 /* The starting number. */
41 static double from;
43 /* The name that this program was run with. */
44 char *program_name;
46 /* The string used to separate two number. */
47 static char *separator;
49 /* If nonzero, display usage information and exit. */
50 static int show_help;
52 /* If nonzero, print the version on standard output and exit. */
53 static int show_version;
55 /* The increment. */
56 static double step;
58 /* The last number. */
59 static double last;
61 static struct option const long_options[] =
63 { "equal-width", no_argument, NULL, 'w'},
64 { "format", required_argument, NULL, 'f'},
65 { "help", no_argument, &show_help, 1},
66 { "separator", required_argument, NULL, 's'},
67 { "version", no_argument, &show_version, 1},
68 { NULL, 0, NULL, 0}
71 static void
72 usage (int status)
74 if (status != 0)
75 (void) fprintf (stderr, _("Try `%s --help' for more information.\n"),
76 program_name);
77 else
79 (void) printf (_("\
80 Usage: %s [OPTION]... [from [step]] to\n\
81 "), program_name);
82 (void) printf (_("\
83 \n\
84 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
85 --help display this help and exit\n\
86 -s, --separator STRING use STRING for separating numbers (default: \\n)\n\
87 --version output version information and exit\n\
88 -w, --equal-width equalize width by padding with leading zeroes\n\
89 \n\
90 FROM, STEP, TO are interpreted as floating point. STEP should be > 0 if\n\
91 FROM is smaller than TO and vice versa. When given, the FORMAT argument\n\
92 must contain exactly one of the float output formats %%e, %%f, or %%g.\n\
93 "));
95 exit (status);
98 void
99 main (int argc, char **argv)
101 int errs;
102 int optc;
103 int step_is_set;
105 program_name = argv[0];
106 setlocale (LC_ALL, "");
107 bindtextdomain (PACKAGE, LOCALEDIR);
108 textdomain (PACKAGE);
110 equal_width = 0;
111 format_str = NULL;
112 separator = "\n";
113 from = 1.0;
114 step_is_set = 0;
116 /* We have to handle negative numbers in the command line but this
117 conflicts with the command line arguments. So the getopt mode is
118 REQUIRE_ORDER (the '+' in the format string) and it abort on the
119 first non-option or negative number. */
120 while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
121 (int *) 0)) != EOF)
123 if ('0' <= optc && optc <= '9')
125 /* means negative number */
126 break;
129 switch (optc)
131 case 0:
132 break;
134 case 'f':
135 format_str = optarg;
136 break;
138 case 's':
139 separator = optarg;
140 break;
142 case 'w':
143 equal_width = 1;
144 break;
146 default:
147 usage (1);
148 /* NOTREACHED */
152 if (show_version)
154 (void) printf ("seq - %s\n", version_string);
155 exit (0);
158 if (show_help)
160 usage (0);
161 /* NOTREACHED */
164 if (optind >= argc)
166 error (0, 0, _("too few arguments"));
167 usage (1);
168 /* NOTREACHED */
170 last = scan_double_arg (argv[optind++]);
172 if (optind < argc)
174 from = last;
175 last = scan_double_arg (argv[optind++]);
177 if (optind < argc)
179 step = last;
180 step_is_set = 1;
181 last = scan_double_arg (argv[optind++]);
183 if (optind < argc)
185 usage (1);
186 /* NOTREACHED */
191 if (format_str != NULL && equal_width)
193 error (0, 0, _("\
194 format string may not be specified when printing equal width strings"));
195 usage (1);
198 if (!step_is_set)
200 step = from <= last ? 1.0 : -1.0;
203 if (format_str != NULL)
205 if (!check_format (format_str))
207 error (0, 0, _("invalid format string: `%s'"), format_str);
208 usage (1);
211 else
213 if (equal_width)
214 format_str = get_width_format ();
215 else
216 format_str = "%g";
219 errs = print_numbers (format_str);
221 exit (errs);
222 /* NOTREACHED */
225 /* Read a double value from the command line.
226 Return if the string is correct else signal error. */
228 static double
229 scan_double_arg (char *arg)
231 char *end_ptr;
232 double ret_val;
234 /* FIXME: use xstrtod? At least set and check errno. */
235 ret_val = strtod (arg, &end_ptr);
236 if (end_ptr == arg || *end_ptr != '\0')
238 error (0, 0, _("invalid float argument: %s"), arg);
239 usage (1);
240 /* NOTREACHED */
243 return ret_val;
246 /* Check whether the format string is valid for a single double
247 argument.
248 Return 0 if not, 1 if correct. */
250 static int
251 check_format (char *format_string)
253 while (*format_string != '\0')
255 if (*format_string == '%')
257 format_string++;
258 if (*format_string != '%')
259 break;
262 format_string++;
264 if (*format_string == '\0')
265 return 0;
267 format_string += strspn (format_string, "-+#0");
268 if (isdigit (*format_string))
270 format_string += strspn (format_string, "012345789");
272 if (*format_string == '.')
273 format_string += strspn (++format_string, "0123456789");
276 if (*format_string != 'e' && *format_string != 'f' &&
277 *format_string != 'g')
278 return 0;
280 format_string++;
281 while (*format_string != '\0')
283 if (*format_string == '%')
285 format_string++;
286 if (*format_string != '%')
287 return 0;
290 format_string++;
293 return 1;
296 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
298 /* Return a printf-style format string with which all selected numbers
299 will format to strings of the same width. */
301 static char *
302 get_width_format ()
304 static char buffer[256];
305 int full_width;
306 int frac_width;
307 int width1, width2;
308 double max_val;
309 double min_val;
310 double temp;
312 if (from > last)
314 min_val = from - step * floor ((from - last) / step);
315 max_val = from;
317 else
319 min_val = from;
320 max_val = from + step * floor ((last - from) / step);
323 (void) sprintf (buffer, "%g", rint (max_val));
324 if (buffer[strspn (buffer, "0123456789")] != '\0')
325 return "%g";
326 width1 = strlen (buffer);
328 if (min_val < 0.0)
330 (void) sprintf (buffer, "%g", rint (min_val));
331 if (buffer[strspn (buffer, "-0123456789")] != '\0')
332 return "%g";
333 width2 = strlen (buffer);
335 width1 = width1 > width2 ? width1 : width2;
337 full_width = width1;
339 (void) sprintf (buffer, "%g", 1.0 + modf (min_val, &temp));
340 width1 = strlen (buffer);
341 if (width1 == 1)
342 width1 = 0;
343 else
345 if (buffer[0] != '1' || buffer[1] != '.' ||
346 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
347 return "%g";
348 width1 -= 2;
351 (void) sprintf (buffer, "%g", 1.0 + modf (step, &temp));
352 width2 = strlen (buffer);
353 if (width2 == 1)
354 width2 = 0;
355 else
357 if (buffer[0] != '1' || buffer[1] != '.' ||
358 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
359 return "%g";
360 width2 -= 2;
362 frac_width = width1 > width2 ? width1 : width2;
364 if (frac_width)
365 (void) sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
366 else
367 (void) sprintf (buffer, "%%0%dg", full_width);
369 return buffer;
372 #else /* one of the math functions rint, modf, floor is missing. */
374 static char *
375 get_width_format (void)
377 /* We cannot compute the needed information to determine the correct
378 answer. So we simply return a value that works for all cases. */
379 return "%g";
382 #endif
384 /* Actually print the sequence of numbers in the specified range, with the
385 given or default stepping and format. */
386 static int
387 print_numbers (char *format_str)
389 if (from > last)
391 if (step >= 0)
393 error (0, 0, _("invalid increment: %g"), step);
394 usage (1);
395 /* NOTREACHED */
398 while (1)
400 (void) printf (format_str, from);
402 /* FIXME: don't increment!!! Use `first + i * step'. */
403 from += step;
404 if (from < last)
405 break;
407 (void) fputs (separator, stdout);
410 else
412 if (step <= 0)
414 error (0, 0, _("invalid increment: %g"), step);
415 usage (1);
416 /* NOTREACHED */
419 while (1)
421 (void) printf (format_str, from);
423 /* FIXME: don't increment!!! Use `first + i * step'. */
424 from += step;
425 if (from > last)
426 break;
428 (void) fputs (separator, stdout);
432 return 0;