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. */
24 #include <sys/types.h>
30 static double scan_double_arg __P ((const char *arg));
31 static int check_format __P ((const char *format_string));
32 static char *get_width_format __P ((void));
33 static int print_numbers __P ((const char *format_str));
35 /* If nonzero print all number with equal width. */
36 static int equal_width;
38 /* The printf(3) format used for output. */
39 static char *format_str;
41 /* The starting number. */
44 /* The name that this program was run with. */
47 /* The string used to separate two numbers. */
48 static char *separator;
50 /* The string output after all numbers have been output.
51 Usually "\n" or "\0". */
52 /* FIXME: make this an option. */
53 static char *terminator = "\n";
55 /* If nonzero, display usage information and exit. */
58 /* If nonzero, print the version on standard output and exit. */
59 static int show_version;
64 /* The last number. */
67 static struct option const long_options[] =
69 { "equal-width", no_argument, NULL, 'w'},
70 { "format", required_argument, NULL, 'f'},
71 { "help", no_argument, &show_help, 1},
72 { "separator", required_argument, NULL, 's'},
73 { "version", no_argument, &show_version, 1},
81 fprintf (stderr, _("Try `%s --help' for more information.\n"),
86 Usage: %s [OPTION]... LAST\n\
87 or: %s [OPTION]... FIRST LAST\n\
88 or: %s [OPTION]... FIRST INCREMENT LAST\n\
89 "), program_name, program_name, program_name);
91 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
93 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
94 -s, --separator STRING use STRING to separate numbers (default: \\n)\n\
95 -w, --equal-width equalize width by padding with leading zeroes\n\
96 --help display this help and exit\n\
97 --version output version information and exit\n\
99 If FIRST or INCREMENT is omitted, it defaults to 1.\n\
100 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
101 INCREMENT should be positive if FIRST is smaller than LAST, and negative\n\
102 otherwise. When given, the FORMAT argument must contain exactly one of\n\
103 the printf-style, floating point output formats %%e, %%f, or %%g.\n\
105 puts (_("\nReport bugs to sh-utils-bugs@gnu.ai.mit.edu"));
111 main (int argc, char **argv)
117 program_name = argv[0];
118 setlocale (LC_ALL, "");
119 bindtextdomain (PACKAGE, LOCALEDIR);
120 textdomain (PACKAGE);
128 /* We have to handle negative numbers in the command line but this
129 conflicts with the command line arguments. So the getopt mode is
130 REQUIRE_ORDER (the '+' in the format string) and it abort on the
131 first non-option or negative number. */
132 while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
135 if ('0' <= optc && optc <= '9')
137 /* means negative number */
166 printf ("seq (%s) %s\n", GNU_PACKAGE, VERSION);
178 error (0, 0, _("too few arguments"));
182 last = scan_double_arg (argv[optind++]);
187 last = scan_double_arg (argv[optind++]);
193 last = scan_double_arg (argv[optind++]);
203 if (format_str != NULL && equal_width)
206 format string may not be specified when printing equal width strings"));
212 step = first <= last ? 1.0 : -1.0;
215 if (format_str != NULL)
217 if (!check_format (format_str))
219 error (0, 0, _("invalid format string: `%s'"), format_str);
226 format_str = get_width_format ();
231 errs = print_numbers (format_str);
237 /* Read a double value from the command line.
238 Return if the string is correct else signal error. */
241 scan_double_arg (const char *arg)
245 if (xstrtod (arg, NULL, &ret_val))
247 error (0, 0, _("invalid floating point argument: %s"), arg);
255 /* Check whether the format string is valid for a single double
257 Return 0 if not, 1 if correct. */
260 check_format (const char *format_string)
262 while (*format_string != '\0')
264 if (*format_string == '%')
267 if (*format_string != '%')
273 if (*format_string == '\0')
276 format_string += strspn (format_string, "-+#0");
277 if (isdigit (*format_string))
279 format_string += strspn (format_string, "012345789");
281 if (*format_string == '.')
282 format_string += strspn (++format_string, "0123456789");
285 if (*format_string != 'e' && *format_string != 'f' &&
286 *format_string != 'g')
290 while (*format_string != '\0')
292 if (*format_string == '%')
295 if (*format_string != '%')
305 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
307 /* Return a printf-style format string with which all selected numbers
308 will format to strings of the same width. */
313 static char buffer[256];
323 min_val = first - step * floor ((first - last) / step);
329 max_val = first + step * floor ((last - first) / step);
332 sprintf (buffer, "%g", rint (max_val));
333 if (buffer[strspn (buffer, "0123456789")] != '\0')
335 width1 = strlen (buffer);
339 sprintf (buffer, "%g", rint (min_val));
340 if (buffer[strspn (buffer, "-0123456789")] != '\0')
342 width2 = strlen (buffer);
344 width1 = width1 > width2 ? width1 : width2;
348 sprintf (buffer, "%g", 1.0 + modf (min_val, &temp));
349 width1 = strlen (buffer);
354 if (buffer[0] != '1' || buffer[1] != '.' ||
355 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
360 sprintf (buffer, "%g", 1.0 + modf (step, &temp));
361 width2 = strlen (buffer);
366 if (buffer[0] != '1' || buffer[1] != '.' ||
367 buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
371 frac_width = width1 > width2 ? width1 : width2;
374 sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
376 sprintf (buffer, "%%0%dg", full_width);
381 #else /* one of the math functions rint, modf, floor is missing. */
384 get_width_format (void)
386 /* We cannot compute the needed information to determine the correct
387 answer. So we simply return a value that works for all cases. */
393 /* Actually print the sequence of numbers in the specified range, with the
394 given or default stepping and format. */
396 print_numbers (const char *format_str)
405 _("when the starting value is larger than the limit,\n\
406 the increment must be negative"));
411 printf (format_str, first);
412 for (i = 1; /* empty */; i++)
414 double x = first + i * step;
419 fputs (separator, stdout);
420 printf (format_str, x);
430 _("when the starting value is smaller than the limit,\n\
431 the increment must be positive"));
436 printf (format_str, first);
437 for (i = 1; /* empty */; i++)
439 double x = first + i * step;
444 fputs (separator, stdout);
445 printf (format_str, x);
448 fputs (terminator, stdout);