1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Ulrich Drepper. */
23 #include <sys/types.h>
31 /* Roll our own isfinite rather than using <math.h>, so that we don't
32 have to worry about linking -lm just for isfinite. */
34 # define isfinite(x) ((x) * 0 == 0)
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "seq"
40 #define AUTHORS "Ulrich Drepper"
42 /* If true print all number with equal width. */
43 static bool equal_width;
45 /* The name that this program was run with. */
48 /* The string used to separate two numbers. */
49 static char const *separator;
51 /* The string output after all numbers have been output.
52 Usually "\n" or "\0". */
53 /* FIXME: make this an option. */
54 static char const terminator[] = "\n";
56 static struct option const long_options[] =
58 { "equal-width", no_argument, NULL, 'w'},
59 { "format", required_argument, NULL, 'f'},
60 { "separator", required_argument, NULL, 's'},
61 {GETOPT_HELP_OPTION_DECL},
62 {GETOPT_VERSION_OPTION_DECL},
69 if (status != EXIT_SUCCESS)
70 fprintf (stderr, _("Try `%s --help' for more information.\n"),
75 Usage: %s [OPTION]... LAST\n\
76 or: %s [OPTION]... FIRST LAST\n\
77 or: %s [OPTION]... FIRST INCREMENT LAST\n\
78 "), program_name, program_name, program_name);
80 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
82 -f, --format=FORMAT use printf style floating-point FORMAT\n\
83 -s, --separator=STRING use STRING to separate numbers (default: \\n)\n\
84 -w, --equal-width equalize width by padding with leading zeroes\n\
86 fputs (HELP_OPTION_DESCRIPTION, stdout);
87 fputs (VERSION_OPTION_DESCRIPTION, stdout);
90 If FIRST or INCREMENT is omitted, it defaults to 1. That is, an\n\
91 omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.\n\
92 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
93 INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
94 INCREMENT is usually negative if FIRST is greater than LAST.\n\
95 FORMAT must be suitable for printing one argument of type `double';\n\
96 it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point\n\
97 decimal numbers with maximum precision PREC, and to %g otherwise.\n\
99 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
104 /* A command-line operand. */
107 /* Its value, converted to 'long double'. */
110 /* Its print width, if it were printed out in a form similar to its
111 input form. An input like "-.1" is treated like "-0.1", and an
112 input like "1." is treated like "1", but otherwise widths are
116 /* Number of digits after the decimal point, or INT_MAX if the
117 number can't easily be expressed as a fixed-point number. */
120 typedef struct operand operand;
122 /* Read a long double value from the command line.
123 Return if the string is correct else signal error. */
126 scan_arg (const char *arg)
130 if (! xstrtold (arg, NULL, &ret.value, c_strtold))
132 error (0, 0, _("invalid floating point argument: %s"), arg);
133 usage (EXIT_FAILURE);
136 ret.width = strlen (arg);
137 ret.precision = INT_MAX;
139 if (! arg[strcspn (arg, "eExX")] && isfinite (ret.value))
141 char const *decimal_point = strchr (arg, '.');
146 size_t fraction_len = strlen (decimal_point + 1);
147 if (fraction_len <= INT_MAX)
148 ret.precision = fraction_len;
149 ret.width += (fraction_len == 0
151 : (decimal_point == arg
152 || ! ISDIGIT (decimal_point[-1])));
159 /* If FORMAT is a valid printf format for a double argument, return
160 its long double equivalent, possibly allocated from dynamic
161 storage; otherwise, return NULL. */
164 long_double_format (char const *fmt)
170 for (i = 0; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i++)
175 i += strspn (fmt + i, "-+#0 '");
176 i += strspn (fmt + i, "0123456789");
180 i += strspn (fmt + i, "0123456789");
184 has_L = (fmt[i] == 'L');
186 if (! strchr ("efgaEFGA", fmt[i]))
189 for (i++; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i++)
192 size_t format_size = i + 1;
193 char *ldfmt = xmalloc (format_size + 1);
194 memcpy (ldfmt, fmt, prefix_len);
195 ldfmt[prefix_len] = 'L';
196 strcpy (ldfmt + prefix_len + 1, fmt + prefix_len + has_L);
203 /* Actually print the sequence of numbers in the specified range, with the
204 given or default stepping and format. */
207 print_numbers (char const *fmt,
208 long double first, long double step, long double last)
212 for (i = 0; /* empty */; i++)
214 long double x = first + i * step;
215 if (step < 0 ? x < last : last < x)
218 fputs (separator, stdout);
223 fputs (terminator, stdout);
226 /* Return the default format given FIRST, STEP, and LAST. */
228 get_default_format (operand first, operand step, operand last)
230 static char format_buf[sizeof "%0.Lf" + 2 * INT_STRLEN_BOUND (int)];
232 int prec = MAX (first.precision, step.precision);
234 if (prec != INT_MAX && last.precision != INT_MAX)
238 size_t first_width = first.width + (prec - first.precision);
239 size_t last_width = last.width + (prec - last.precision);
240 if (first.width <= first_width
241 && (last.width < last_width) == (prec < last.precision))
243 size_t width = MAX (first_width, last_width);
244 if (width <= INT_MAX)
247 sprintf (format_buf, "%%0%d.%dLf", w, prec);
254 sprintf (format_buf, "%%.%dLf", prec);
263 main (int argc, char **argv)
266 operand first = { 1, 1, 0 };
267 operand step = { 1, 1, 0 };
270 /* The printf(3) format used for output. */
271 char const *format_str = NULL;
273 initialize_main (&argc, &argv);
274 program_name = argv[0];
275 setlocale (LC_ALL, "");
276 bindtextdomain (PACKAGE, LOCALEDIR);
277 textdomain (PACKAGE);
279 atexit (close_stdout);
284 /* We have to handle negative numbers in the command line but this
285 conflicts with the command line arguments. So explicitly check first
286 whether the next argument looks like a negative number. */
287 while (optind < argc)
289 if (argv[optind][0] == '-'
290 && ((optc = argv[optind][1]) == '.' || ISDIGIT (optc)))
292 /* means negative number */
296 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
314 case_GETOPT_HELP_CHAR;
316 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
319 usage (EXIT_FAILURE);
323 if (argc - optind < 1)
325 error (0, 0, _("missing operand"));
326 usage (EXIT_FAILURE);
329 if (3 < argc - optind)
331 error (0, 0, _("extra operand %s"), quote (argv[optind + 3]));
332 usage (EXIT_FAILURE);
337 char const *f = long_double_format (format_str);
340 error (0, 0, _("invalid format string: %s"), quote (format_str));
341 usage (EXIT_FAILURE);
346 last = scan_arg (argv[optind++]);
351 last = scan_arg (argv[optind++]);
356 last = scan_arg (argv[optind++]);
360 if (format_str != NULL && equal_width)
363 format string may not be specified when printing equal width strings"));
364 usage (EXIT_FAILURE);
367 if (format_str == NULL)
368 format_str = get_default_format (first, step, last);
370 print_numbers (format_str, first.value, step.value, last.value);