1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-1999 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>
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "seq"
33 #define AUTHORS "Ulrich Drepper"
35 static double scan_double_arg
PARAMS ((const char *arg
));
36 static int check_format
PARAMS ((const char *format_string
));
37 static char *get_width_format
PARAMS ((void));
38 static int print_numbers
PARAMS ((const char *format_str
));
40 /* If nonzero print all number with equal width. */
41 static int equal_width
;
43 /* The printf(3) format used for output. */
44 static char *format_str
;
46 /* The starting number. */
49 /* The name that this program was run with. */
52 /* The string used to separate two numbers. */
53 static char *separator
;
55 /* The string output after all numbers have been output.
56 Usually "\n" or "\0". */
57 /* FIXME: make this an option. */
58 static char *terminator
= "\n";
60 /* The representation of the decimal point in the current locale.
61 Always "." if the localeconv function is not supported. */
62 static char *decimal_point
= ".";
67 /* The last number. */
70 static struct option
const long_options
[] =
72 { "equal-width", no_argument
, NULL
, 'w'},
73 { "format", required_argument
, NULL
, 'f'},
74 { "separator", required_argument
, NULL
, 's'},
75 {GETOPT_HELP_OPTION_DECL
},
76 {GETOPT_VERSION_OPTION_DECL
},
84 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
89 Usage: %s [OPTION]... LAST\n\
90 or: %s [OPTION]... FIRST LAST\n\
91 or: %s [OPTION]... FIRST INCREMENT LAST\n\
92 "), program_name
, program_name
, program_name
);
94 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
96 -f, --format FORMAT use printf(3) style FORMAT (default: %%g)\n\
97 -s, --separator STRING use STRING to separate numbers (default: \\n)\n\
98 -w, --equal-width equalize width by padding with leading zeroes\n\
99 --help display this help and exit\n\
100 --version output version information and exit\n\
102 If FIRST or INCREMENT is omitted, it defaults to 1.\n\
103 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
104 INCREMENT should be positive if FIRST is smaller than LAST, and negative\n\
105 otherwise. When given, the FORMAT argument must contain exactly one of\n\
106 the printf-style, floating point output formats %%e, %%f, or %%g.\n\
108 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
114 main (int argc
, char **argv
)
120 program_name
= argv
[0];
121 setlocale (LC_ALL
, "");
122 bindtextdomain (PACKAGE
, LOCALEDIR
);
123 textdomain (PACKAGE
);
131 /* Figure out the locale's idea of a decimal point. */
132 #ifdef HAVE_LOCALECONV
134 struct lconv
*locale
;
136 locale
= localeconv ();
138 if (locale
&& locale
->decimal_point
&& locale
->decimal_point
[0] != '\0')
139 decimal_point
= locale
->decimal_point
;
143 /* We have to handle negative numbers in the command line but this
144 conflicts with the command line arguments. So explicitly check first
145 whether the next argument looks like a negative number. */
146 while (optind
< argc
)
148 if (argv
[optind
][0] == '-'
149 && ((optc
= argv
[optind
][1]) == decimal_point
[0]
152 /* means negative number */
156 optc
= getopt_long (argc
, argv
, "+f:s:w", long_options
, NULL
);
177 case_GETOPT_HELP_CHAR
;
179 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
189 error (0, 0, _("too few arguments"));
193 last
= scan_double_arg (argv
[optind
++]);
198 last
= scan_double_arg (argv
[optind
++]);
204 last
= scan_double_arg (argv
[optind
++]);
214 if (format_str
!= NULL
&& equal_width
)
217 format string may not be specified when printing equal width strings"));
223 step
= first
<= last
? 1.0 : -1.0;
226 if (format_str
!= NULL
)
228 if (!check_format (format_str
))
230 error (0, 0, _("invalid format string: `%s'"), format_str
);
237 format_str
= get_width_format ();
242 errs
= print_numbers (format_str
);
248 /* Read a double value from the command line.
249 Return if the string is correct else signal error. */
252 scan_double_arg (const char *arg
)
256 if (xstrtod (arg
, NULL
, &ret_val
))
258 error (0, 0, _("invalid floating point argument: %s"), arg
);
266 /* Check whether the format string is valid for a single double
268 Return 0 if not, 1 if correct. */
271 check_format (const char *fmt
)
287 fmt
+= strspn (fmt
, "-+#0");
290 fmt
+= strspn (fmt
, "0123456789");
293 fmt
+= strspn (++fmt
, "0123456789");
296 if (*fmt
!= 'e' && *fmt
!= 'f' && *fmt
!= 'g')
315 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
317 /* Return a printf-style format string with which all selected numbers
318 will format to strings of the same width. */
323 static char buffer
[256];
333 min_val
= first
- step
* floor ((first
- last
) / step
);
339 max_val
= first
+ step
* floor ((last
- first
) / step
);
342 sprintf (buffer
, "%g", rint (max_val
));
343 if (buffer
[strspn (buffer
, "0123456789")] != '\0')
345 width1
= strlen (buffer
);
349 sprintf (buffer
, "%g", rint (min_val
));
350 if (buffer
[strspn (buffer
, "-0123456789")] != '\0')
352 width2
= strlen (buffer
);
354 width1
= width1
> width2
? width1
: width2
;
358 sprintf (buffer
, "%g", 1.0 + modf (fabs (min_val
), &temp
));
359 width1
= strlen (buffer
);
365 /* FIXME: assumes that decimal_point is a single character
367 || buffer
[1] != decimal_point
[0]
368 || buffer
[2 + strspn (&buffer
[2], "0123456789")] != '\0')
373 sprintf (buffer
, "%g", 1.0 + modf (fabs (step
), &temp
));
374 width2
= strlen (buffer
);
380 /* FIXME: assumes that decimal_point is a single character
382 || buffer
[1] != decimal_point
[0]
383 || buffer
[2 + strspn (&buffer
[2], "0123456789")] != '\0')
387 frac_width
= width1
> width2
? width1
: width2
;
390 sprintf (buffer
, "%%0%d.%df", full_width
+ 1 + frac_width
, frac_width
);
392 sprintf (buffer
, "%%0%dg", full_width
);
397 #else /* one of the math functions rint, modf, floor is missing. */
400 get_width_format (void)
402 /* We cannot compute the needed information to determine the correct
403 answer. So we simply return a value that works for all cases. */
409 /* Actually print the sequence of numbers in the specified range, with the
410 given or default stepping and format. */
412 print_numbers (const char *fmt
)
421 _("when the starting value is larger than the limit,\n\
422 the increment must be negative"));
428 for (i
= 1; /* empty */; i
++)
430 double x
= first
+ i
* step
;
435 fputs (separator
, stdout
);
446 _("when the starting value is smaller than the limit,\n\
447 the increment must be positive"));
453 for (i
= 1; /* empty */; i
++)
455 double x
= first
+ i
* step
;
460 fputs (separator
, stdout
);
461 printf (format_str
, x
);
464 fputs (terminator
, stdout
);