|
|
1.1 root 1: #include <stdio.h>
2:
3: double min=1.0;
4: double max=0.0;
5: double incr=1.0;
6: int width=0;
7: int const=0;
8: char *format="%.0f\n";
9: char *picture;
10:
11: extern double atof();
12: extern char *strchr();
13:
14: main(argc, argv)
15: char *argv[];
16: {
17: register i, j, k, n;
18: char buf[BUFSIZ];
19: double x;
20:
21: while(argc>1 && argv[1][0]=='-'){
22: switch(argv[1][1]){
23: case 'w':
24: const++;
25: break;
26: case 'p':
27: picture= &argv[1][2];
28: break;
29: default:
30: goto out;
31: }
32: --argc;
33: argv++;
34: }
35: out:
36: if(argc<2 || argc>4)
37: usage();
38: max=atof(argv[argc-1]);
39: if(argc>2){
40: if(argc>3){
41: incr=atof(argv[2]);
42: argv[2]=argv[3];
43: }
44: min=atof(argv[1]);
45: }
46: if(incr==0){
47: fprintf(stderr, "seq: zero increment\n");
48: exit(1);
49: }
50: buildfmt();
51: n=(max-min)/incr;
52: for(i=0; i<=n; i++){
53: x=min+i*incr;
54: if(width){
55: if(const){
56: sprintf(buf, "%.0f", x);
57: k=width-strlen(buf);
58: }else
59: k=width;
60: if(x<0){
61: putchar('-');
62: x= -x;
63: }
64: for(j=0; j<k; j++)
65: putchar('0');
66: }
67: sprintf(buf, format, x);
68: fputs(buf, stdout);
69: }
70: return 0;
71: }
72: usage(){
73: fprintf(stderr, "usage: seq [-w] [-p10.2] [first [incr]] last\n");
74: exit(1);
75: }
76: buildfmt()
77: {
78: static char fmt[10]="%.0f";
79: register char *t;
80: char buf[32];
81: if(const){
82: sprintf(buf, fmt, min);
83: width=strlen(buf);
84: sprintf(buf, fmt, max);
85: if(strlen(buf)>width)
86: width=strlen(buf);
87: }
88: if(picture)
89: while(picture[0]=='0' && picture[1]!='.'){
90: width++;
91: picture++;
92: }
93: if(picture==0)
94: return;
95: if(picture[0]=='-')
96: picture++;
97: t=strchr(picture, '.');
98: if(t>0){
99: t++;
100: if(*t==0)
101: strcat(fmt, ".");
102: else
103: sprintf(fmt, "%%.%df", strlen(t));
104: }
105: strcat(fmt, "\n");
106: format=fmt;
107: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.