1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 94, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Ulrich Drepper. */
28 static double scan_double_arg
__P ((char *arg
));
29 static int check_format
__P ((char *format_string
));
30 static char *get_width_format
__P ((void));
31 static int print_numbers
__P ((char *format_str
));
33 /* If nonzero print all number with equal width. */
34 static int equal_width
;
36 /* The printf(3) format used for output. */
37 static char *format_str
;
39 /* The starting number. */
42 /* The name that this program was run with. */
45 /* The string used to separate two number. */
46 static char *separator
;
48 /* If nonzero, display usage information and exit. */
51 /* If nonzero, print the version on standard output and exit. */
52 static int show_version
;
57 /* The last number. */
60 static struct option
const long_options
[] =
62 { "equal-width", no_argument
, NULL
, 'w'},
63 { "format", required_argument
, NULL
, 'f'},
64 { "help", no_argument
, &show_help
, 1},
65 { "separator", required_argument
, NULL
, 's'},
66 { "version", no_argument
, &show_version
, 1},
74 (void) fprintf (stderr
, _("Try `%s --help' for more information.\n"),
79 Usage: %s [OPTION]... [from [step]] to\n\
83 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
84 --help display this help and exit\n\
85 -s, --separator STRING use STRING for separating numbers (default: \\n)\n\
86 --version output version information and exit\n\
87 -w, --equal-width equalize width by padding with leading zeroes\n\
89 FROM, STEP, TO are interpreted as floating point. STEP should be > 0 if\n\
90 FROM is smaller than TO and vice versa. When given, the FORMAT argument\n\
91 must contain exactly one of the float output formats %%e, %%f, or %%g.\n\
98 main (int argc
, char **argv
)
104 program_name
= argv
[0];
105 setlocale (LC_ALL
, "");
106 bindtextdomain (PACKAGE
, LOCALEDIR
);
107 textdomain (PACKAGE
);
115 /* We have to handle negative numbers in the command line but this
116 conflicts with the command line arguments. So the getopt mode is
117 REQUIRE_ORDER (the '+' in the format string) and it abort on the
118 first non-option or negative number. */
119 while ((optc
= getopt_long (argc
, argv
, "+0123456789f:s:w", long_options
,
122 if ('0' <= optc
&& optc
<= '9')
124 /* means negative number */
153 (void) printf ("seq - %s\n", PACKAGE_VERSION
);
165 error (0, 0, _("too few arguments"));
169 last
= scan_double_arg (argv
[optind
++]);
174 last
= scan_double_arg (argv
[optind
++]);
180 last
= scan_double_arg (argv
[optind
++]);
190 if (format_str
!= NULL
&& equal_width
)
193 format string may not be specified when printing equal width strings"));
199 step
= from
<= last
? 1.0 : -1.0;
202 if (format_str
!= NULL
)
204 if (!check_format (format_str
))
206 error (0, 0, _("invalid format string: `%s'"), format_str
);
213 format_str
= get_width_format ();
218 errs
= print_numbers (format_str
);
224 /* Read a double value from the command line.
225 Return if the string is correct else signal error. */
228 scan_double_arg (char *arg
)
233 /* FIXME: use xstrtod? At least set and check errno. */
234 ret_val
= strtod (arg
, &end_ptr
);
235 if (end_ptr
== arg
|| *end_ptr
!= '\0')
237 error (0, 0, _("invalid float argument: %s"), arg
);
245 /* Check whether the format string is valid for a single double
247 Return 0 if not, 1 if correct. */
250 check_format (char *format_string
)
252 while (*format_string
!= '\0')
254 if (*format_string
== '%')
257 if (*format_string
!= '%')
263 if (*format_string
== '\0')
266 format_string
+= strspn (format_string
, "-+#0");
267 if (isdigit (*format_string
))
269 format_string
+= strspn (format_string
, "012345789");
271 if (*format_string
== '.')
272 format_string
+= strspn (++format_string
, "0123456789");
275 if (*format_string
!= 'e' && *format_string
!= 'f' &&
276 *format_string
!= 'g')
280 while (*format_string
!= '\0')
282 if (*format_string
== '%')
285 if (*format_string
!= '%')
295 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
297 /* Return a printf-style format string with which all selected numbers
298 will format to strings of the same width. */
303 static char buffer
[256];
313 min_val
= from
- step
* floor ((from
- last
) / step
);
319 max_val
= from
+ step
* floor ((last
- from
) / step
);
322 (void) sprintf (buffer
, "%g", rint (max_val
));
323 if (buffer
[strspn (buffer
, "0123456789")] != '\0')
325 width1
= strlen (buffer
);
329 (void) sprintf (buffer
, "%g", rint (min_val
));
330 if (buffer
[strspn (buffer
, "-0123456789")] != '\0')
332 width2
= strlen (buffer
);
334 width1
= width1
> width2
? width1
: width2
;
338 (void) sprintf (buffer
, "%g", 1.0 + modf (min_val
, &temp
));
339 width1
= strlen (buffer
);
344 if (buffer
[0] != '1' || buffer
[1] != '.' ||
345 buffer
[2 + strspn (&buffer
[2], "0123456789")] != '\0')
350 (void) sprintf (buffer
, "%g", 1.0 + modf (step
, &temp
));
351 width2
= strlen (buffer
);
356 if (buffer
[0] != '1' || buffer
[1] != '.' ||
357 buffer
[2 + strspn (&buffer
[2], "0123456789")] != '\0')
361 frac_width
= width1
> width2
? width1
: width2
;
364 (void) sprintf (buffer
, "%%0%d.%df", full_width
+ 1 + frac_width
, frac_width
);
366 (void) sprintf (buffer
, "%%0%dg", full_width
);
371 #else /* one of the math functions rint, modf, floor is missing. */
374 get_width_format (void)
376 /* We cannot compute the needed information to determine the correct
377 answer. So we simply return a value that works for all cases. */
383 /* Actually print the sequence of numbers in the specified range, with the
384 given or default stepping and format. */
386 print_numbers (char *format_str
)
392 error (0, 0, _("invalid increment: %g"), step
);
399 (void) printf (format_str
, from
);
401 /* FIXME: don't increment!!! Use `first + i * step'. */
406 (void) fputs (separator
, stdout
);
413 error (0, 0, _("invalid increment: %g"), step
);
420 (void) printf (format_str
, from
);
422 /* FIXME: don't increment!!! Use `first + i * step'. */
427 (void) fputs (separator
, stdout
);