On Sun, Feb 13, 2022 at 12:07:31PM -0800, Greg Steuck wrote: > Ingo Schwarze <schwa...@usta.de> writes: > > > Hi Todd, > > > > in view of your arguments and sthen@'s OK, i'm also OK with this > > going in. I think a bit of code cleanup and copy editing in the > > manual page may be useful afterwards, but that can be done in the > > tree, no need for playing patch ping pong. > > I noticed that despite the OKs the code didn't submitted. Should we > revive this diff now and continue in the tree? > > I have vested interest: I don't want to fix up lang/ghc test suite which > is full of seq(1). > > Thanks > Greg
here is a port of the plan 9 seq from the last time this came up --- /dev/null Mon Feb 14 10:16:42 2022 +++ seq.c Wed Mar 24 16:24:15 2021 @@ -0,0 +1,135 @@ +/* $OpenBSD$ */ + +/* + * Copyright 2021 Plan 9 Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <err.h> + +double min = 1.0; +double max = 0.0; +double incr = 1.0; +int constant = 0; +int nsteps; +char *format; + +void +usage(void) +{ + fprintf(stderr, "usage: seq [-fformat] [-w] [first [incr]] last\n"); + exit(1); +} + +void +buildfmt(void) +{ + char *dp; + int w, p, maxw, maxp; + static char fmt[16]; + char buf[32]; + double val; + + format = "%g\n"; + if (!constant) + return; + maxw = 0; + maxp = 0; + for (val = min; val <= max; val += incr) { + sprintf(buf, "%g", val); + if (strchr(buf, 'e') != 0) + return; + dp = strchr(buf,'.'); + w = dp == 0 ? strlen(buf) : dp - buf; + p = dp == 0 ? 0 : strlen(strchr(buf,'.') + 1); + if (w > maxw) + maxw = w; + if (p > maxp) + maxp = p; + } + if (maxp > 0) + maxw += maxp + 1; + sprintf(fmt, "%%%d.%df\n", maxw, maxp); + format = fmt; +} + +int +main(int argc, char *argv[]) +{ + int j, n, c; + char buf[256], ffmt[4096]; + double val; + + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + + while ((c = getopt(argc, argv, "wf:")) != -1) + switch (c) { + case 'w': + constant++; + break; + case 'f': + format = optarg; + if (format[strlen(format) - 1] != '\n') { + sprintf(ffmt, "%s\n", format); + format = ffmt; + } + break; + default: + goto out; + } + argc -= optind; + argv += optind; +out: + if (argc < 1 || argc > 3) + usage(); + max = atof(argv[argc - 1]); + if (argc > 1) + min = atof(argv[0]); + if (argc > 2) + incr = atof(argv[1]); + if (incr == 0) + errx(1, "zero increment"); + if (!format) + buildfmt(); + if (incr > 0) { + for (val = min; val <= max; val += incr) { + n = sprintf(buf, format, val); + if (constant) + for (j=0; buf[j] == ' '; j++) + buf[j] = '0'; + write(1, buf, n); + } + } else { + for (val = min; val >= max; val += incr) { + n = sprintf(buf, format, val); + if (constant) + for (j=0; buf[j] == ' '; j++) + buf[j] = '0'; + write(1, buf, n); + } + } + return 0; +} --- /dev/null Mon Feb 14 10:16:46 2022 +++ seq.1 Mon Feb 14 10:16:29 2022 @@ -0,0 +1,84 @@ +.\" $OpenBSD$ +.\" +.\" Copyright 2021 Plan 9 Foundation +.\" +.\" Permission is hereby granted, free of charge, to any person obtaining +.\" a copy of this software and associated documentation files (the +.\" "Software"), to deal in the Software without restriction, including +.\" without limitation the rights to use, copy, modify, merge, publish, +.\" distribute, sublicense, and/or sell copies of the Software, and to +.\" permit persons to whom the Software is furnished to do so, subject to +.\" the following conditions: +.\" +.\" The above copyright notice and this permission notice shall be +.\" included in all copies or substantial portions of the Software. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +.\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +.\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +.\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +.\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +.\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +.\" +.Dd $Mdocdate$ +.Dt SEQ 1 +.Os +.Sh NAME +.Nm seq +.Nd print sequences of numbers +.Sh SYNOPSIS +.Nm seq +.Op Fl w +.Op Fl f Ar format +.Op Ar first Op Ar incr +.Ar last +.Sh DESCRIPTION +.Nm +prints a sequence of numbers, one per line, from +.Ar first +(default 1) to as near +.Ar last +as possible, in increments of +.Ar incr +(default 1). +The loop is: +.sp +.Dl for (val = min; val <= max; val += incr) print val; +.sp +The numbers are interpreted as floating point. +.Pp +Normally integer values are printed as decimal integers. +The options are +.Bl -tag -width Ds +.It Fl f Ar format +Use the +.Xr printf 3 +style +.Ar format +for printing each (floating point) number. +The default is +.Qq %g . +.It Fl w +Equalize the widths of all numbers by padding with +leading zeros as necessary. +Not effective with option +.Fl f , +nor with numbers in exponential notation. +.El +.Sh EXAMPLES +Print +.Qq 0 0.05 0.1 +(on separate lines). +.Dl $ seq 0 .05 .1 +.Pp +Print +.Qq "0.00 0.05 0.10" . +.Dl $ seq -w 0 .05 .1 +.Sh BUGS +Option +.Fl w +always surveys every value in advance. +Thus +.Ql seq -w 1000000000 +is a painful way to get an `infinite' sequence.