diff options
author | rsc <devnull@localhost> | 2003-11-23 18:04:47 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2003-11-23 18:04:47 +0000 |
commit | bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d (patch) | |
tree | 8ca0fe4e2418e6aa18dc74a236c577a719f6c6ed /src/cmd/seq.c | |
parent | f08fdedcee12c06e3ce9ac9bec363915978e8289 (diff) | |
download | plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.gz plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.bz2 plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.zip |
new utilities.
the .C files compile but are renamed to avoid building automatically.
Diffstat (limited to 'src/cmd/seq.c')
-rw-r--r-- | src/cmd/seq.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/cmd/seq.c b/src/cmd/seq.c new file mode 100644 index 00000000..062bb8a4 --- /dev/null +++ b/src/cmd/seq.c @@ -0,0 +1,92 @@ +#include <u.h> +#include <libc.h> + +double min = 1.0; +double max = 0.0; +double incr = 1.0; +int constant = 0; +int nsteps; +char *format; + +void +usage(void) +{ + fprint(2, "usage: seq [-fformat] [-w] [first [incr]] last\n"); + exits("usage"); +} + +void +buildfmt(void) +{ + int i; + char *dp; + int w, p, maxw, maxp; + static char fmt[16]; + char buf[32]; + + format = "%g\n"; + if(!constant) + return; + maxw = 0; + maxp = 0; + for(i=0; i<=nsteps; i++){ + sprint(buf, "%g", min+i*incr); + 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; + sprint(fmt,"%%%d.%df\n", maxw, maxp); + format = fmt; +} + +void +main(int argc, char *argv[]){ + int i, j, n; + char buf[256], ffmt[4096]; + + ARGBEGIN{ + case 'w': + constant++; + break; + case 'f': + format = ARGF(); + if(format[strlen(format)-1] != '\n'){ + sprint(ffmt, "%s\n", format); + format = ffmt; + } + break; + default: + goto out; + }ARGEND + 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){ + fprint(2, "seq: zero increment\n"); + exits("zero increment"); + } + nsteps = (max-min)/incr+.5; + if(!format) + buildfmt(); + for(i=0; i<=nsteps; i++){ + n = sprint(buf, format, min+i*incr); + if(constant) + for(j=0; buf[j]==' '; j++) + buf[j] ='0'; + write(1, buf, n); + } + exits(0); +} |