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 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]... [START [INCREMENT]] LIMIT\n\
89 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
90 --help display this help and exit\n\
91 -s, --separator STRING use STRING for separating numbers (default: \\n)\n\
92 --version output version information and exit\n\
93 -w, --equal-width equalize width by padding with leading zeroes\n\
95 START, INCREMENT, and LIMIT are interpreted as floating point values.\n\
96 INCREMENT should be positive if START is smaller than LIMIT and negative\n\
97 otherwise. When given, the FORMAT argument must contain exactly one of\n\
98 the printf-style, floating point output formats %%e, %%f, or %%g.\n\
105 main (int argc, char **argv)
111 program_name = argv[0];
112 setlocale (LC_ALL, "");
113 bindtextdomain (PACKAGE, LOCALEDIR);
114 textdomain (PACKAGE);
122 /* We have to handle negative numbers in the command line but this
123 conflicts with the command line arguments. So the getopt mode is
124 REQUIRE_ORDER (the '+' in the format string) and it abort on the
125 first non-option or negative number. */
126 while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
129 if ('0' <= optc && optc <= '9')
131 /* means negative number */
160 (void) printf ("seq - %s\n", PACKAGE_VERSION);
172 error (0, 0, _("too few arguments"));
176 last = scan_double_arg (argv[optind++]);
181 last = scan_double_arg (argv[optind++]);
187 last = scan_double_arg (argv[optind++]);
197 if (format_str != NULL && equal_width)
200 format string may not be specified when printing equal width strings"));
206 step = first <= last ? 1.0 : -1.0;
209 if (format_str != NULL)
211 if (!check_format (format_str))
213 error (0, 0, _("invalid format string: `%s'"), format_str);
220 format_str = get_width_format ();
225 errs = print_numbers (format_str);
231 /* Read a double value from the command line.
232 Return if the string is correct else signal error. */
235 scan_double_arg (const char *arg)
239 if (xstrtod (arg, NULL, &ret_val))
241 error (0, 0, _("invalid floating point argument: %s"), arg);
249 /* Check whether the format string is valid for a single double
251 Return 0 if not, 1 if correct. */
254 check_format (const char *format_string)
256 while (*format_string != '\0')
258 if (*format_string == '%')
261 if (*format_string != '%')
267 if (*format_string == '\0')
270 format_string += strspn (format_string, "-+#0");
271 if (isdigit (*format_string))
273 format_string += strspn (format_string, "012345789");
275 if (*format_string == '.')
276 format_string += strspn (++format_string, "0123456789");
279 if (*format_string != 'e' && *format_string != 'f' &&
280 *format_string != 'g')
284 while (*format_string != '\0')
286 if (*format_string == '%')
289 if (*format_string != '%')
299 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
301 /* Return a printf-style format string with which all selected numbers
302 will format to strings of the same width. */
307 static char buffer[256];
317 min_val = first - step * floor ((first - last) / step);
323 max_val = first + step * floor ((last - first) / step);
326 (void) sprintf (buffer, "%g", rint (max_val));
327 if (buffer[strspn (buffer, "0123456789")] != '\0')
329 width1 = strlen (buffer);
333 (void) sprintf (buffer, "%g", rint (min_val));
334 if (buffer[strspn (buffer, "-0123456789")] != '\0')
336 width2 = strlen (buffer);
338 width1 = width1 > width2 ? width1 : width2;
342 (void) sprintf (buffer, "%g", 1.0 + modf (min_val, &temp));
343 width1 = strlen (buffer);
348 if (buffer[0] != '1' || buffer[1] != '.' ||
349 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
354 (void) sprintf (buffer, "%g", 1.0 + modf (step, &temp));
355 width2 = strlen (buffer);
360 if (buffer[0] != '1' || buffer[1] != '.' ||
361 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
365 frac_width = width1 > width2 ? width1 : width2;
368 (void) sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
370 (void) sprintf (buffer, "%%0%dg", full_width);
375 #else /* one of the math functions rint, modf, floor is missing. */
378 get_width_format (void)
380 /* We cannot compute the needed information to determine the correct
381 answer. So we simply return a value that works for all cases. */
387 /* Actually print the sequence of numbers in the specified range, with the
388 given or default stepping and format. */
390 print_numbers (const char *format_str)
399 _("when the starting value is larger than the limit,\n\
400 the increment must be negative"));
405 printf (format_str, first);
406 for (i = 0; /* empty */; i++)
408 double x = first + i * step;
413 fputs (separator, stdout);
414 printf (format_str, x);
424 _("when the starting value is smaller than the limit,\n\
425 the increment must be positive"));
430 printf (format_str, first);
431 for (i = 1; /* empty */; i++)
433 double x = first + i * step;
438 fputs (separator, stdout);
439 printf (format_str, x);
442 fputs (terminator, stdout);