1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2009 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Ulrich Drepper. */
22 #include <sys/types.h>
30 /* Roll our own isfinite rather than using <math.h>, so that we don't
31 have to worry about linking -lm just for isfinite. */
33 # define isfinite(x) ((x) * 0 == 0)
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "seq"
39 #define AUTHORS proper_name ("Ulrich Drepper")
41 /* If true print all number with equal width. */
42 static bool equal_width
;
44 /* The string used to separate two numbers. */
45 static char const *separator
;
47 /* The string output after all numbers have been output.
48 Usually "\n" or "\0". */
49 /* FIXME: make this an option. */
50 static char const terminator
[] = "\n";
52 static struct option
const long_options
[] =
54 { "equal-width", no_argument
, NULL
, 'w'},
55 { "format", required_argument
, NULL
, 'f'},
56 { "separator", required_argument
, NULL
, 's'},
57 {GETOPT_HELP_OPTION_DECL
},
58 {GETOPT_VERSION_OPTION_DECL
},
65 if (status
!= EXIT_SUCCESS
)
66 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
71 Usage: %s [OPTION]... LAST\n\
72 or: %s [OPTION]... FIRST LAST\n\
73 or: %s [OPTION]... FIRST INCREMENT LAST\n\
74 "), program_name
, program_name
, program_name
);
76 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
78 -f, --format=FORMAT use printf style floating-point FORMAT\n\
79 -s, --separator=STRING use STRING to separate numbers (default: \\n)\n\
80 -w, --equal-width equalize width by padding with leading zeroes\n\
82 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
83 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
86 If FIRST or INCREMENT is omitted, it defaults to 1. That is, an\n\
87 omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.\n\
88 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
89 INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
90 INCREMENT is usually negative if FIRST is greater than LAST.\n\
93 FORMAT must be suitable for printing one argument of type `double';\n\
94 it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point\n\
95 decimal numbers with maximum precision PREC, and to %g otherwise.\n\
97 emit_bug_reporting_address ();
102 /* A command-line operand. */
105 /* Its value, converted to 'long double'. */
108 /* Its print width, if it were printed out in a form similar to its
109 input form. An input like "-.1" is treated like "-0.1", and an
110 input like "1." is treated like "1", but otherwise widths are
114 /* Number of digits after the decimal point, or INT_MAX if the
115 number can't easily be expressed as a fixed-point number. */
118 typedef struct operand operand
;
120 /* Description of what a number-generating format will generate. */
123 /* Number of bytes before and after the number. */
128 /* Read a long double value from the command line.
129 Return if the string is correct else signal error. */
132 scan_arg (const char *arg
)
136 if (! xstrtold (arg
, NULL
, &ret
.value
, c_strtold
))
138 error (0, 0, _("invalid floating point argument: %s"), arg
);
139 usage (EXIT_FAILURE
);
142 /* We don't output spaces or '+' so don't include in width */
143 while (isspace (to_uchar (*arg
)) || *arg
== '+')
146 ret
.width
= strlen (arg
);
147 ret
.precision
= INT_MAX
;
149 if (! arg
[strcspn (arg
, "xX")] && isfinite (ret
.value
))
151 char const *decimal_point
= strchr (arg
, '.');
156 size_t fraction_len
= strcspn (decimal_point
+ 1, "eE");
157 if (fraction_len
<= INT_MAX
)
158 ret
.precision
= fraction_len
;
159 ret
.width
+= (fraction_len
== 0 /* #. -> # */
161 : (decimal_point
== arg
/* .# -> 0.# */
162 || ! ISDIGIT (decimal_point
[-1]))); /* -.# -> 0.# */
164 char const *e
= strchr (arg
, 'e');
166 e
= strchr (arg
, 'E');
169 long exponent
= strtol (e
+ 1, NULL
, 10);
170 ret
.precision
+= exponent
< 0 ? -exponent
: 0;
177 /* If FORMAT is a valid printf format for a double argument, return
178 its long double equivalent, allocated from dynamic storage, and
179 store into *LAYOUT a description of the output layout; otherwise,
180 report an error and exit. */
183 long_double_format (char const *fmt
, struct layout
*layout
)
186 size_t prefix_len
= 0;
187 size_t suffix_len
= 0;
188 size_t length_modifier_offset
;
191 for (i
= 0; ! (fmt
[i
] == '%' && fmt
[i
+ 1] != '%'); i
+= (fmt
[i
] == '%') + 1)
194 error (EXIT_FAILURE
, 0,
195 _("format %s has no %% directive"), quote (fmt
));
200 i
+= strspn (fmt
+ i
, "-+#0 '");
201 i
+= strspn (fmt
+ i
, "0123456789");
205 i
+= strspn (fmt
+ i
, "0123456789");
208 length_modifier_offset
= i
;
209 has_L
= (fmt
[i
] == 'L');
212 error (EXIT_FAILURE
, 0, _("format %s ends in %%"), quote (fmt
));
213 if (! strchr ("efgaEFGA", fmt
[i
]))
214 error (EXIT_FAILURE
, 0,
215 _("format %s has unknown %%%c directive"), quote (fmt
), fmt
[i
]);
217 for (i
++; ; i
+= (fmt
[i
] == '%') + 1)
218 if (fmt
[i
] == '%' && fmt
[i
+ 1] != '%')
219 error (EXIT_FAILURE
, 0, _("format %s has too many %% directives"),
225 size_t format_size
= i
+ 1;
226 char *ldfmt
= xmalloc (format_size
+ 1);
227 memcpy (ldfmt
, fmt
, length_modifier_offset
);
228 ldfmt
[length_modifier_offset
] = 'L';
229 strcpy (ldfmt
+ length_modifier_offset
+ 1,
230 fmt
+ length_modifier_offset
+ has_L
);
231 layout
->prefix_len
= prefix_len
;
232 layout
->suffix_len
= suffix_len
;
237 /* Actually print the sequence of numbers in the specified range, with the
238 given or default stepping and format. */
241 print_numbers (char const *fmt
, struct layout layout
,
242 long double first
, long double step
, long double last
)
244 bool out_of_range
= (step
< 0 ? first
< last
: last
< first
);
248 long double x
= first
;
257 x
= first
+ i
* step
;
258 out_of_range
= (step
< 0 ? x
< last
: last
< x
);
262 /* If the number just past LAST prints as a value equal
263 to LAST, and prints differently from the previous
264 number, then print the number. This avoids problems
265 with rounding. For example, with the x86 it causes
266 "seq 0 0.000001 0.000003" to print 0.000003 instead
267 of stopping at 0.000002. */
269 bool print_extra_number
= false;
273 setlocale (LC_NUMERIC
, "C");
274 x_strlen
= asprintf (&x_str
, fmt
, x
);
275 setlocale (LC_NUMERIC
, "");
278 x_str
[x_strlen
- layout
.suffix_len
] = '\0';
280 if (xstrtold (x_str
+ layout
.prefix_len
, NULL
, &x_val
, c_strtold
)
284 if (asprintf (&x0_str
, fmt
, x0
) < 0)
286 print_extra_number
= !STREQ (x0_str
, x_str
);
291 if (! print_extra_number
)
295 fputs (separator
, stdout
);
298 fputs (terminator
, stdout
);
302 /* Return the default format given FIRST, STEP, and LAST. */
304 get_default_format (operand first
, operand step
, operand last
)
306 static char format_buf
[sizeof "%0.Lf" + 2 * INT_STRLEN_BOUND (int)];
308 int prec
= MAX (first
.precision
, step
.precision
);
310 if (prec
!= INT_MAX
&& last
.precision
!= INT_MAX
)
314 /* increase first_width by any increased precision in step */
315 size_t first_width
= first
.width
+ (prec
- first
.precision
);
316 /* adjust last_width to use precision from first/step */
317 size_t last_width
= last
.width
+ (prec
- last
.precision
);
318 if (last
.precision
&& prec
== 0)
319 last_width
--; /* don't include space for '.' */
320 if (last
.precision
== 0 && prec
)
321 last_width
++; /* include space for '.' */
322 size_t width
= MAX (first_width
, last_width
);
323 if (width
<= INT_MAX
)
326 sprintf (format_buf
, "%%0%d.%dLf", w
, prec
);
332 sprintf (format_buf
, "%%.%dLf", prec
);
341 main (int argc
, char **argv
)
344 operand first
= { 1, 1, 0 };
345 operand step
= { 1, 1, 0 };
347 struct layout layout
= { 0, 0 };
349 /* The printf(3) format used for output. */
350 char const *format_str
= NULL
;
352 initialize_main (&argc
, &argv
);
353 set_program_name (argv
[0]);
354 setlocale (LC_ALL
, "");
355 bindtextdomain (PACKAGE
, LOCALEDIR
);
356 textdomain (PACKAGE
);
358 atexit (close_stdout
);
363 /* We have to handle negative numbers in the command line but this
364 conflicts with the command line arguments. So explicitly check first
365 whether the next argument looks like a negative number. */
366 while (optind
< argc
)
368 if (argv
[optind
][0] == '-'
369 && ((optc
= argv
[optind
][1]) == '.' || ISDIGIT (optc
)))
371 /* means negative number */
375 optc
= getopt_long (argc
, argv
, "+f:s:w", long_options
, NULL
);
393 case_GETOPT_HELP_CHAR
;
395 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
398 usage (EXIT_FAILURE
);
402 if (argc
- optind
< 1)
404 error (0, 0, _("missing operand"));
405 usage (EXIT_FAILURE
);
408 if (3 < argc
- optind
)
410 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 3]));
411 usage (EXIT_FAILURE
);
415 format_str
= long_double_format (format_str
, &layout
);
417 last
= scan_arg (argv
[optind
++]);
422 last
= scan_arg (argv
[optind
++]);
427 last
= scan_arg (argv
[optind
++]);
431 if (format_str
!= NULL
&& equal_width
)
434 format string may not be specified when printing equal width strings"));
435 usage (EXIT_FAILURE
);
438 if (format_str
== NULL
)
439 format_str
= get_default_format (first
, step
, last
);
441 print_numbers (format_str
, layout
, first
.value
, step
.value
, last
.value
);