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)
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. */
28 static double scan_double_arg __P ((const char *arg));
29 static int check_format __P ((const char *format_string));
30 static char *get_width_format __P ((void));
31 static int print_numbers __P ((const char *format_str));
33 /* If nonzero 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 numbers. */
46 static char *separator;
48 /* The string output after all numbers have been output.
49 Usually "\n" or "\0". */
50 /* FIXME: make this an option. */
51 static char *terminator = "\n";
53 /* If nonzero, display usage information and exit. */
56 /* If nonzero, print the version on standard output and exit. */
57 static int show_version;
62 /* The last number. */
65 static struct option const long_options[] =
67 { "equal-width", no_argument, NULL, 'w'},
68 { "format", required_argument, NULL, 'f'},
69 { "help", no_argument, &show_help, 1},
70 { "separator", required_argument, NULL, 's'},
71 { "version", no_argument, &show_version, 1},
79 (void) fprintf (stderr, _("Try `%s --help' for more information.\n"),
84 Usage: %s [OPTION]... [FIRST [step]] LAST\n\
88 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
89 --help display this help and exit\n\
90 -s, --separator STRING use STRING for separating numbers (default: \\n)\n\
91 --version output version information and exit\n\
92 -w, --equal-width equalize width by padding with leading zeroes\n\
94 FROM, STEP, TO are interpreted as floating point. STEP should be > 0 if\n\
95 FROM is smaller than TO and vice versa. When given, the FORMAT argument\n\
96 must contain exactly one of the float output formats %%e, %%f, or %%g.\n\
103 main (int argc, char **argv)
109 program_name = argv[0];
110 setlocale (LC_ALL, "");
111 bindtextdomain (PACKAGE, LOCALEDIR);
112 textdomain (PACKAGE);
120 /* We have to handle negative numbers in the command line but this
121 conflicts with the command line arguments. So the getopt mode is
122 REQUIRE_ORDER (the '+' in the format string) and it abort on the
123 first non-option or negative number. */
124 while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
127 if ('0' <= optc && optc <= '9')
129 /* means negative number */
158 (void) printf ("seq - %s\n", PACKAGE_VERSION);
170 error (0, 0, _("too few arguments"));
174 last = scan_double_arg (argv[optind++]);
179 last = scan_double_arg (argv[optind++]);
185 last = scan_double_arg (argv[optind++]);
195 if (format_str != NULL && equal_width)
198 format string may not be specified when printing equal width strings"));
204 step = first <= last ? 1.0 : -1.0;
207 if (format_str != NULL)
209 if (!check_format (format_str))
211 error (0, 0, _("invalid format string: `%s'"), format_str);
218 format_str = get_width_format ();
223 errs = print_numbers (format_str);
229 /* Read a double value from the command line.
230 Return if the string is correct else signal error. */
233 scan_double_arg (const char *arg)
238 /* FIXME: use xstrtod? At least set and check errno. */
239 ret_val = strtod (arg, &end_ptr);
240 if (end_ptr == arg || *end_ptr != '\0')
242 error (0, 0, _("invalid float 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)
399 error (0, 0, _("invalid increment: %g"), step);
404 for (i = 0; /* empty */; i++)
406 double x = first + i * step;
407 printf (format_str, x);
412 fputs (separator, stdout);
421 error (0, 0, _("invalid increment: %g"), step);
426 for (i = 0; /* empty */; i++)
428 double x = first + i * step;
429 printf (format_str, x);
434 fputs (separator, stdout);
437 fputs (terminator, stdout);