#include #include #include double first = 1.0; double last = 0.0; double step = 1.0; double nsteps = 0.0; double final = 0.0; int constant; int consigned; char *format; char *zarg; char *sstep; void usage(void) { fprint(2, "usage: %s [-w] [-fformat] [first [step]] last\n", argv0); exits("usage"); } /* * [+-]?(([0-9]+([.][0-9]*)?)|([.][0-9]+))([Ee][+-]?[0-9]+)? */ void getwidths(char *s, int *mh, int *mt) { int h, t, e, m; h = 0; t = 0; m = 0; if(*s == '+' || *s == '-'){ s++; m++; } if(isdigit(*s)){ s++; h++; while(isdigit(*s)){ s++; h++; } if(*s == '.'){ s++; while(isdigit(*s)){ s++; t++; } } }else if(*s == '.'){ s++; if(!isdigit(*s++)) goto out; while(isdigit(*s)){ s++; t++; } }else goto out; if(*s|32 == 'e'){ s++; e = atoi(s); if(e > 307) e = 307; if(e < -30) e = -30; h += e; t -= e; } out: if(h <= 0) h = 1 + m; if(t < 0) t = 0; if(h > *mh) *mh = h; if(t > *mt) *mt = t; return; } int headwidth(double d) { int i; i = 1; if(d < 0.0){ d = -d; i++; } if(d > 1.0) i += log10(d); return i; } void buildfmt(void) { int h, t, z; static char fmt[16]; h = 0; t = 0; /* determine head and tail widths from bounds and increment */ getwidths(zarg, &h, &t); if(sstep) getwidths(sstep, &z, &t); z = headwidth(final); if(z > h) h = z; if(constant){ if(t > 0) h += t + 1; if(first < 0.0 || last < 0.0){ consigned = 1; sprint(fmt, "%%+%d.%df\n", h, t); }else sprint(fmt, "%%%d.%df\n", h, t); }else sprint(fmt, "%%.%df\n", t); format = fmt; } void main(int argc, char *argv[]){ int j, n; char buf[512], ffmt[4096]; double val, i, preval; ARGBEGIN{ case 'w': if(format) sysfatal("-w incompatible with -f"); constant++; break; case 'f': if(constant) sysfatal("-f incompatible with -w"); format = EARGF(usage()); 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(); last = atof(argv[argc-1]); if(argc > 1) first = atof(argv[0]); if(argc > 2){ sstep = argv[1]; step = atof(argv[1]); } if(step == 0.0) sysfatal("zero increment"); nsteps = (last - first) / step; final = floor(nsteps) * step + first; zarg = argv[0]; if(!format) buildfmt(); preval = step + first; for(i = 0; i <= nsteps; i++){ val = i * step + first; n = sprint(buf, format, val); if(constant){ for(j = 0; buf[j] == ' '; j++) buf[j] = '0'; if(consigned && j > 0){ buf[0] = buf[j]; buf[j] = '0'; } } write(1, buf, n); preval = val; } exits(0); }