source: trunk/coreutils/src/seq.c@ 2662

Last change on this file since 2662 was 2554, checked in by bird, 20 years ago

coretuils-5.94

File size: 9.5 KB
Line 
1/* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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. */
17
18/* Written by Ulrich Drepper. */
19
20#include <config.h>
21#include <getopt.h>
22#include <math.h>
23#include <stdio.h>
24#include <sys/types.h>
25
26#include "system.h"
27#include "c-strtod.h"
28#include "error.h"
29#include "quote.h"
30#include "xstrtol.h"
31#include "xstrtod.h"
32
33/* The official name of this program (e.g., no `g' prefix). */
34#define PROGRAM_NAME "seq"
35
36#define AUTHORS "Ulrich Drepper"
37
38/* If true print all number with equal width. */
39static bool equal_width;
40
41/* The name that this program was run with. */
42char *program_name;
43
44/* The string used to separate two numbers. */
45static char *separator;
46
47/* The string output after all numbers have been output.
48 Usually "\n" or "\0". */
49/* FIXME: make this an option. */
50static char *terminator = "\n";
51
52/* The representation of the decimal point in the current locale. */
53static char decimal_point;
54
55/* The starting number. */
56static double first;
57
58/* The increment. */
59static double step = 1.0;
60
61/* The last number. */
62static double last;
63
64static struct option const long_options[] =
65{
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}
72};
73
74void
75usage (int status)
76{
77 if (status != EXIT_SUCCESS)
78 fprintf (stderr, _("Try `%s --help' for more information.\n"),
79 program_name);
80 else
81 {
82 printf (_("\
83Usage: %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 (_("\
88Print 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\
98If FIRST or INCREMENT is omitted, it defaults to 1. That is, an\n\
99omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.\n\
100FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
101INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
102INCREMENT is usually negative if FIRST is greater than LAST.\n\
103When given, the FORMAT argument must contain exactly one of\n\
104the printf-style, floating point output formats %e, %f, %g\n\
105"), stdout);
106 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
107 }
108 exit (status);
109}
110
111/* Read a double value from the command line.
112 Return if the string is correct else signal error. */
113
114static double
115scan_double_arg (const char *arg)
116{
117 double ret_val;
118
119 if (! xstrtod (arg, NULL, &ret_val, c_strtod))
120 {
121 error (0, 0, _("invalid floating point argument: %s"), arg);
122 usage (EXIT_FAILURE);
123 }
124
125 return ret_val;
126}
127
128/* Return true if the format string is valid for a single `double'
129 argument. */
130
131static bool
132valid_format (const char *fmt)
133{
134 while (*fmt != '\0')
135 {
136 if (*fmt == '%')
137 {
138 fmt++;
139 if (*fmt != '%')
140 break;
141 }
142
143 fmt++;
144 }
145 if (*fmt == '\0')
146 return false;
147
148 fmt += strspn (fmt, "-+#0 '");
149 if (ISDIGIT (*fmt) || *fmt == '.')
150 {
151 fmt += strspn (fmt, "0123456789");
152
153 if (*fmt == '.')
154 {
155 ++fmt;
156 fmt += strspn (fmt, "0123456789");
157 }
158 }
159
160 if (!(*fmt == 'e' || *fmt == 'f' || *fmt == 'g'))
161 return false;
162
163 fmt++;
164 while (*fmt != '\0')
165 {
166 if (*fmt == '%')
167 {
168 fmt++;
169 if (*fmt != '%')
170 return false;
171 }
172
173 fmt++;
174 }
175
176 return true;
177}
178
179/* Actually print the sequence of numbers in the specified range, with the
180 given or default stepping and format. */
181static void
182print_numbers (const char *fmt)
183{
184 double i;
185
186 for (i = 0; /* empty */; i++)
187 {
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);
194 }
195
196 if (i)
197 fputs (terminator, stdout);
198}
199
200#if HAVE_RINT && HAVE_MODF && HAVE_FLOOR
201
202/* Return a printf-style format string with which all selected numbers
203 will format to strings of the same width. */
204
205static char *
206get_width_format (void)
207{
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;
215
216 if (first > last)
217 {
218 min_val = first - step * floor ((first - last) / step);
219 max_val = first;
220 }
221 else
222 {
223 min_val = first;
224 max_val = first + step * floor ((last - first) / step);
225 }
226
227 sprintf (buffer, "%g", rint (max_val));
228 if (buffer[strspn (buffer, "-0123456789")] != '\0')
229 return "%g";
230 width1 = strlen (buffer);
231
232 if (min_val < 0.0)
233 {
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));
241
242 width1 = width1 > width2 ? width1 : width2;
243 }
244 full_width = width1;
245
246 sprintf (buffer, "%g", 1.0 + modf (fabs (min_val), &temp));
247 width1 = strlen (buffer);
248 if (width1 == 1)
249 width1 = 0;
250 else
251 {
252 if (buffer[0] != '1'
253 || buffer[1] != decimal_point
254 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
255 return "%g";
256 width1 -= 2;
257 }
258
259 sprintf (buffer, "%g", 1.0 + modf (fabs (step), &temp));
260 width2 = strlen (buffer);
261 if (width2 == 1)
262 width2 = 0;
263 else
264 {
265 if (buffer[0] != '1'
266 || buffer[1] != decimal_point
267 || buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
268 return "%g";
269 width2 -= 2;
270 }
271 frac_width = width1 > width2 ? width1 : width2;
272
273 if (frac_width)
274 sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
275 else
276 sprintf (buffer, "%%0%dg", full_width);
277
278 return buffer;
279}
280
281#else /* one of the math functions rint, modf, floor is missing. */
282
283static char *
284get_width_format (void)
285{
286 /* We cannot compute the needed information to determine the correct
287 answer. So we simply return a value that works for all cases. */
288 return "%g";
289}
290
291#endif
292
293int
294main (int argc, char **argv)
295{
296 int optc;
297
298 /* The printf(3) format used for output. */
299 char *format_str = NULL;
300
301 initialize_main (&argc, &argv);
302 program_name = argv[0];
303 setlocale (LC_ALL, "");
304 bindtextdomain (PACKAGE, LOCALEDIR);
305 textdomain (PACKAGE);
306
307 atexit (close_stdout);
308
309 equal_width = false;
310 separator = "\n";
311 first = 1.0;
312
313 /* Get locale's representation of the decimal point. */
314 {
315 struct lconv const *locale = localeconv ();
316
317 /* If the locale doesn't define a decimal point, or if the decimal
318 point is multibyte, use the C locale's decimal point. FIXME:
319 add support for multibyte decimal points. */
320 decimal_point = locale->decimal_point[0];
321 if (! decimal_point || locale->decimal_point[1])
322 decimal_point = '.';
323 }
324
325 /* We have to handle negative numbers in the command line but this
326 conflicts with the command line arguments. So explicitly check first
327 whether the next argument looks like a negative number. */
328 while (optind < argc)
329 {
330 if (argv[optind][0] == '-'
331 && ((optc = argv[optind][1]) == decimal_point
332 || ISDIGIT (optc)))
333 {
334 /* means negative number */
335 break;
336 }
337
338 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
339 if (optc == -1)
340 break;
341
342 switch (optc)
343 {
344 case 'f':
345 format_str = optarg;
346 break;
347
348 case 's':
349 separator = optarg;
350 break;
351
352 case 'w':
353 equal_width = true;
354 break;
355
356 case_GETOPT_HELP_CHAR;
357
358 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
359
360 default:
361 usage (EXIT_FAILURE);
362 }
363 }
364
365 if (argc - optind < 1)
366 {
367 error (0, 0, _("missing operand"));
368 usage (EXIT_FAILURE);
369 }
370
371 if (3 < argc - optind)
372 {
373 error (0, 0, _("extra operand %s"), quote (argv[optind + 3]));
374 usage (EXIT_FAILURE);
375 }
376
377 if (format_str && !valid_format (format_str))
378 {
379 error (0, 0, _("invalid format string: %s"), quote (format_str));
380 usage (EXIT_FAILURE);
381 }
382
383 last = scan_double_arg (argv[optind++]);
384
385 if (optind < argc)
386 {
387 first = last;
388 last = scan_double_arg (argv[optind++]);
389
390 if (optind < argc)
391 {
392 step = last;
393 last = scan_double_arg (argv[optind++]);
394 }
395 }
396
397 if (format_str != NULL && equal_width)
398 {
399 error (0, 0, _("\
400format string may not be specified when printing equal width strings"));
401 usage (EXIT_FAILURE);
402 }
403
404 if (format_str == NULL)
405 {
406 if (equal_width)
407 format_str = get_width_format ();
408 else
409 format_str = "%g";
410 }
411
412 print_numbers (format_str);
413
414 exit (EXIT_SUCCESS);
415}
Note: See TracBrowser for help on using the repository browser.