2012-04-10 11:33:19 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2012-05-10 14:20:16 -04:00
|
|
|
#include <stdbool.h>
|
2012-04-10 11:33:19 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
static int digitsleft(const char *);
|
|
|
|
static int digitsright(const char *);
|
|
|
|
static double estrtod(const char *);
|
|
|
|
static bool validfmt(const char *);
|
2012-04-10 11:33:19 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2012-05-10 14:20:16 -04:00
|
|
|
const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n";
|
|
|
|
bool fflag = false, wflag = false;
|
|
|
|
char c, ftmp[BUFSIZ], *fmt = ftmp;
|
2012-04-18 07:41:05 -04:00
|
|
|
double start, step, end, out, dir;
|
2012-04-16 15:46:59 -04:00
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
while((c = getopt(argc, argv, "f:s:w")) != -1)
|
2012-04-16 15:46:59 -04:00
|
|
|
switch(c) {
|
|
|
|
case 'f':
|
2012-05-10 14:20:16 -04:00
|
|
|
if(!validfmt(optarg))
|
|
|
|
eprintf("%s: invalid format\n", optarg);
|
2012-04-16 15:46:59 -04:00
|
|
|
fmt = optarg;
|
|
|
|
fflag = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sep = optarg;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
wflag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(argc-optind) {
|
|
|
|
case 3:
|
|
|
|
starts = argv[optind++];
|
|
|
|
steps = argv[optind++];
|
|
|
|
ends = argv[optind++];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
starts = argv[optind++];
|
2012-05-10 14:20:16 -04:00
|
|
|
/* fallthrough */
|
2012-04-16 15:46:59 -04:00
|
|
|
case 1:
|
|
|
|
ends = argv[optind++];
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-10 14:20:16 -04:00
|
|
|
eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]] end\n", argv[0]);
|
2012-04-16 15:46:59 -04:00
|
|
|
}
|
2012-05-10 14:20:16 -04:00
|
|
|
start = estrtod(starts);
|
|
|
|
step = estrtod(steps);
|
|
|
|
end = estrtod(ends);
|
2012-04-16 15:46:59 -04:00
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
dir = (step > 0) ? 1.0 : -1.0;
|
|
|
|
if(step == 0 || start * dir > end * dir)
|
2012-04-18 07:41:05 -04:00
|
|
|
return EXIT_FAILURE;
|
2012-04-16 15:46:59 -04:00
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
if(fmt == ftmp) {
|
|
|
|
int right = MAX(digitsright(starts),
|
|
|
|
MAX(digitsright(ends),
|
|
|
|
digitsright(steps)));
|
2012-04-16 15:46:59 -04:00
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
if(wflag) {
|
|
|
|
int left = MAX(digitsleft(starts), digitsleft(ends));
|
2012-04-16 15:46:59 -04:00
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
snprintf(ftmp, sizeof ftmp, "%%0%d.%df", right+left+(right != 0), right);
|
2012-04-16 15:46:59 -04:00
|
|
|
}
|
2012-05-10 14:20:16 -04:00
|
|
|
else
|
|
|
|
snprintf(ftmp, sizeof ftmp, "%%.%df", right);
|
|
|
|
}
|
|
|
|
for(out = start; out * dir <= end * dir; out += step) {
|
|
|
|
if(out != start)
|
|
|
|
fputs(sep, stdout);
|
|
|
|
printf(fmt, out);
|
2012-04-16 15:46:59 -04:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2012-04-10 11:33:19 -04:00
|
|
|
}
|
|
|
|
|
2012-05-10 14:20:16 -04:00
|
|
|
int
|
|
|
|
digitsleft(const char *d)
|
|
|
|
{
|
|
|
|
char *exp;
|
|
|
|
int shift;
|
|
|
|
|
|
|
|
if(*d == '+')
|
|
|
|
d++;
|
|
|
|
exp = strpbrk(d, "eE");
|
|
|
|
shift = exp ? atoi(&exp[1]) : 0;
|
|
|
|
|
|
|
|
return MAX(0, strspn(d, "-0123456789")+shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
digitsright(const char *d)
|
|
|
|
{
|
|
|
|
char *exp;
|
|
|
|
int shift, after;
|
|
|
|
|
|
|
|
exp = strpbrk(d, "eE");
|
|
|
|
shift = exp ? atoi(&exp[1]) : 0;
|
|
|
|
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
|
|
|
|
|
|
|
|
return MAX(0, after-shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
estrtod(const char *s)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
d = strtod(s, &end);
|
|
|
|
if(end == s || *end != '\0')
|
|
|
|
eprintf("%s: not a real number\n", s);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
validfmt(const char *fmt)
|
|
|
|
{
|
|
|
|
int occur = 0;
|
|
|
|
|
|
|
|
literal:
|
|
|
|
while(*fmt)
|
|
|
|
if(*fmt++ == '%')
|
|
|
|
goto format;
|
|
|
|
return occur == 1;
|
|
|
|
|
|
|
|
format:
|
|
|
|
if(*fmt == '%') {
|
|
|
|
fmt++;
|
|
|
|
goto literal;
|
|
|
|
}
|
|
|
|
fmt += strspn(fmt, "-+#0 '");
|
|
|
|
fmt += strspn(fmt, "0123456789");
|
|
|
|
if(*fmt == '.') {
|
|
|
|
fmt++;
|
|
|
|
fmt += strspn(fmt, "0123456789");
|
|
|
|
}
|
|
|
|
if(*fmt == 'L')
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
switch(*fmt) {
|
|
|
|
case 'f': case 'F':
|
|
|
|
case 'g': case 'G':
|
|
|
|
case 'e': case 'E':
|
|
|
|
case 'a': case 'A':
|
|
|
|
occur++;
|
|
|
|
goto literal;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|