Fixes and checks for seq. Thanks David Galos!
This commit is contained in:
parent
d512d7938e
commit
29ce2560a7
1
LICENSE
1
LICENSE
|
@ -10,6 +10,7 @@ MIT/X Consortium License
|
|||
© 2012 William Haddon <william@haddonthethird.net>
|
||||
© 2012 Kurt H. Maier <khm@intma.in>
|
||||
© 2012 Christoph Lohmann <20h@r-36.net>
|
||||
© 2012 David Galos <galosd83@students.rowan.edu>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
|
|
1
Makefile
1
Makefile
|
@ -52,6 +52,7 @@ SRC = \
|
|||
tty.c \
|
||||
uname.c \
|
||||
seq.c \
|
||||
seq-test.c \
|
||||
wc.c
|
||||
|
||||
OBJ = $(SRC:.c=.o) $(LIB)
|
||||
|
|
149
seq.c
149
seq.c
|
@ -5,85 +5,81 @@
|
|||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <libgen.h>
|
||||
#include <regex.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
char afmt[4096];
|
||||
#define MAX(a, b) (((a) > (b))? (a):(b))
|
||||
|
||||
char *
|
||||
getalignedwidthfmt(char *fmt, char *starts, char *steps, char *ends)
|
||||
int
|
||||
validfloat(char *fmt)
|
||||
{
|
||||
char starttmp[4096], steptmp[4096], endtmp[4096];
|
||||
char *startdelim, *stepdelim, *enddelim;
|
||||
int startl, startr, stepr, endl, endr;
|
||||
int maxl, maxr;
|
||||
char *end;
|
||||
|
||||
/*
|
||||
* 1.) Get a temp buffer we can work in.
|
||||
* 2.) Calculate the *r(ight) and *l(eft) size.
|
||||
*/
|
||||
memmove(starttmp, starts, strlen(starts)+1);
|
||||
startdelim = strchr(starttmp, '.');
|
||||
if (startdelim != NULL) {
|
||||
startdelim[0] = '\0';
|
||||
startr = strlen(startdelim+1);
|
||||
} else {
|
||||
startr = 0;
|
||||
}
|
||||
startl = strlen(starttmp);
|
||||
if (starttmp[0] == '+')
|
||||
startl--;
|
||||
fmt += strspn(fmt, " ");
|
||||
strtod(fmt, &end);
|
||||
if (fmt == end || end != fmt + strlen(fmt))
|
||||
return 0;
|
||||
|
||||
memmove(endtmp, ends, strlen(ends)+1);
|
||||
enddelim = strchr(endtmp, '.');
|
||||
if (enddelim != NULL) {
|
||||
enddelim[0] = '\0';
|
||||
endr = strlen(enddelim+1);
|
||||
} else {
|
||||
endr = 0;
|
||||
}
|
||||
endl = strlen(endtmp);
|
||||
if (endtmp[0] == '+')
|
||||
endl--;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* We do not care for the left length of the step, because it
|
||||
* will never be displayed.
|
||||
*/
|
||||
memmove(steptmp, steps, strlen(steps)+1);
|
||||
stepdelim = strchr(steptmp, '.');
|
||||
if (stepdelim != NULL) {
|
||||
stepdelim[0] = '\0';
|
||||
stepr = strlen(stepdelim+1);
|
||||
} else {
|
||||
stepr = 0;
|
||||
}
|
||||
int
|
||||
digitsleft(char *d)
|
||||
{
|
||||
char *exp;
|
||||
int shift;
|
||||
|
||||
maxl = (startl > endl)? startl : endl;
|
||||
maxr = (startl > endr)? startr : endr;
|
||||
if (stepr > maxr)
|
||||
maxr = stepr;
|
||||
if (d[0] == '-' || d[0] == '+')
|
||||
d++;
|
||||
exp = strpbrk(d, "eE");
|
||||
shift = exp? atoi(exp+1) : 0;
|
||||
|
||||
if (maxl <= 1) {
|
||||
snprintf(afmt, sizeof(afmt), "%%.%df", maxr);
|
||||
} else if (maxr == 0) {
|
||||
snprintf(afmt, sizeof(afmt), "%%0%d.f", maxl);
|
||||
} else {
|
||||
snprintf(afmt, sizeof(afmt), "%%0%d.%df", maxl+maxr+1, maxr);
|
||||
}
|
||||
return MAX(0, strspn(d, "0123456789")+shift);
|
||||
}
|
||||
|
||||
return afmt;
|
||||
int
|
||||
digitsright(char *d)
|
||||
{
|
||||
char *exp;
|
||||
int shift, after;
|
||||
|
||||
if (d[0] == '-' || d[0] == '+')
|
||||
d++;
|
||||
exp = strpbrk(d, "eE");
|
||||
shift = exp ? atoi(exp+1) : 0;
|
||||
after = (d = strchr(d, '.'))? strspn(d+1, "0123456789") : 0;
|
||||
|
||||
return MAX(0, after-shift);
|
||||
}
|
||||
|
||||
int
|
||||
validfmt(char *fmt)
|
||||
{
|
||||
regex_t reg;
|
||||
int ret;
|
||||
|
||||
regcomp(®, "\\([^%]|%%\\)*%[0-9]*\\.[0-9]*[fFgG]"
|
||||
"\\([^%]|%%\\)*", REG_NOSUB);
|
||||
ret = regexec(®, fmt, 0, NULL, 0);
|
||||
regfree(®);
|
||||
|
||||
return (ret == 0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char c, *fmt, *sep, *starts, *steps, *ends;
|
||||
bool wflag = false;
|
||||
char c, *fmt, ftmp[4096], *sep, *starts, *steps, *ends;
|
||||
bool wflag, fflag;
|
||||
double start, step, end, out;
|
||||
int left, right;
|
||||
|
||||
sep = "\n";
|
||||
fmt = "%G";
|
||||
fmt = ftmp;
|
||||
|
||||
wflag = false;
|
||||
fflag = false;
|
||||
|
||||
starts = "1";
|
||||
steps = "1";
|
||||
|
@ -92,7 +88,10 @@ main(int argc, char *argv[])
|
|||
while((c = getopt(argc, argv, "f:s:w")) != -1) {
|
||||
switch(c) {
|
||||
case 'f':
|
||||
if (!validfmt(optarg))
|
||||
eprintf("invalid format.\n");
|
||||
fmt = optarg;
|
||||
fflag = true;
|
||||
break;
|
||||
case 's':
|
||||
sep = optarg;
|
||||
|
@ -103,6 +102,9 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
if (wflag && fflag)
|
||||
eprintf("-f and -w cannot be combined.\n");
|
||||
|
||||
switch(argc-optind) {
|
||||
case 3:
|
||||
starts = argv[optind++];
|
||||
|
@ -117,10 +119,18 @@ main(int argc, char *argv[])
|
|||
ends = argv[optind++];
|
||||
break;
|
||||
default:
|
||||
eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]]"
|
||||
" end\n", basename(argv[0]));
|
||||
eprintf("usage: %s [-w] [-f fmt] [-s separator] "
|
||||
"[start [step]] end\n",
|
||||
basename(argv[0]));
|
||||
}
|
||||
|
||||
if (!validfloat(starts))
|
||||
eprintf("start is not a valid float.\n");
|
||||
if (!validfloat(steps))
|
||||
eprintf("step is not a valid float.\n");
|
||||
if (!validfloat(ends))
|
||||
eprintf("end is not a valid float.\n");
|
||||
|
||||
start = atof(starts);
|
||||
step = atof(steps);
|
||||
end = atof(ends);
|
||||
|
@ -136,9 +146,18 @@ main(int argc, char *argv[])
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (wflag)
|
||||
fmt = getalignedwidthfmt(fmt, starts, steps, ends);
|
||||
printf("%s\n", fmt);
|
||||
right = MAX(digitsright(starts),
|
||||
MAX(digitsright(ends),
|
||||
digitsright(steps)));
|
||||
if (wflag) {
|
||||
left = MAX(digitsleft(starts), digitsleft(ends));
|
||||
|
||||
snprintf(ftmp, sizeof(ftmp), "%%0%d.%df",
|
||||
right+left+(right != 0),
|
||||
right);
|
||||
} else if (fmt == ftmp) {
|
||||
snprintf(ftmp, sizeof(ftmp), "%%.%df", right);
|
||||
}
|
||||
|
||||
for (out = start;;) {
|
||||
printf(fmt, out);
|
||||
|
|
Loading…
Reference in New Issue
Block a user