1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994, 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)
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. */
28 static double scan_double_arg ();
29 static int check_format ();
30 static char *get_width_format ();
31 static int print_numbers ();
33 /* If non-zero print all number with equal width. */
34 static int equal_width;
36 /* The printf(3) format used for output. */
37 static char *format_str;
39 /* The starting number. */
42 /* The name that this program was run with. */
45 /* The string used to separate two number. */
46 static char *separator;
48 /* If non-zero, display usage information and exit. */
51 /* If non-zero, print the version on standard output and exit. */
52 static int show_version;
57 /* The last number. */
60 static struct option const long_options[] =
62 { "equal-width", no_argument, NULL, 'w'},
63 { "format", required_argument, NULL, 'f'},
64 { "help", no_argument, &show_help, 1},
65 { "separator", required_argument, NULL, 's'},
66 { "version", no_argument, &show_version, 1},
75 (void) fprintf (stderr, "Try `%s --help' for more information.\n",
80 Usage: %s [OPTION]... [from [step]] to\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\
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\
107 program_name = argv[0];
114 /* We have to handle negative numbers in the command line but this
115 conflicts with the command line arguments. So the getopt mode is
116 REQUIRE_ORDER (the '+' in the format string) and it abort on the
117 first non-option or negative number. */
118 while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
121 if ('0' <= optc && optc <= '9')
123 /* means negative number */
152 (void) printf ("seq - %s\n", version_string);
163 error (0, 0, "too few arguments");
167 last = scan_double_arg (argv[optind++]);
172 last = scan_double_arg (argv[optind++]);
178 last = scan_double_arg (argv[optind++]);
190 step = from < last ? 1.0 : -1.0;
193 if (format_str != NULL)
195 if (!check_format (format_str))
197 error (0, 0, "invalid format string: `%s'", format_str);
204 format_str = get_width_format ();
209 errs = print_numbers (format_str);
215 /* Read a double value from the command line.
216 Return if the string is correct else signal error. */
219 scan_double_arg (arg)
225 /* FIXME: use xstrtod? At least set and check errno. */
226 ret_val = strtod (arg, &end_ptr);
227 if (end_ptr == arg || *end_ptr != '\0')
229 error (0, 0, "invalid float argument: %s\n", arg);
237 /* Check whether the format string is valid for a single double
239 Return 0 if not, 1 if correct. */
242 check_format (format_string)
245 while (*format_string != '\0')
247 if (*format_string == '%')
250 if (*format_string != '%')
256 if (*format_string == '\0')
259 format_string += strspn (format_string, "-+#0");
260 if (isdigit (*format_string))
262 format_string += strspn (format_string, "012345789");
264 if (*format_string == '.')
265 format_string += strspn (++format_string, "0123456789");
268 if (*format_string != 'e' && *format_string != 'f' &&
269 *format_string != 'g')
273 while (*format_string != '\0')
275 if (*format_string == '%')
278 if (*format_string != '%')
288 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
290 /* Return a printf-style format string with which all selected numbers
291 will format to strings of the same width. */
296 static char buffer[256];
306 min_val = from - step * floor ((from - last) / step);
312 max_val = from + step * floor ((last - from) / step);
315 (void) sprintf (buffer, "%g", rint (max_val));
316 if (buffer[strspn (buffer, "0123456789")] != '\0')
318 width1 = strlen (buffer);
322 (void) sprintf (buffer, "%g", rint (min_val));
323 if (buffer[strspn (buffer, "-0123456789")] != '\0')
325 width2 = strlen (buffer);
327 width1 = width1 > width2 ? width1 : width2;
331 (void) sprintf (buffer, "%g", 1.0 + modf (min_val, &temp));
332 width1 = strlen (buffer);
337 if (buffer[0] != '1' || buffer[1] != '.' ||
338 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
343 (void) sprintf (buffer, "%g", 1.0 + modf (step, &temp));
344 width2 = strlen (buffer);
349 if (buffer[0] != '1' || buffer[1] != '.' ||
350 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
354 frac_width = width1 > width2 ? width1 : width2;
357 (void) sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
359 (void) sprintf (buffer, "%%0%dg", full_width);
364 #else /* one of the math functions rint, modf, floor is missing. */
369 /* We cannot compute the needed information to determine the correct
370 answer. So we simply return a value that works for all cases. */
376 /* Actually print the sequence of numbers in the specified range, with the
377 given or default stepping and format. */
379 print_numbers (format_str)
386 error (0, 0, "invalid increment: %g\n", step);
393 (void) printf (format_str, from);
395 /* FIXME: don't increment!!! Use `first + i * step'. */
400 (void) fputs (separator, stdout);
407 error (0, 0, "invalid increment: %g\n", step);
414 (void) printf (format_str, from);
416 /* FIXME: don't increment!!! Use `first + i * step'. */
421 (void) fputs (separator, stdout);