split: port ‘split -n N /dev/null’ better to macOS
[coreutils.git] / src / seq.c
blobfe65670f4abc87b23556fe130b6000dd6b3b7a8f
1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2023 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 3 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Ulrich Drepper. */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "die.h"
26 #include "cl-strtod.h"
27 #include "error.h"
28 #include "quote.h"
29 #include "xstrtod.h"
31 /* Roll our own isfinite/isnan 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
36 #ifndef isnan
37 # define isnan(x) ((x) != (x))
38 #endif
40 /* Limit below which seq_fast has more throughput.
41 Determined with: seq 0 200 inf | pv > /dev/null */
42 #define SEQ_FAST_STEP_LIMIT 200 /* Keep in sync with texinfo description. */
43 #define SEQ_FAST_STEP_LIMIT_DIGITS 3
45 /* The official name of this program (e.g., no 'g' prefix). */
46 #define PROGRAM_NAME "seq"
48 #define AUTHORS proper_name ("Ulrich Drepper")
50 /* True if the locale settings were honored. */
51 static bool locale_ok;
53 /* If true print all number with equal width. */
54 static bool equal_width;
56 /* The string used to separate two numbers. */
57 static char const *separator;
59 /* The string output after all numbers have been output.
60 Usually "\n" or "\0". */
61 static char const terminator[] = "\n";
63 static struct option const long_options[] =
65 { "equal-width", no_argument, NULL, 'w'},
66 { "format", required_argument, NULL, 'f'},
67 { "separator", required_argument, NULL, 's'},
68 {GETOPT_HELP_OPTION_DECL},
69 {GETOPT_VERSION_OPTION_DECL},
70 { NULL, 0, NULL, 0}
73 void
74 usage (int status)
76 if (status != EXIT_SUCCESS)
77 emit_try_help ();
78 else
80 printf (_("\
81 Usage: %s [OPTION]... LAST\n\
82 or: %s [OPTION]... FIRST LAST\n\
83 or: %s [OPTION]... FIRST INCREMENT LAST\n\
84 "), program_name, program_name, program_name);
85 fputs (_("\
86 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
87 "), stdout);
89 emit_mandatory_arg_note ();
91 fputs (_("\
92 -f, --format=FORMAT use printf style floating-point FORMAT\n\
93 -s, --separator=STRING use STRING to separate numbers (default: \\n)\n\
94 -w, --equal-width equalize width by padding with leading zeroes\n\
95 "), stdout);
96 fputs (HELP_OPTION_DESCRIPTION, stdout);
97 fputs (VERSION_OPTION_DESCRIPTION, stdout);
98 fputs (_("\
99 \n\
100 If FIRST or INCREMENT is omitted, it defaults to 1. That is, an\n\
101 omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.\n\
102 The sequence of numbers ends when the sum of the current number and\n\
103 INCREMENT would become greater than LAST.\n\
104 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
105 INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
106 INCREMENT is usually negative if FIRST is greater than LAST.\n\
107 INCREMENT must not be 0; none of FIRST, INCREMENT and LAST may be NaN.\n\
108 "), stdout);
109 fputs (_("\
110 FORMAT must be suitable for printing one argument of type 'double';\n\
111 it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point\n\
112 decimal numbers with maximum precision PREC, and to %g otherwise.\n\
113 "), stdout);
114 emit_ancillary_info (PROGRAM_NAME);
116 exit (status);
119 /* A command-line operand. */
120 struct operand
122 /* Its value, converted to 'long double'. */
123 long double value;
125 /* Its print width, if it were printed out in a form similar to its
126 input form. An input like "-.1" is treated like "-0.1", and an
127 input like "1." is treated like "1", but otherwise widths are
128 left alone. */
129 size_t width;
131 /* Number of digits after the decimal point, or INT_MAX if the
132 number can't easily be expressed as a fixed-point number. */
133 int precision;
135 typedef struct operand operand;
137 /* Description of what a number-generating format will generate. */
138 struct layout
140 /* Number of bytes before and after the number. */
141 size_t prefix_len;
142 size_t suffix_len;
145 /* Read a long double value from the command line.
146 Return if the string is correct else signal error. */
148 static operand
149 scan_arg (char const *arg)
151 operand ret;
153 if (! xstrtold (arg, NULL, &ret.value, cl_strtold))
155 error (0, 0, _("invalid floating point argument: %s"), quote (arg));
156 usage (EXIT_FAILURE);
159 if (isnan (ret.value))
161 error (0, 0, _("invalid %s argument: %s"), quote_n (0, "not-a-number"),
162 quote_n (1, arg));
163 usage (EXIT_FAILURE);
166 /* We don't output spaces or '+' so don't include in width */
167 while (isspace (to_uchar (*arg)) || *arg == '+')
168 arg++;
170 /* Default to auto width and precision. */
171 ret.width = 0;
172 ret.precision = INT_MAX;
174 /* Use no precision (and possibly fast generation) for integers. */
175 char const *decimal_point = strchr (arg, '.');
176 if (! decimal_point && ! strchr (arg, 'p') /* not a hex float */)
177 ret.precision = 0;
179 /* auto set width and precision for decimal inputs. */
180 if (! arg[strcspn (arg, "xX")] && isfinite (ret.value))
182 size_t fraction_len = 0;
183 ret.width = strlen (arg);
185 if (decimal_point)
187 fraction_len = strcspn (decimal_point + 1, "eE");
188 if (fraction_len <= INT_MAX)
189 ret.precision = fraction_len;
190 ret.width += (fraction_len == 0 /* #. -> # */
191 ? -1
192 : (decimal_point == arg /* .# -> 0.# */
193 || ! ISDIGIT (decimal_point[-1]))); /* -.# -> 0.# */
195 char const *e = strchr (arg, 'e');
196 if (! e)
197 e = strchr (arg, 'E');
198 if (e)
200 long exponent = MAX (strtol (e + 1, NULL, 10), -LONG_MAX);
201 ret.precision += exponent < 0 ? -exponent
202 : - MIN (ret.precision, exponent);
203 /* Don't account for e.... in the width since this is not output. */
204 ret.width -= strlen (arg) - (e - arg);
205 /* Adjust the width as per the exponent. */
206 if (exponent < 0)
208 if (decimal_point)
210 if (e == decimal_point + 1) /* undo #. -> # above */
211 ret.width++;
213 else
214 ret.width++;
215 exponent = -exponent;
217 else
219 if (decimal_point && ret.precision == 0 && fraction_len)
220 ret.width--; /* discount space for '.' */
221 exponent -= MIN (fraction_len, exponent);
223 ret.width += exponent;
227 return ret;
230 /* If FORMAT is a valid printf format for a double argument, return
231 its long double equivalent, allocated from dynamic storage, and
232 store into *LAYOUT a description of the output layout; otherwise,
233 report an error and exit. */
235 static char const *
236 long_double_format (char const *fmt, struct layout *layout)
238 size_t i;
239 size_t prefix_len = 0;
240 size_t suffix_len = 0;
241 size_t length_modifier_offset;
242 bool has_L;
244 for (i = 0; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i += (fmt[i] == '%') + 1)
246 if (!fmt[i])
247 die (EXIT_FAILURE, 0,
248 _("format %s has no %% directive"), quote (fmt));
249 prefix_len++;
252 i++;
253 i += strspn (fmt + i, "-+#0 '");
254 i += strspn (fmt + i, "0123456789");
255 if (fmt[i] == '.')
257 i++;
258 i += strspn (fmt + i, "0123456789");
261 length_modifier_offset = i;
262 has_L = (fmt[i] == 'L');
263 i += has_L;
264 if (fmt[i] == '\0')
265 die (EXIT_FAILURE, 0, _("format %s ends in %%"), quote (fmt));
266 if (! strchr ("efgaEFGA", fmt[i]))
267 die (EXIT_FAILURE, 0,
268 _("format %s has unknown %%%c directive"), quote (fmt), fmt[i]);
270 for (i++; ; i += (fmt[i] == '%') + 1)
271 if (fmt[i] == '%' && fmt[i + 1] != '%')
272 die (EXIT_FAILURE, 0, _("format %s has too many %% directives"),
273 quote (fmt));
274 else if (fmt[i])
275 suffix_len++;
276 else
278 size_t format_size = i + 1;
279 char *ldfmt = xmalloc (format_size + 1);
280 memcpy (ldfmt, fmt, length_modifier_offset);
281 ldfmt[length_modifier_offset] = 'L';
282 strcpy (ldfmt + length_modifier_offset + 1,
283 fmt + length_modifier_offset + has_L);
284 layout->prefix_len = prefix_len;
285 layout->suffix_len = suffix_len;
286 return ldfmt;
290 static void
291 io_error (void)
293 /* FIXME: consider option to silently ignore errno=EPIPE */
294 clearerr (stdout);
295 die (EXIT_FAILURE, errno, _("write error"));
298 /* Actually print the sequence of numbers in the specified range, with the
299 given or default stepping and format. */
301 static void
302 print_numbers (char const *fmt, struct layout layout,
303 long double first, long double step, long double last)
305 bool out_of_range = (step < 0 ? first < last : last < first);
307 if (! out_of_range)
309 long double x = first;
310 long double i;
312 for (i = 1; ; i++)
314 long double x0 = x;
315 if (printf (fmt, x) < 0)
316 io_error ();
317 if (out_of_range)
318 break;
319 x = first + i * step;
320 out_of_range = (step < 0 ? x < last : last < x);
322 if (out_of_range)
324 /* If the number just past LAST prints as a value equal
325 to LAST, and prints differently from the previous
326 number, then print the number. This avoids problems
327 with rounding. For example, with the x86 it causes
328 "seq 0 0.000001 0.000003" to print 0.000003 instead
329 of stopping at 0.000002. */
331 bool print_extra_number = false;
332 long double x_val;
333 char *x_str;
334 int x_strlen;
335 if (locale_ok)
336 setlocale (LC_NUMERIC, "C");
337 x_strlen = asprintf (&x_str, fmt, x);
338 if (locale_ok)
339 setlocale (LC_NUMERIC, "");
340 if (x_strlen < 0)
341 xalloc_die ();
342 x_str[x_strlen - layout.suffix_len] = '\0';
344 if (xstrtold (x_str + layout.prefix_len, NULL, &x_val, cl_strtold)
345 && x_val == last)
347 char *x0_str = NULL;
348 int x0_strlen = asprintf (&x0_str, fmt, x0);
349 if (x0_strlen < 0)
350 xalloc_die ();
351 x0_str[x0_strlen - layout.suffix_len] = '\0';
352 print_extra_number = !STREQ (x0_str, x_str);
353 free (x0_str);
356 free (x_str);
357 if (! print_extra_number)
358 break;
361 if (fputs (separator, stdout) == EOF)
362 io_error ();
365 if (fputs (terminator, stdout) == EOF)
366 io_error ();
370 /* Return the default format given FIRST, STEP, and LAST. */
371 static char const *
372 get_default_format (operand first, operand step, operand last)
374 static char format_buf[sizeof "%0.Lf" + 2 * INT_STRLEN_BOUND (int)];
376 int prec = MAX (first.precision, step.precision);
378 if (prec != INT_MAX && last.precision != INT_MAX)
380 if (equal_width)
382 /* increase first_width by any increased precision in step */
383 size_t first_width = first.width + (prec - first.precision);
384 /* adjust last_width to use precision from first/step */
385 size_t last_width = last.width + (prec - last.precision);
386 if (last.precision && prec == 0)
387 last_width--; /* don't include space for '.' */
388 if (last.precision == 0 && prec)
389 last_width++; /* include space for '.' */
390 if (first.precision == 0 && prec)
391 first_width++; /* include space for '.' */
392 size_t width = MAX (first_width, last_width);
393 if (width <= INT_MAX)
395 int w = width;
396 sprintf (format_buf, "%%0%d.%dLf", w, prec);
397 return format_buf;
400 else
402 sprintf (format_buf, "%%.%dLf", prec);
403 return format_buf;
407 return "%Lg";
410 /* The NUL-terminated string S0 of length S_LEN represents a valid
411 non-negative decimal integer. Adjust the string and length so
412 that the pair describe the next-larger value. */
413 static void
414 incr (char **s0, size_t *s_len)
416 char *s = *s0;
417 char *endp = s + *s_len - 1;
421 if ((*endp)++ < '9')
422 return;
423 *endp-- = '0';
425 while (endp >= s);
426 *--(*s0) = '1';
427 ++*s_len;
430 /* Compare A and B (each a NUL-terminated digit string), with lengths
431 given by A_LEN and B_LEN. Return +1 if A < B, -1 if B < A, else 0. */
432 static int
433 cmp (char const *a, size_t a_len, char const *b, size_t b_len)
435 if (a_len < b_len)
436 return -1;
437 if (b_len < a_len)
438 return 1;
439 return (memcmp (a, b, a_len));
442 /* Trim leading 0's from S, but if S is all 0's, leave one.
443 Return a pointer to the trimmed string. */
444 ATTRIBUTE_PURE
445 static char const *
446 trim_leading_zeros (char const *s)
448 char const *p = s;
449 while (*s == '0')
450 ++s;
452 /* If there were only 0's, back up, to leave one. */
453 if (!*s && s != p)
454 --s;
455 return s;
458 /* Print all whole numbers from A to B, inclusive -- to stdout, each
459 followed by a newline. If B < A, return and print nothing.
460 Otherwise, do all the work and exit. */
461 static void
462 seq_fast (char const *a, char const *b, uintmax_t step)
464 bool inf = STREQ (b, "inf");
466 /* Skip past any leading 0's. Without this, our naive cmp
467 function would declare 000 to be larger than 99. */
468 a = trim_leading_zeros (a);
469 b = trim_leading_zeros (b);
471 size_t p_len = strlen (a);
472 size_t q_len = inf ? 0 : strlen (b);
474 /* Allow for at least 31 digits without realloc.
475 1 more than p_len is needed for the inf case. */
476 #define INITIAL_ALLOC_DIGITS 31
477 size_t inc_size = MAX (MAX (p_len + 1, q_len), INITIAL_ALLOC_DIGITS);
478 /* Ensure we only increase by at most 1 digit at buffer boundaries. */
479 static_assert (SEQ_FAST_STEP_LIMIT_DIGITS < INITIAL_ALLOC_DIGITS - 1);
481 /* Copy input strings (incl NUL) to end of new buffers. */
482 char *p0 = xmalloc (inc_size + 1);
483 char *p = memcpy (p0 + inc_size - p_len, a, p_len + 1);
484 char *q;
485 char *q0;
486 if (! inf)
488 q0 = xmalloc (inc_size + 1);
489 q = memcpy (q0 + inc_size - q_len, b, q_len + 1);
491 else
492 q = q0 = NULL;
494 bool ok = inf || cmp (p, p_len, q, q_len) <= 0;
495 if (ok)
497 /* Reduce number of fwrite calls which is seen to
498 give a speed-up of more than 2x over the unbuffered code
499 when printing the first 10^9 integers. */
500 size_t buf_size = MAX (BUFSIZ, (inc_size + 1) * 2);
501 char *buf = xmalloc (buf_size);
502 char const *buf_end = buf + buf_size;
504 char *bufp = buf;
506 /* Write first number to buffer. */
507 bufp = mempcpy (bufp, p, p_len);
509 /* Append separator then number. */
510 while (true)
512 for (uintmax_t n_incr = step; n_incr; n_incr--)
513 incr (&p, &p_len);
515 if (! inf && 0 < cmp (p, p_len, q, q_len))
516 break;
518 *bufp++ = *separator;
520 /* Double up the buffers when needed for the inf case. */
521 if (p_len == inc_size)
523 inc_size *= 2;
524 p0 = xrealloc (p0, inc_size + 1);
525 p = memmove (p0 + p_len, p0, p_len + 1);
527 if (buf_size < (inc_size + 1) * 2)
529 size_t buf_offset = bufp - buf;
530 buf_size = (inc_size + 1) * 2;
531 buf = xrealloc (buf, buf_size);
532 buf_end = buf + buf_size;
533 bufp = buf + buf_offset;
537 bufp = mempcpy (bufp, p, p_len);
538 /* If no place for another separator + number then
539 output buffer so far, and reset to start of buffer. */
540 if (buf_end - (p_len + 1) < bufp)
542 if (fwrite (buf, bufp - buf, 1, stdout) != 1)
543 io_error ();
544 bufp = buf;
548 /* Write any remaining buffered output, and the terminator. */
549 *bufp++ = *terminator;
550 if (fwrite (buf, bufp - buf, 1, stdout) != 1)
551 io_error ();
554 if (ok)
555 exit (EXIT_SUCCESS);
557 free (p0);
558 free (q0);
561 /* Return true if S consists of at least one digit and no non-digits. */
562 ATTRIBUTE_PURE
563 static bool
564 all_digits_p (char const *s)
566 size_t n = strlen (s);
567 return ISDIGIT (s[0]) && n == strspn (s, "0123456789");
571 main (int argc, char **argv)
573 int optc;
574 operand first = { 1, 1, 0 };
575 operand step = { 1, 1, 0 };
576 operand last;
577 struct layout layout = { 0, 0 };
579 /* The printf(3) format used for output. */
580 char const *format_str = NULL;
582 initialize_main (&argc, &argv);
583 set_program_name (argv[0]);
584 locale_ok = !!setlocale (LC_ALL, "");
585 bindtextdomain (PACKAGE, LOCALEDIR);
586 textdomain (PACKAGE);
588 atexit (close_stdout);
590 equal_width = false;
591 separator = "\n";
593 /* We have to handle negative numbers in the command line but this
594 conflicts with the command line arguments. So explicitly check first
595 whether the next argument looks like a negative number. */
596 while (optind < argc)
598 if (argv[optind][0] == '-'
599 && ((optc = argv[optind][1]) == '.' || ISDIGIT (optc)))
601 /* means negative number */
602 break;
605 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
606 if (optc == -1)
607 break;
609 switch (optc)
611 case 'f':
612 format_str = optarg;
613 break;
615 case 's':
616 separator = optarg;
617 break;
619 case 'w':
620 equal_width = true;
621 break;
623 case_GETOPT_HELP_CHAR;
625 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
627 default:
628 usage (EXIT_FAILURE);
632 unsigned int n_args = argc - optind;
633 if (n_args < 1)
635 error (0, 0, _("missing operand"));
636 usage (EXIT_FAILURE);
639 if (3 < n_args)
641 error (0, 0, _("extra operand %s"), quote (argv[optind + 3]));
642 usage (EXIT_FAILURE);
645 if (format_str)
646 format_str = long_double_format (format_str, &layout);
648 if (format_str != NULL && equal_width)
650 error (0, 0, _("format string may not be specified"
651 " when printing equal width strings"));
652 usage (EXIT_FAILURE);
655 /* If the following hold:
656 - no format string, [FIXME: relax this, eventually]
657 - integer start (or no start)
658 - integer end
659 - integer increment <= SEQ_FAST_STEP_LIMIT
660 then use the much more efficient integer-only code,
661 operating on arbitrarily large numbers. */
662 bool fast_step_ok = false;
663 if (n_args != 3
664 || (all_digits_p (argv[optind + 1])
665 && xstrtold (argv[optind + 1], NULL, &step.value, cl_strtold)
666 && 0 < step.value && step.value <= SEQ_FAST_STEP_LIMIT))
667 fast_step_ok = true;
669 if (all_digits_p (argv[optind])
670 && (n_args == 1 || all_digits_p (argv[optind + 1]))
671 && (n_args < 3 || (fast_step_ok
672 && all_digits_p (argv[optind + 2])))
673 && !equal_width && !format_str && strlen (separator) == 1)
675 char const *s1 = n_args == 1 ? "1" : argv[optind];
676 char const *s2 = argv[optind + (n_args - 1)];
677 seq_fast (s1, s2, step.value);
679 /* Upon any failure, let the more general code deal with it. */
682 last = scan_arg (argv[optind++]);
684 if (optind < argc)
686 first = last;
687 last = scan_arg (argv[optind++]);
689 if (optind < argc)
691 step = last;
692 if (step.value == 0)
694 error (0, 0, _("invalid Zero increment value: %s"),
695 quote (argv[optind - 1]));
696 usage (EXIT_FAILURE);
699 last = scan_arg (argv[optind++]);
703 /* Try the fast method again, for integers of the form 1e1 etc.,
704 or "inf" end value. */
705 if (first.precision == 0 && step.precision == 0 && last.precision == 0
706 && isfinite (first.value) && 0 <= first.value && 0 <= last.value
707 && 0 < step.value && step.value <= SEQ_FAST_STEP_LIMIT
708 && !equal_width && !format_str && strlen (separator) == 1)
710 char *s1;
711 char *s2;
712 if (asprintf (&s1, "%0.Lf", first.value) < 0)
713 xalloc_die ();
714 if (! isfinite (last.value))
715 s2 = xstrdup ("inf"); /* Ensure "inf" is used. */
716 else if (asprintf (&s2, "%0.Lf", last.value) < 0)
717 xalloc_die ();
719 if (*s1 != '-' && *s2 != '-')
720 seq_fast (s1, s2, step.value);
722 free (s1);
723 free (s2);
724 /* Upon any failure, let the more general code deal with it. */
727 if (format_str == NULL)
728 format_str = get_default_format (first, step, last);
730 print_numbers (format_str, layout, first.value, step.value, last.value);
732 main_exit (EXIT_SUCCESS);