* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / seq.c
blob997fd730dedf3a3b304bd64d893e285d92ccec89
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)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Ulrich Drepper. */
20 #include <config.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "c-strtod.h"
27 #include "error.h"
28 #include "quote.h"
29 #include "xstrtod.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. */
33 #ifndef isfinite
34 # define isfinite(x) ((x) * 0 == 0)
35 #endif
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. */
46 char *program_name;
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},
63 { NULL, 0, NULL, 0}
66 void
67 usage (int status)
69 if (status != EXIT_SUCCESS)
70 fprintf (stderr, _("Try `%s --help' for more information.\n"),
71 program_name);
72 else
74 printf (_("\
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);
79 fputs (_("\
80 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
81 \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\
85 "), stdout);
86 fputs (HELP_OPTION_DESCRIPTION, stdout);
87 fputs (VERSION_OPTION_DESCRIPTION, stdout);
88 fputs (_("\
89 \n\
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\
98 "), stdout);
99 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
101 exit (status);
104 /* A command-line operand. */
105 struct operand
107 /* Its value, converted to 'long double'. */
108 long double value;
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
113 left alone. */
114 size_t width;
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. */
118 int precision;
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. */
125 static operand
126 scan_arg (const char *arg)
128 operand ret;
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, '.');
142 if (! decimal_point)
143 ret.precision = 0;
144 else
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
150 ? -1
151 : (decimal_point == arg
152 || ! ISDIGIT (decimal_point[-1])));
156 return ret;
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. */
163 static char const *
164 long_double_format (char const *fmt)
166 size_t i;
167 size_t prefix_len;
168 bool has_L;
170 for (i = 0; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i++)
171 if (! fmt[i])
172 return NULL;
174 i++;
175 i += strspn (fmt + i, "-+#0 '");
176 i += strspn (fmt + i, "0123456789");
177 if (fmt[i] == '.')
179 i++;
180 i += strspn (fmt + i, "0123456789");
183 prefix_len = i;
184 has_L = (fmt[i] == 'L');
185 i += has_L;
186 if (! strchr ("efgaEFGA", fmt[i]))
187 return NULL;
189 for (i++; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i++)
190 if (! fmt[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);
197 return ldfmt;
200 return NULL;
203 /* Actually print the sequence of numbers in the specified range, with the
204 given or default stepping and format. */
206 static void
207 print_numbers (char const *fmt,
208 long double first, long double step, long double last)
210 long double i;
212 for (i = 0; /* empty */; i++)
214 long double x = first + i * step;
215 if (step < 0 ? x < last : last < x)
216 break;
217 if (i)
218 fputs (separator, stdout);
219 printf (fmt, x);
222 if (i)
223 fputs (terminator, stdout);
226 /* Return the default format given FIRST, STEP, and LAST. */
227 static char const *
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)
236 if (equal_width)
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)
246 int w = width;
247 sprintf (format_buf, "%%0%d.%dLf", w, prec);
248 return format_buf;
252 else
254 sprintf (format_buf, "%%.%dLf", prec);
255 return format_buf;
259 return "%Lg";
263 main (int argc, char **argv)
265 int optc;
266 operand first = { 1, 1, 0 };
267 operand step = { 1, 1, 0 };
268 operand last;
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);
281 equal_width = false;
282 separator = "\n";
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 */
293 break;
296 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
297 if (optc == -1)
298 break;
300 switch (optc)
302 case 'f':
303 format_str = optarg;
304 break;
306 case 's':
307 separator = optarg;
308 break;
310 case 'w':
311 equal_width = true;
312 break;
314 case_GETOPT_HELP_CHAR;
316 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
318 default:
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);
335 if (format_str)
337 char const *f = long_double_format (format_str);
338 if (! f)
340 error (0, 0, _("invalid format string: %s"), quote (format_str));
341 usage (EXIT_FAILURE);
343 format_str = f;
346 last = scan_arg (argv[optind++]);
348 if (optind < argc)
350 first = last;
351 last = scan_arg (argv[optind++]);
353 if (optind < argc)
355 step = last;
356 last = scan_arg (argv[optind++]);
360 if (format_str != NULL && equal_width)
362 error (0, 0, _("\
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);
372 exit (EXIT_SUCCESS);