2012-06-09 13:53:05 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-30 10:52:44 -05:00
|
|
|
#include <limits.h>
|
2012-06-09 13:53:05 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2012-06-09 13:53:05 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int digitsleft(const char *);
|
|
|
|
static int digitsright(const char *);
|
2014-11-13 15:24:47 -05:00
|
|
|
static int validfmt(const char *);
|
2012-06-09 13:53:05 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-07-01 13:25:41 -04:00
|
|
|
eprintf("usage: %s [-f fmt] [-s separator] [-w width] [start"
|
|
|
|
" [step]] end\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2012-06-09 13:53:05 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n";
|
2014-11-13 15:24:47 -05:00
|
|
|
int wflag = 0;
|
2013-07-01 13:25:41 -04:00
|
|
|
char *tmp, ftmp[BUFSIZ], *fmt = ftmp;
|
2012-06-09 13:53:05 -04:00
|
|
|
double start, step, end, out, dir;
|
2013-07-01 13:25:41 -04:00
|
|
|
int left, right;
|
2012-06-09 13:53:05 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'f':
|
2013-07-01 13:25:41 -04:00
|
|
|
if(!validfmt(tmp=EARGF(usage())))
|
|
|
|
eprintf("%s: invalid format\n", tmp);
|
|
|
|
fmt = tmp;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sep = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'w':
|
2014-11-13 15:24:47 -05:00
|
|
|
wflag = 1;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
switch (argc) {
|
2012-06-09 13:53:05 -04:00
|
|
|
case 3:
|
2013-07-01 13:25:41 -04:00
|
|
|
steps = argv[1];
|
|
|
|
argv[1] = argv[2];
|
|
|
|
/* fallthrough */
|
2012-06-09 13:53:05 -04:00
|
|
|
case 2:
|
2013-06-14 14:20:47 -04:00
|
|
|
starts = argv[0];
|
|
|
|
argv++;
|
2012-06-09 13:53:05 -04:00
|
|
|
/* fallthrough */
|
|
|
|
case 1:
|
2013-06-14 14:20:47 -04:00
|
|
|
ends = argv[0];
|
2012-06-09 13:53:05 -04:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-14 14:20:47 -04:00
|
|
|
usage();
|
2012-06-09 13:53:05 -04:00
|
|
|
}
|
|
|
|
start = estrtod(starts);
|
|
|
|
step = estrtod(steps);
|
|
|
|
end = estrtod(ends);
|
|
|
|
|
|
|
|
dir = (step > 0) ? 1.0 : -1.0;
|
2014-11-13 12:29:30 -05:00
|
|
|
if (step == 0 || start * dir > end * dir)
|
2014-10-02 18:46:04 -04:00
|
|
|
return 1;
|
2012-06-09 13:53:05 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (fmt == ftmp) {
|
2013-07-01 13:25:41 -04:00
|
|
|
right = MAX(digitsright(starts),
|
2012-06-09 13:53:05 -04:00
|
|
|
MAX(digitsright(ends),
|
|
|
|
digitsright(steps)));
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (wflag) {
|
2013-07-01 13:25:41 -04:00
|
|
|
left = MAX(digitsleft(starts), digitsleft(ends));
|
2012-06-09 13:53:05 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
snprintf(ftmp, sizeof ftmp, "%%0%d.%df",
|
2014-06-01 07:57:22 -04:00
|
|
|
right + left + (right != 0), right);
|
2013-06-14 14:20:47 -04:00
|
|
|
} else
|
2012-06-09 13:53:05 -04:00
|
|
|
snprintf(ftmp, sizeof ftmp, "%%.%df", right);
|
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
for (out = start; out * dir <= end * dir; out += step) {
|
|
|
|
if (out != start)
|
2012-06-09 13:53:05 -04:00
|
|
|
fputs(sep, stdout);
|
|
|
|
printf(fmt, out);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2012-06-09 13:53:05 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 07:57:22 -04:00
|
|
|
static int
|
2012-06-09 13:53:05 -04:00
|
|
|
digitsleft(const char *d)
|
|
|
|
{
|
|
|
|
char *exp;
|
|
|
|
int shift;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*d == '+')
|
2012-06-09 13:53:05 -04:00
|
|
|
d++;
|
|
|
|
exp = strpbrk(d, "eE");
|
2015-01-30 11:47:00 -05:00
|
|
|
shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0;
|
2012-06-09 13:53:05 -04:00
|
|
|
|
2014-06-01 07:57:22 -04:00
|
|
|
return MAX(0, strspn(d, "-0123456789") + shift);
|
2012-06-09 13:53:05 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 07:57:22 -04:00
|
|
|
static int
|
2012-06-09 13:53:05 -04:00
|
|
|
digitsright(const char *d)
|
|
|
|
{
|
|
|
|
char *exp;
|
|
|
|
int shift, after;
|
|
|
|
|
|
|
|
exp = strpbrk(d, "eE");
|
2015-01-30 10:52:44 -05:00
|
|
|
shift = exp ? estrtonum(&exp[1], -INT_MAX, INT_MAX) : 0;
|
2012-06-09 13:53:05 -04:00
|
|
|
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
|
|
|
|
|
2014-06-01 07:57:22 -04:00
|
|
|
return MAX(0, after - shift);
|
2012-06-09 13:53:05 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 15:24:47 -05:00
|
|
|
static int
|
2012-06-09 13:53:05 -04:00
|
|
|
validfmt(const char *fmt)
|
|
|
|
{
|
|
|
|
int occur = 0;
|
|
|
|
|
|
|
|
literal:
|
2014-11-13 12:29:30 -05:00
|
|
|
while (*fmt)
|
|
|
|
if (*fmt++ == '%')
|
2012-06-09 13:53:05 -04:00
|
|
|
goto format;
|
|
|
|
return occur == 1;
|
|
|
|
|
|
|
|
format:
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*fmt == '%') {
|
2012-06-09 13:53:05 -04:00
|
|
|
fmt++;
|
|
|
|
goto literal;
|
|
|
|
}
|
|
|
|
fmt += strspn(fmt, "-+#0 '");
|
|
|
|
fmt += strspn(fmt, "0123456789");
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*fmt == '.') {
|
2012-06-09 13:53:05 -04:00
|
|
|
fmt++;
|
|
|
|
fmt += strspn(fmt, "0123456789");
|
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
if (*fmt == 'L')
|
2012-06-09 13:53:05 -04:00
|
|
|
fmt++;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
switch (*fmt) {
|
2012-06-09 13:53:05 -04:00
|
|
|
case 'f': case 'F':
|
|
|
|
case 'g': case 'G':
|
|
|
|
case 'e': case 'E':
|
|
|
|
case 'a': case 'A':
|
|
|
|
occur++;
|
|
|
|
goto literal;
|
|
|
|
default:
|
2014-11-13 15:24:47 -05:00
|
|
|
return 0;
|
2012-06-09 13:53:05 -04:00
|
|
|
}
|
|
|
|
}
|