sbase/seq.c

148 lines
2.5 KiB
C
Raw Normal View History

2012-06-09 13:53:05 -04:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2012-06-09 13:53:05 -04:00
#include "util.h"
2015-03-07 07:36:40 -05:00
static int
digitsleft(const char *d)
{
int shift;
2015-03-20 11:37:29 -04:00
char *exp;
2015-03-07 07:36:40 -05:00
if (*d == '+')
d++;
exp = strpbrk(d, "eE");
shift = exp ? estrtonum(exp + 1, INT_MIN, INT_MAX) : 0;
2015-03-07 07:36:40 -05:00
return MAX(0, strspn(d, "-0123456789") + shift);
}
static int
digitsright(const char *d)
{
int shift, after;
2015-03-20 11:37:29 -04:00
char *exp;
2015-03-07 07:36:40 -05:00
exp = strpbrk(d, "eE");
shift = exp ? estrtonum(&exp[1], INT_MIN, INT_MAX) : 0;
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
return MAX(0, after - shift);
}
static int
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 0;
}
}
2012-06-09 13:53:05 -04:00
2013-06-14 14:20:47 -04:00
static void
usage(void)
{
eprintf("usage: %s [-f fmt] [-s sep] [-w] [startnum"
" [step]] endnum\n", argv0);
2013-06-14 14:20:47 -04:00
}
2012-06-09 13:53:05 -04:00
int
main(int argc, char *argv[])
{
double start, step, end, out, dir;
2015-03-20 11:37:29 -04:00
int wflag = 0, left, right;
char *tmp, ftmp[BUFSIZ], *fmt = ftmp;
const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n";
2012-06-09 13:53:05 -04:00
2013-06-14 14:20:47 -04:00
ARGBEGIN {
case 'f':
if (!validfmt(tmp=EARGF(usage())))
2013-07-01 13:25:41 -04:00
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':
wflag = 1;
2013-06-14 14:20:47 -04:00
break;
default:
usage();
} ARGEND
2013-06-14 14:20:47 -04: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;
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
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)));
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",
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);
}
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);
}
putchar('\n');
2012-06-09 13:53:05 -04:00
Add *fshut() functions to properly flush file streams This has been a known issue for a long time. Example: printf "word" > /dev/full wouldn't report there's not enough space on the device. This is due to the fact that every libc has internal buffers for stdout which store fragments of written data until they reach a certain size or on some callback to flush them all at once to the kernel. You can force the libc to flush them with fflush(). In case flushing fails, you can check the return value of fflush() and report an error. However, previously, sbase didn't have such checks and without fflush(), the libc silently flushes the buffers on exit without checking the errors. No offense, but there's no way for the libc to report errors in the exit- condition. GNU coreutils solve this by having onexit-callbacks to handle the flushing and report issues, but they have obvious deficiencies. After long discussions on IRC, we came to the conclusion that checking the return value of every io-function would be a bit too much, and having a general-purpose fclose-wrapper would be the best way to go. It turned out that fclose() alone is not enough to detect errors. The right way to do it is to fflush() + check ferror on the fp and then to a fclose(). This is what fshut does and that's how it's done before each return. The return value is obviously affected, reporting an error in case a flush or close failed, but also when reading failed for some reason, the error- state is caught. the !!( ... + ...) construction is used to call all functions inside the brackets and not "terminating" on the first. We want errors to be reported, but there's no reason to stop flushing buffers when one other file buffer has issues. Obviously, functionales come before the flush and ret-logic comes after to prevent early exits as well without reporting warnings if there are any. One more advantage of fshut() is that it is even able to report errors on obscure NFS-setups which the other coreutils are unable to detect, because they only check the return-value of fflush() and fclose(), not ferror() as well.
2015-04-04 15:25:17 -04:00
return fshut(stdout, "<stdout>");
2012-06-09 13:53:05 -04:00
}