Make parameters const where appropriate.
[platform/upstream/coreutils.git] / src / seq.c
1 /* seq - print sequence of numbers to standard output.
2    Copyright (C) 94, 1995 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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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
25 #include "system.h"
26 #include "error.h"
27
28 static double scan_double_arg __P ((const char *arg));
29 static int check_format __P ((const char *format_string));
30 static char *get_width_format __P ((void));
31 static int print_numbers __P ((const char *format_str));
32
33 /* If nonzero print all number with equal width.  */
34 static int equal_width;
35
36 /* The printf(3) format used for output.  */
37 static char *format_str;
38
39 /* The starting number.  */
40 static double first;
41
42 /* The name that this program was run with.  */
43 char *program_name;
44
45 /* The string used to separate two numbers.  */
46 static char *separator;
47
48 /* The string output after all numbers have been output.
49    Usually "\n" or "\0".  */
50 /* FIXME: make this an option.  */
51 static char *terminator = "\n";
52
53 /* If nonzero, display usage information and exit.  */
54 static int show_help;
55
56 /* If nonzero, print the version on standard output and exit.  */
57 static int show_version;
58
59 /* The increment.  */
60 static double step;
61
62 /* The last number.  */
63 static double last;
64
65 static struct option const long_options[] =
66 {
67   { "equal-width", no_argument, NULL, 'w'},
68   { "format", required_argument, NULL, 'f'},
69   { "help", no_argument, &show_help, 1},
70   { "separator", required_argument, NULL, 's'},
71   { "version", no_argument, &show_version, 1},
72   { NULL, 0, NULL, 0}
73 };
74
75 static void
76 usage (int status)
77 {
78   if (status != 0)
79     (void) fprintf (stderr, _("Try `%s --help' for more information.\n"),
80                     program_name);
81   else
82     {
83       (void) printf (_("\
84 Usage: %s [OPTION]... [FIRST [step]] LAST\n\
85 "), program_name);
86       (void) printf (_("\
87 \n\
88   -f, --format FORMAT      use printf(3) style FORMAT (default: %%g)\n\
89       --help               display this help and exit\n\
90   -s, --separator STRING   use STRING for separating numbers (default: \\n)\n\
91       --version            output version information and exit\n\
92   -w, --equal-width        equalize width by padding with leading zeroes\n\
93 \n\
94   FROM, STEP, TO are interpreted as floating point.  STEP should be > 0 if\n\
95   FROM is smaller than TO and vice versa.  When given, the FORMAT argument\n\
96   must contain exactly one of the float output formats %%e, %%f, or %%g.\n\
97 "));
98     }
99   exit (status);
100 }
101
102 int
103 main (int argc, char **argv)
104 {
105   int errs;
106   int optc;
107   int step_is_set;
108
109   program_name = argv[0];
110   setlocale (LC_ALL, "");
111   bindtextdomain (PACKAGE, LOCALEDIR);
112   textdomain (PACKAGE);
113
114   equal_width = 0;
115   format_str = NULL;
116   separator = "\n";
117   first = 1.0;
118   step_is_set = 0;
119
120   /* We have to handle negative numbers in the command line but this
121      conflicts with the command line arguments.  So the getopt mode is
122      REQUIRE_ORDER (the '+' in the format string) and it abort on the
123      first non-option or negative number.  */
124   while ((optc = getopt_long (argc, argv, "+0123456789f:s:w", long_options,
125                               (int *) 0)) != EOF)
126     {
127       if ('0' <= optc && optc <= '9')
128         {
129           /* means negative number */
130           break;
131         }
132
133       switch (optc)
134         {
135         case 0:
136           break;
137
138         case 'f':
139           format_str = optarg;
140           break;
141
142         case 's':
143           separator = optarg;
144           break;
145
146         case 'w':
147           equal_width = 1;
148           break;
149
150         default:
151           usage (1);
152           /* NOTREACHED */
153         }
154     }
155
156   if (show_version)
157     {
158       (void) printf ("seq - %s\n", PACKAGE_VERSION);
159       exit (0);
160     }
161
162   if (show_help)
163     {
164       usage (0);
165       /* NOTREACHED */
166     }
167
168   if (optind >= argc)
169     {
170       error (0, 0, _("too few arguments"));
171       usage (1);
172       /* NOTREACHED */
173     }
174   last = scan_double_arg (argv[optind++]);
175
176   if (optind < argc)
177     {
178       first = last;
179       last = scan_double_arg (argv[optind++]);
180
181       if (optind < argc)
182         {
183           step = last;
184           step_is_set = 1;
185           last = scan_double_arg (argv[optind++]);
186
187           if (optind < argc)
188             {
189               usage (1);
190               /* NOTREACHED */
191             }
192         }
193     }
194
195   if (format_str != NULL && equal_width)
196     {
197       error (0, 0, _("\
198 format string may not be specified when printing equal width strings"));
199       usage (1);
200     }
201
202   if (!step_is_set)
203     {
204       step = first <= last ? 1.0 : -1.0;
205     }
206
207   if (format_str != NULL)
208     {
209       if (!check_format (format_str))
210         {
211           error (0, 0, _("invalid format string: `%s'"), format_str);
212           usage (1);
213         }
214     }
215   else
216     {
217       if (equal_width)
218         format_str = get_width_format ();
219       else
220         format_str = "%g";
221     }
222
223   errs = print_numbers (format_str);
224
225   exit (errs);
226   /* NOTREACHED */
227 }
228
229 /* Read a double value from the command line.
230    Return if the string is correct else signal error.  */
231
232 static double
233 scan_double_arg (const char *arg)
234 {
235   char *end_ptr;
236   double ret_val;
237
238   /* FIXME: use xstrtod?  At least set and check errno.  */
239   ret_val = strtod (arg, &end_ptr);
240   if (end_ptr == arg || *end_ptr != '\0')
241     {
242       error (0, 0, _("invalid float argument: %s"), arg);
243       usage (1);
244       /* NOTREACHED */
245     }
246
247   return ret_val;
248 }
249
250 /* Check whether the format string is valid for a single double
251    argument.
252    Return 0 if not, 1 if correct.  */
253
254 static int
255 check_format (const char *format_string)
256 {
257   while (*format_string != '\0')
258     {
259       if (*format_string == '%')
260         {
261           format_string++;
262           if (*format_string != '%')
263             break;
264         }
265
266       format_string++;
267     }
268   if (*format_string == '\0')
269     return 0;
270
271   format_string += strspn (format_string, "-+#0");
272   if (isdigit (*format_string))
273     {
274       format_string += strspn (format_string, "012345789");
275
276       if (*format_string == '.')
277         format_string += strspn (++format_string, "0123456789");
278     }
279
280   if (*format_string != 'e' && *format_string != 'f' &&
281       *format_string != 'g')
282     return 0;
283
284   format_string++;
285   while (*format_string != '\0')
286     {
287       if (*format_string == '%')
288         {
289           format_string++;
290           if (*format_string != '%')
291             return 0;
292         }
293
294       format_string++;
295     }
296
297   return 1;
298 }
299
300 #if defined (HAVE_RINT) && defined (HAVE_MODF) && defined (HAVE_FLOOR)
301
302 /* Return a printf-style format string with which all selected numbers
303    will format to strings of the same width.  */
304
305 static char *
306 get_width_format ()
307 {
308   static char buffer[256];
309   int full_width;
310   int frac_width;
311   int width1, width2;
312   double max_val;
313   double min_val;
314   double temp;
315
316   if (first > last)
317     {
318       min_val = first - step * floor ((first - last) / step);
319       max_val = first;
320     }
321   else
322     {
323       min_val = first;
324       max_val = first + step * floor ((last - first) / step);
325     }
326
327   (void) sprintf (buffer, "%g", rint (max_val));
328   if (buffer[strspn (buffer, "0123456789")] != '\0')
329     return "%g";
330   width1 = strlen (buffer);
331
332   if (min_val < 0.0)
333     {
334       (void) sprintf (buffer, "%g", rint (min_val));
335       if (buffer[strspn (buffer, "-0123456789")] != '\0')
336         return "%g";
337       width2 = strlen (buffer);
338
339       width1 = width1 > width2 ? width1 : width2;
340     }
341   full_width = width1;
342
343   (void) sprintf (buffer, "%g", 1.0 + modf (min_val, &temp));
344   width1 = strlen (buffer);
345   if (width1 == 1)
346     width1 = 0;
347   else
348     {
349       if (buffer[0] != '1' || buffer[1] != '.' ||
350           buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
351         return "%g";
352       width1 -= 2;
353     }
354
355   (void) sprintf (buffer, "%g", 1.0 + modf (step, &temp));
356   width2 = strlen (buffer);
357   if (width2 == 1)
358     width2 = 0;
359   else
360     {
361       if (buffer[0] != '1' || buffer[1] != '.' ||
362           buffer[2 + strspn (&buffer[2], "0123456789")] != '\0')
363         return "%g";
364       width2 -= 2;
365     }
366   frac_width = width1 > width2 ? width1 : width2;
367
368   if (frac_width)
369     (void) sprintf (buffer, "%%0%d.%df", full_width + 1 + frac_width, frac_width);
370   else
371     (void) sprintf (buffer, "%%0%dg", full_width);
372
373   return buffer;
374 }
375
376 #else   /* one of the math functions rint, modf, floor is missing.  */
377
378 static char *
379 get_width_format (void)
380 {
381   /* We cannot compute the needed information to determine the correct
382      answer.  So we simply return a value that works for all cases.  */
383   return "%g";
384 }
385
386 #endif
387
388 /* Actually print the sequence of numbers in the specified range, with the
389    given or default stepping and format.  */
390 static int
391 print_numbers (const char *format_str)
392 {
393   if (first > last)
394     {
395       int i;
396
397       if (step >= 0)
398         {
399           error (0, 0, _("invalid increment: %g"), step);
400           usage (1);
401           /* NOTREACHED */
402         }
403
404       for (i = 0; /* empty */; i++)
405         {
406           double x = first + i * step;
407           printf (format_str, x);
408
409           if (x <= last)
410             break;
411
412           fputs (separator, stdout);
413         }
414     }
415   else
416     {
417       int i;
418
419       if (step <= 0)
420         {
421           error (0, 0, _("invalid increment: %g"), step);
422           usage (1);
423           /* NOTREACHED */
424         }
425
426       for (i = 0; /* empty */; i++)
427         {
428           double x = first + i * step;
429           printf (format_str, x);
430
431           if (x >= last)
432             break;
433
434           fputs (separator, stdout);
435         }
436     }
437   fputs (terminator, stdout);
438
439   return 0;
440 }