.
[coreutils.git] / src / seq.c
blob45ba5a58da521a61c9c2f47e188407a18a0c4ad6
1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2004 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)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Ulrich Drepper. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <math.h>
23 #include <stdio.h>
24 #include <sys/types.h>
26 #include "system.h"
27 #include "c-strtod.h"
28 #include "error.h"
29 #include "xstrtol.h"
30 #include "xstrtod.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "seq"
35 #define AUTHORS "Ulrich Drepper"
37 /* If nonzero print all number with equal width. */
38 static int equal_width;
40 /* The name that this program was run with. */
41 char *program_name;
43 /* The string used to separate two numbers. */
44 static char *separator;
46 /* The string output after all numbers have been output.
47 Usually "\n" or "\0". */
48 /* FIXME: make this an option. */
49 static char *terminator = "\n";
51 /* The representation of the decimal point in the current locale.
52 Always "." if the localeconv function is not supported. */
53 static char *decimal_point = ".";
55 /* The starting number. */
56 static double first;
58 /* The increment. */
59 static double step = 1.0;
61 /* The last number. */
62 static double last;
64 static struct option const long_options[] =
66 { "equal-width", no_argument, NULL, 'w'},
67 { "format", required_argument, NULL, 'f'},
68 { "separator", required_argument, NULL, 's'},
69 {GETOPT_HELP_OPTION_DECL},
70 {GETOPT_VERSION_OPTION_DECL},
71 { NULL, 0, NULL, 0}
74 void
75 usage (int status)
77 if (status != EXIT_SUCCESS)
78 fprintf (stderr, _("Try `%s --help' for more information.\n"),
79 program_name);
80 else
82 printf (_("\
83 Usage: %s [OPTION]... LAST\n\
84 or: %s [OPTION]... FIRST LAST\n\
85 or: %s [OPTION]... FIRST INCREMENT LAST\n\
86 "), program_name, program_name, program_name);
87 fputs (_("\
88 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
89 \n\
90 -f, --format=FORMAT use printf style floating-point FORMAT (default: %g)\n\
91 -s, --separator=STRING use STRING to separate numbers (default: \\n)\n\
92 -w, --equal-width equalize width by padding with leading zeroes\n\
93 "), stdout);
94 fputs (HELP_OPTION_DESCRIPTION, stdout);
95 fputs (VERSION_OPTION_DESCRIPTION, stdout);
96 fputs (_("\
97 \n\
98 If FIRST or INCREMENT is omitted, it defaults to 1. That is, an\n\
99 omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.\n\
100 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
101 INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
102 INCREMENT is usually negative if FIRST is greater than LAST.\n\
103 When given, the FORMAT argument must contain exactly one of\n\
104 the printf-style, floating point output formats %e, %f, %g\n\
105 "), stdout);
106 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
108 exit (status);
111 /* Read a double value from the command line.
112 Return if the string is correct else signal error. */
114 static double
115 scan_double_arg (const char *arg)
117 double ret_val;
119 if (xstrtod (arg, NULL, &ret_val, c_strtod))
121 error (0, 0, _("invalid floating point argument: %s"), arg);
122 usage (EXIT_FAILURE);
125 return ret_val;
128 /* Check whether the format string is valid for a single `double'
129 argument. Return 0 if not, 1 if correct. */
131 static int
132 valid_format (const char *fmt)
134 while (*fmt != '\0')
136 if (*fmt == '%')
138 fmt++;
139 if (*fmt != '%')
140 break;
143 fmt++;
145 if (*fmt == '\0')
146 return 0;
148 fmt += strspn (fmt, "-+#0 '");
149 if (ISDIGIT (*fmt) || *fmt == '.')
151 fmt += strspn (fmt, "0123456789");
153 if (*fmt == '.')
155 ++fmt;
156 fmt += strspn (fmt, "0123456789");
160 if (!(*fmt == 'e' || *fmt == 'f' || *fmt == 'g'))
161 return 0;
163 fmt++;
164 while (*fmt != '\0')
166 if (*fmt == '%')
168 fmt++;
169 if (*fmt != '%')
170 return 0;
173 fmt++;
176 return 1;
179 /* Actually print the sequence of numbers in the specified range, with the
180 given or default stepping and format. */
181 static void
182 print_numbers (const char *fmt)
184 double i;
186 for (i = 0; /* empty */; i++)
188 double x = first + i * step;
189 if (step < 0 ? x < last : last < x)
190 break;
191 if (i)
192 fputs (separator, stdout);
193 printf (fmt, x);
196 if (i)
197 fputs (terminator, stdout);
200 #if HAVE_RINT && HAVE_MODF && HAVE_FLOOR
202 /* Return a printf-style format string with which all selected numbers
203 will format to strings of the same width. */
205 static char *
206 get_width_format ()
208 static char buffer[256];
209 int full_width;
210 int frac_width;
211 int width1, width2;
212 double max_val;
213 double min_val;
214 double temp;
216 if (first > last)
218 min_val = first - step * floor ((first - last) / step);
219 max_val = first;
221 else
223 min_val = first;
224 max_val = first + step * floor ((last - first) / step);
227 sprintf (buffer, "%g", rint (max_val));
228 if (buffer[strspn (buffer, "-0123456789")] != '\0')
229 return "%g";
230 width1 = strlen (buffer);
232 if (min_val < 0.0)
234 double int_min_val = rint (min_val);
235 sprintf (buffer, "%g", int_min_val);
236 if (buffer[strspn (buffer, "-0123456789")] != '\0')
237 return "%g";
238 /* On some systems, `seq -w -.1 .1 .1' results in buffer being `-0'.
239 On others, it is just `0'. The former results in better output. */
240 width2 = (int_min_val == 0 ? 2 : strlen (buffer));
242 width1 = width1 > width2 ? width1 : width2;
244 full_width = width1;
246 sprintf (buffer, "%g", 1.0 + modf (fabs (min_val), &temp));
247 width1 = strlen (buffer);
248 if (width1 == 1)
249 width1 = 0;
250 else
252 if (buffer[0] != '1'
253 /* FIXME: assumes that decimal_point is a single character
254 string. */
255 || buffer[1] != decimal_point[0]
256 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
257 return "%g";
258 width1 -= 2;
261 sprintf (buffer, "%g", 1.0 + modf (fabs (step), &temp));
262 width2 = strlen (buffer);
263 if (width2 == 1)
264 width2 = 0;
265 else
267 if (buffer[0] != '1'
268 /* FIXME: assumes that decimal_point is a single character
269 string. */
270 || buffer[1] != decimal_point[0]
271 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
272 return "%g";
273 width2 -= 2;
275 frac_width = width1 > width2 ? width1 : width2;
277 if (frac_width)
278 sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
279 else
280 sprintf (buffer, "%%0%dg", full_width);
282 return buffer;
285 #else /* one of the math functions rint, modf, floor is missing. */
287 static char *
288 get_width_format (void)
290 /* We cannot compute the needed information to determine the correct
291 answer. So we simply return a value that works for all cases. */
292 return "%g";
295 #endif
298 main (int argc, char **argv)
300 int optc;
302 /* The printf(3) format used for output. */
303 char *format_str = NULL;
305 initialize_main (&argc, &argv);
306 program_name = argv[0];
307 setlocale (LC_ALL, "");
308 bindtextdomain (PACKAGE, LOCALEDIR);
309 textdomain (PACKAGE);
311 atexit (close_stdout);
313 equal_width = 0;
314 separator = "\n";
315 first = 1.0;
317 /* Figure out the locale's idea of a decimal point. */
318 #if HAVE_LOCALECONV
320 struct lconv *locale;
322 locale = localeconv ();
323 /* Paranoia. */
324 if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
325 decimal_point = locale->decimal_point;
327 #endif
329 /* We have to handle negative numbers in the command line but this
330 conflicts with the command line arguments. So explicitly check first
331 whether the next argument looks like a negative number. */
332 while (optind < argc)
334 if (argv[optind][0] == '-'
335 && ((optc = argv[optind][1]) == decimal_point[0]
336 || ISDIGIT (optc)))
338 /* means negative number */
339 break;
342 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
343 if (optc == -1)
344 break;
346 switch (optc)
348 case 0:
349 break;
351 case 'f':
352 format_str = optarg;
353 break;
355 case 's':
356 separator = optarg;
357 break;
359 case 'w':
360 equal_width = 1;
361 break;
363 case_GETOPT_HELP_CHAR;
365 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
367 default:
368 usage (EXIT_FAILURE);
372 if (argc - optind < 1)
374 error (0, 0, _("too few arguments"));
375 usage (EXIT_FAILURE);
378 if (3 < argc - optind)
380 error (0, 0, _("too many arguments"));
381 usage (EXIT_FAILURE);
384 if (format_str && !valid_format (format_str))
386 error (0, 0, _("invalid format string: `%s'"), format_str);
387 usage (EXIT_FAILURE);
390 last = scan_double_arg (argv[optind++]);
392 if (optind < argc)
394 first = last;
395 last = scan_double_arg (argv[optind++]);
397 if (optind < argc)
399 step = last;
400 last = scan_double_arg (argv[optind++]);
404 if (format_str != NULL && equal_width)
406 error (0, 0, _("\
407 format string may not be specified when printing equal width strings"));
408 usage (EXIT_FAILURE);
411 if (format_str == NULL)
413 if (equal_width)
414 format_str = get_width_format ();
415 else
416 format_str = "%g";
419 print_numbers (format_str);
421 exit (EXIT_SUCCESS);