1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2000 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>
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "seq"
35 #define AUTHORS "Ulrich Drepper"
37 /* If nonzero print all number with equal width. */
38 static int equal_width;
40 /* The name that this program was run with. */
43 /* The string used to separate two numbers. */
44 static char *separator;
46 /* The string output after all numbers have been output.
47 Usually "\n" or "\0". */
48 /* FIXME: make this an option. */
49 static char *terminator = "\n";
51 /* The representation of the decimal point in the current locale.
52 Always "." if the localeconv function is not supported. */
53 static char *decimal_point = ".";
55 /* The starting number. */
61 /* The last number. */
64 static struct option const long_options[] =
66 { "equal-width", no_argument, NULL, 'w'},
67 { "format", required_argument, NULL, 'f'},
68 { "separator", required_argument, NULL, 's'},
69 {GETOPT_HELP_OPTION_DECL},
70 {GETOPT_VERSION_OPTION_DECL},
78 fprintf (stderr, _("Try `%s --help' for more information.\n"),
83 Usage: %s [OPTION]... LAST\n\
84 or: %s [OPTION]... FIRST LAST\n\
85 or: %s [OPTION]... FIRST INCREMENT LAST\n\
86 "), program_name, program_name, program_name);
88 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
90 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
91 -s, --separator STRING use STRING to separate 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 If FIRST or INCREMENT is omitted, it defaults to 1.\n\
97 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
98 INCREMENT should be positive if FIRST is smaller than LAST, and negative\n\
99 otherwise. When given, the FORMAT argument must contain exactly one of\n\
100 the printf-style, floating point output formats %%e, %%f, %%g\n\
102 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
107 /* Read a double value from the command line.
108 Return if the string is correct else signal error. */
111 scan_double_arg (const char *arg)
115 if (xstrtod (arg, NULL, &ret_val))
117 error (0, 0, _("invalid floating point argument: %s"), arg);
124 /* Check whether the format string is valid for a single `double'
125 argument. Return 0 if not, 1 if correct. */
128 valid_format (const char *fmt)
144 fmt += strspn (fmt, "-+#0");
147 fmt += strspn (fmt, "0123456789");
150 fmt += strspn (++fmt, "0123456789");
153 if (!(*fmt == 'e' || *fmt == 'f' || *fmt == 'g'))
172 /* Actually print the sequence of numbers in the specified range, with the
173 given or default stepping and format. */
175 print_numbers (const char *fmt)
184 _("when the starting value is larger than the limit,\n\
185 the increment must be negative"));
190 for (i = 1; /* empty */; i++)
192 double x = first + i * step;
197 fputs (separator, stdout);
208 _("when the starting value is smaller than the limit,\n\
209 the increment must be positive"));
214 for (i = 1; /* empty */; i++)
216 double x = first + i * step;
221 fputs (separator, stdout);
225 fputs (terminator, stdout);
230 #if HAVE_RINT && HAVE_MODF && HAVE_FLOOR
232 /* Return a printf-style format string with which all selected numbers
233 will format to strings of the same width. */
238 static char buffer[256];
248 min_val = first - step * floor ((first - last) / step);
254 max_val = first + step * floor ((last - first) / step);
257 sprintf (buffer, "%g", rint (max_val));
258 if (buffer[strspn (buffer, "0123456789")] != '\0')
260 width1 = strlen (buffer);
264 double int_min_val = rint (min_val);
265 sprintf (buffer, "%g", int_min_val);
266 if (buffer[strspn (buffer, "-0123456789")] != '\0')
268 /* On some systems, `seq -w -.1 .1 .1' results in buffer being `-0'.
269 On others, it is just `0'. The former results in better output. */
270 width2 = (int_min_val == 0 ? 2 : strlen (buffer));
272 width1 = width1 > width2 ? width1 : width2;
276 sprintf (buffer, "%g", 1.0 + modf (fabs (min_val), &temp));
277 width1 = strlen (buffer);
283 /* FIXME: assumes that decimal_point is a single character
285 || buffer[1] != decimal_point[0]
286 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
291 sprintf (buffer, "%g", 1.0 + modf (fabs (step), &temp));
292 width2 = strlen (buffer);
298 /* FIXME: assumes that decimal_point is a single character
300 || buffer[1] != decimal_point[0]
301 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
305 frac_width = width1 > width2 ? width1 : width2;
308 sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
310 sprintf (buffer, "%%0%dg", full_width);
315 #else /* one of the math functions rint, modf, floor is missing. */
318 get_width_format (void)
320 /* We cannot compute the needed information to determine the correct
321 answer. So we simply return a value that works for all cases. */
328 main (int argc, char **argv)
334 /* The printf(3) format used for output. */
335 char *format_str = NULL;
337 program_name = argv[0];
338 setlocale (LC_ALL, "");
339 bindtextdomain (PACKAGE, LOCALEDIR);
340 textdomain (PACKAGE);
347 /* Figure out the locale's idea of a decimal point. */
350 struct lconv *locale;
352 locale = localeconv ();
354 if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
355 decimal_point = locale->decimal_point;
359 /* We have to handle negative numbers in the command line but this
360 conflicts with the command line arguments. So explicitly check first
361 whether the next argument looks like a negative number. */
362 while (optind < argc)
364 if (argv[optind][0] == '-'
365 && ((optc = argv[optind][1]) == decimal_point[0]
368 /* means negative number */
372 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
393 case_GETOPT_HELP_CHAR;
395 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
402 if (argc - optind < 1)
404 error (0, 0, _("too few arguments"));
408 if (3 < argc - optind)
410 error (0, 0, _("too many arguments"));
414 if (format_str && !valid_format (format_str))
416 error (0, 0, _("invalid format string: `%s'"), format_str);
420 last = scan_double_arg (argv[optind++]);
425 last = scan_double_arg (argv[optind++]);
431 last = scan_double_arg (argv[optind++]);
436 if (format_str != NULL && equal_width)
439 format string may not be specified when printing equal width strings"));
445 step = first <= last ? 1.0 : -1.0;
448 if (format_str == NULL)
451 format_str = get_width_format ();
456 errs = print_numbers (format_str);