Fixing seq

This commit is contained in:
David Galos 2013-07-01 13:25:41 -04:00
parent c94fad2ccc
commit 563742500e
2 changed files with 15 additions and 17 deletions

5
seq.1
View File

@ -10,7 +10,7 @@ seq \- print a sequence of numbers
.IR separator ] .IR separator ]
.RI [ start .RI [ start
.RI [ step ]] .RI [ step ]]
.RI end .IR end
.SH DESCRIPTION .SH DESCRIPTION
.B seq .B seq
will print a sequence of numbers from will print a sequence of numbers from
@ -31,5 +31,6 @@ specifies the separator to print between output lines
.TP .TP
.BI \-w .BI \-w
tells seq to print out lines in equal width tells seq to print out lines in equal width
.TP
.SH SEE ALSO
.IR printf (3)

27
seq.c
View File

@ -14,8 +14,8 @@ static bool validfmt(const char *);
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-f format] [-s sep] [-w width] first" eprintf("usage: %s [-f fmt] [-s separator] [-w width] [start"
" [inc [last]]\n", argv0); " [step]] end\n", argv0);
} }
int int
@ -23,14 +23,15 @@ main(int argc, char *argv[])
{ {
const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n"; const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n";
bool wflag = false; bool wflag = false;
char ftmp[BUFSIZ], *fmt = ftmp; char *tmp, ftmp[BUFSIZ], *fmt = ftmp;
double start, step, end, out, dir; double start, step, end, out, dir;
int left, right;
ARGBEGIN { ARGBEGIN {
case 'f': case 'f':
if(!validfmt(EARGF(usage()))) if(!validfmt(tmp=EARGF(usage())))
eprintf("%s: invalid format\n", ARGF()); eprintf("%s: invalid format\n", tmp);
fmt = ARGF(); fmt = tmp;
break; break;
case 's': case 's':
sep = EARGF(usage()); sep = EARGF(usage());
@ -44,13 +45,9 @@ main(int argc, char *argv[])
switch(argc) { switch(argc) {
case 3: case 3:
starts = argv[0]; steps = argv[1];
argv++; argv[1] = argv[2];
steps = argv[0]; /* fallthrough */
argv++;
ends = argv[0];
argv++;
break;
case 2: case 2:
starts = argv[0]; starts = argv[0];
argv++; argv++;
@ -70,12 +67,12 @@ main(int argc, char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
if(fmt == ftmp) { if(fmt == ftmp) {
int right = MAX(digitsright(starts), right = MAX(digitsright(starts),
MAX(digitsright(ends), MAX(digitsright(ends),
digitsright(steps))); digitsright(steps)));
if(wflag) { if(wflag) {
int left = MAX(digitsleft(starts), digitsleft(ends)); left = MAX(digitsleft(starts), digitsleft(ends));
snprintf(ftmp, sizeof ftmp, "%%0%d.%df", snprintf(ftmp, sizeof ftmp, "%%0%d.%df",
right+left+(right != 0), right); right+left+(right != 0), right);