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>
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "seq"
34 #define AUTHORS "Ulrich Drepper"
36 /* The C type of value to be printed with format_str. */
42 typedef enum Format_type Format_type;
44 #define DO_printf(Format, Value) \
47 if (format_type == FT_DOUBLE) \
48 printf ((Format), (Value)); \
50 printf ((Format), (int) (Value)); \
54 static double scan_arg PARAMS ((const char *arg));
55 static int check_format PARAMS ((const char *format_string, Format_type *format_type));
56 static char *get_width_format PARAMS ((void));
57 static int print_numbers PARAMS ((const char *format_str));
59 /* If nonzero print all number with equal width. */
60 static int equal_width;
62 /* The name that this program was run with. */
65 /* The string used to separate two numbers. */
66 static char *separator;
68 /* The string output after all numbers have been output.
69 Usually "\n" or "\0". */
70 /* FIXME: make this an option. */
71 static char *terminator = "\n";
73 /* The representation of the decimal point in the current locale.
74 Always "." if the localeconv function is not supported. */
75 static char *decimal_point = ".";
77 /* The C type of value to be printed with format_str. */
78 static Format_type format_type = FT_DOUBLE;
80 /* The starting number. */
86 /* The last number. */
89 static struct option const long_options[] =
91 { "equal-width", no_argument, NULL, 'w'},
92 { "format", required_argument, NULL, 'f'},
93 { "separator", required_argument, NULL, 's'},
94 {GETOPT_HELP_OPTION_DECL},
95 {GETOPT_VERSION_OPTION_DECL},
103 fprintf (stderr, _("Try `%s --help' for more information.\n"),
108 Usage: %s [OPTION]... LAST\n\
109 or: %s [OPTION]... FIRST LAST\n\
110 or: %s [OPTION]... FIRST INCREMENT LAST\n\
111 "), program_name, program_name, program_name);
113 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
115 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
116 -s, --separator STRING use STRING to separate numbers (default: \\n)\n\
117 -w, --equal-width equalize width by padding with leading zeroes\n\
118 --help display this help and exit\n\
119 --version output version information and exit\n\
121 If FIRST or INCREMENT is omitted, it defaults to 1.\n\
122 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
123 INCREMENT should be positive if FIRST is smaller than LAST, and negative\n\
124 otherwise. When given, the FORMAT argument must contain exactly one of\n\
125 the printf-style, floating point output formats %%e, %%f, %%g, or\n\
126 integer output formats %%d, %%u, %%o, %%x, %%X.\n\
128 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
134 main (int argc, char **argv)
141 /* The printf(3) format used for output. */
142 char *format_str = NULL;
144 program_name = argv[0];
145 setlocale (LC_ALL, "");
146 bindtextdomain (PACKAGE, LOCALEDIR);
147 textdomain (PACKAGE);
154 /* Figure out the locale's idea of a decimal point. */
155 #ifdef HAVE_LOCALECONV
157 struct lconv *locale;
159 locale = localeconv ();
161 if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
162 decimal_point = locale->decimal_point;
166 /* We have to handle negative numbers in the command line but this
167 conflicts with the command line arguments. So explicitly check first
168 whether the next argument looks like a negative number. */
169 while (optind < argc)
171 if (argv[optind][0] == '-'
172 && ((optc = argv[optind][1]) == decimal_point[0]
175 /* means negative number */
179 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
200 case_GETOPT_HELP_CHAR;
202 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
210 /* Set format_type before calling scan_arg. */
211 if (format_str != NULL)
212 format_ok = check_format (format_str, &format_type);
216 format_type = FT_DOUBLE;
221 error (0, 0, _("too few arguments"));
225 last = scan_arg (argv[optind++]);
230 last = scan_arg (argv[optind++]);
236 last = scan_arg (argv[optind++]);
246 if (format_str != NULL && equal_width)
249 format string may not be specified when printing equal width strings"));
255 step = first <= last ? 1.0 : -1.0;
258 if (format_str != NULL)
262 error (0, 0, _("invalid format string: `%s'"), format_str);
269 format_str = get_width_format ();
274 errs = print_numbers (format_str);
280 /* Read a double value from the command line.
281 Return if the string is correct else signal error. */
284 scan_double_arg (const char *arg)
288 if (xstrtod (arg, NULL, &ret_val))
290 error (0, 0, _("invalid floating point argument: %s"), arg);
298 /* Read an int value from the command line.
299 Return if the string is correct else signal error. */
302 scan_int_arg (const char *arg)
306 if (xstrtol (arg, NULL, 10, &ret_val, "") != LONGINT_OK
307 || ret_val < INT_MIN || ret_val > INT_MAX)
309 error (0, 0, _("invalid integer argument: %s"), arg);
317 /* Read a double value from the command line.
318 Return if the string is correct else signal error. */
321 scan_arg (const char *arg)
326 return (double) scan_int_arg (arg);
328 return scan_double_arg (arg);
334 /* Check whether the format string is valid for a single `double'
335 argument or a single `int' argument. Return 0 if not, 1 if correct.
336 Set *INTCONV to non-zero if the conversion specifier is valid
337 for a single `int' argument, otherwise to zero. */
340 check_format (const char *fmt, Format_type *format_type_ptr)
342 *format_type_ptr = FT_DOUBLE;
358 fmt += strspn (fmt, "-+#0");
361 fmt += strspn (fmt, "0123456789");
364 fmt += strspn (++fmt, "0123456789");
367 if (*fmt == 'd' || *fmt == 'u' || *fmt == 'o' || *fmt == 'x' || *fmt == 'X')
368 *format_type_ptr = FT_INT;
369 else if (!(*fmt == 'e' || *fmt == 'f' || *fmt == 'g'))
388 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
390 /* Return a printf-style format string with which all selected numbers
391 will format to strings of the same width. */
396 static char buffer[256];
406 min_val = first - step * floor ((first - last) / step);
412 max_val = first + step * floor ((last - first) / step);
415 sprintf (buffer, "%g", rint (max_val));
416 if (buffer[strspn (buffer, "0123456789")] != '\0')
418 width1 = strlen (buffer);
422 sprintf (buffer, "%g", rint (min_val));
423 if (buffer[strspn (buffer, "-0123456789")] != '\0')
425 width2 = strlen (buffer);
427 width1 = width1 > width2 ? width1 : width2;
431 sprintf (buffer, "%g", 1.0 + modf (fabs (min_val), &temp));
432 width1 = strlen (buffer);
438 /* FIXME: assumes that decimal_point is a single character
440 || buffer[1] != decimal_point[0]
441 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
446 sprintf (buffer, "%g", 1.0 + modf (fabs (step), &temp));
447 width2 = strlen (buffer);
453 /* FIXME: assumes that decimal_point is a single character
455 || buffer[1] != decimal_point[0]
456 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
460 frac_width = width1 > width2 ? width1 : width2;
463 sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
465 sprintf (buffer, "%%0%dg", full_width);
470 #else /* one of the math functions rint, modf, floor is missing. */
473 get_width_format (void)
475 /* We cannot compute the needed information to determine the correct
476 answer. So we simply return a value that works for all cases. */
482 /* Actually print the sequence of numbers in the specified range, with the
483 given or default stepping and format. */
485 print_numbers (const char *fmt)
494 _("when the starting value is larger than the limit,\n\
495 the increment must be negative"));
500 DO_printf (fmt, first);
501 for (i = 1; /* empty */; i++)
503 double x = first + i * step;
508 fputs (separator, stdout);
519 _("when the starting value is smaller than the limit,\n\
520 the increment must be positive"));
525 DO_printf (fmt, first);
526 for (i = 1; /* empty */; i++)
528 double x = first + i * step;
533 fputs (separator, stdout);
537 fputs (terminator, stdout);