1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 94, 1995, 96 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Ulrich Drepper. */
29 static double scan_double_arg __P ((const char *arg));
30 static int check_format __P ((const char *format_string));
31 static char *get_width_format __P ((void));
32 static int print_numbers __P ((const 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. */
43 /* The name that this program was run with. */
46 /* The string used to separate two numbers. */
47 static char *separator;
49 /* The string output after all numbers have been output.
50 Usually "\n" or "\0". */
51 /* FIXME: make this an option. */
52 static char *terminator = "\n";
54 /* If nonzero, display usage information and exit. */
57 /* If nonzero, print the version on standard output and exit. */
58 static int show_version;
63 /* The last number. */
66 static struct option const long_options[] =
68 { "equal-width", no_argument, NULL, 'w'},
69 { "format", required_argument, NULL, 'f'},
70 { "help", no_argument, &show_help, 1},
71 { "separator", required_argument, NULL, 's'},
72 { "version", no_argument, &show_version, 1},
80 (void) fprintf (stderr, _("Try `%s --help' for more information.\n"),
85 Usage: %s [OPTION]... [FIRST [INCREMENT]] LAST\n\
88 Print numbers from FIRST (default 1) to LAST, moving by STEP (default 1).\n\
90 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
91 -s, --separator STRING use STRING for separating numbers (default: \\n)\n\
92 -w, --equal-width equalize width by padding with leading zeroes\n\
93 --help display this help and exit\n\
94 --version output version information and exit\n\
96 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
97 INCREMENT should be positive if FIRST is smaller than LAST, and negative\n\
98 otherwise. When given, the FORMAT argument must contain exactly one of\n\
99 the printf-style, floating point output formats %%e, %%f, or %%g.\n\
106 main (int argc, char **argv)
112 program_name = argv[0];
113 setlocale (LC_ALL, "");
114 bindtextdomain (PACKAGE, LOCALEDIR);
115 textdomain (PACKAGE);
123 /* We have to handle negative numbers in the command line but this
124 conflicts with the command line arguments. So the getopt mode is
125 REQUIRE_ORDER (the '+' in the format string) and it abort on the
126 first non-option or negative number. */
127 while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
130 if ('0' <= optc && optc <= '9')
132 /* means negative number */
161 (void) printf ("seq - %s\n", PACKAGE_VERSION);
173 error (0, 0, _("too few arguments"));
177 last = scan_double_arg (argv[optind++]);
182 last = scan_double_arg (argv[optind++]);
188 last = scan_double_arg (argv[optind++]);
198 if (format_str != NULL && equal_width)
201 format string may not be specified when printing equal width strings"));
207 step = first <= last ? 1.0 : -1.0;
210 if (format_str != NULL)
212 if (!check_format (format_str))
214 error (0, 0, _("invalid format string: `%s'"), format_str);
221 format_str = get_width_format ();
226 errs = print_numbers (format_str);
232 /* Read a double value from the command line.
233 Return if the string is correct else signal error. */
236 scan_double_arg (const char *arg)
240 if (xstrtod (arg, NULL, &ret_val))
242 error (0, 0, _("invalid floating point argument: %s"), arg);
250 /* Check whether the format string is valid for a single double
252 Return 0 if not, 1 if correct. */
255 check_format (const char *format_string)
257 while (*format_string != '\0')
259 if (*format_string == '%')
262 if (*format_string != '%')
268 if (*format_string == '\0')
271 format_string += strspn (format_string, "-+#0");
272 if (isdigit (*format_string))
274 format_string += strspn (format_string, "012345789");
276 if (*format_string == '.')
277 format_string += strspn (++format_string, "0123456789");
280 if (*format_string != 'e' && *format_string != 'f' &&
281 *format_string != 'g')
285 while (*format_string != '\0')
287 if (*format_string == '%')
290 if (*format_string != '%')
300 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
302 /* Return a printf-style format string with which all selected numbers
303 will format to strings of the same width. */
308 static char buffer[256];
318 min_val = first - step * floor ((first - last) / step);
324 max_val = first + step * floor ((last - first) / step);
327 (void) sprintf (buffer, "%g", rint (max_val));
328 if (buffer[strspn (buffer, "0123456789")] != '\0')
330 width1 = strlen (buffer);
334 (void) sprintf (buffer, "%g", rint (min_val));
335 if (buffer[strspn (buffer, "-0123456789")] != '\0')
337 width2 = strlen (buffer);
339 width1 = width1 > width2 ? width1 : width2;
343 (void) sprintf (buffer, "%g", 1.0 + modf (min_val, &temp));
344 width1 = strlen (buffer);
349 if (buffer[0] != '1' || buffer[1] != '.' ||
350 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
355 (void) sprintf (buffer, "%g", 1.0 + modf (step, &temp));
356 width2 = strlen (buffer);
361 if (buffer[0] != '1' || buffer[1] != '.' ||
362 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
366 frac_width = width1 > width2 ? width1 : width2;
369 (void) sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
371 (void) sprintf (buffer, "%%0%dg", full_width);
376 #else /* one of the math functions rint, modf, floor is missing. */
379 get_width_format (void)
381 /* We cannot compute the needed information to determine the correct
382 answer. So we simply return a value that works for all cases. */
388 /* Actually print the sequence of numbers in the specified range, with the
389 given or default stepping and format. */
391 print_numbers (const char *format_str)
400 _("when the starting value is larger than the limit,\n\
401 the increment must be negative"));
406 printf (format_str, first);
407 for (i = 1; /* empty */; i++)
409 double x = first + i * step;
414 fputs (separator, stdout);
415 printf (format_str, x);
425 _("when the starting value is smaller than the limit,\n\
426 the increment must be positive"));
431 printf (format_str, first);
432 for (i = 1; /* empty */; i++)
434 double x = first + i * step;
439 fputs (separator, stdout);
440 printf (format_str, x);
443 fputs (terminator, stdout);