seq: set ff=unix (?!)
This commit is contained in:
parent
c68bba6867
commit
ec6924a56e
316
seq.c
316
seq.c
|
@ -1,158 +1,158 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int digitsleft(const char *);
|
static int digitsleft(const char *);
|
||||||
static int digitsright(const char *);
|
static int digitsright(const char *);
|
||||||
static double estrtod(const char *);
|
static double estrtod(const char *);
|
||||||
static bool validfmt(const char *);
|
static bool validfmt(const char *);
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
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 fflag = false, wflag = false;
|
bool fflag = false, wflag = false;
|
||||||
char c, ftmp[BUFSIZ], *fmt = ftmp;
|
char c, ftmp[BUFSIZ], *fmt = ftmp;
|
||||||
double start, step, end, out, dir;
|
double start, step, end, out, dir;
|
||||||
|
|
||||||
while((c = getopt(argc, argv, "f:s:w")) != -1)
|
while((c = getopt(argc, argv, "f:s:w")) != -1)
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case 'f':
|
case 'f':
|
||||||
if(!validfmt(optarg))
|
if(!validfmt(optarg))
|
||||||
eprintf("%s: invalid format\n", optarg);
|
eprintf("%s: invalid format\n", optarg);
|
||||||
fmt = optarg;
|
fmt = optarg;
|
||||||
fflag = true;
|
fflag = true;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
sep = optarg;
|
sep = optarg;
|
||||||
break;
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
wflag = true;
|
wflag = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(argc-optind) {
|
switch(argc-optind) {
|
||||||
case 3:
|
case 3:
|
||||||
starts = argv[optind++];
|
starts = argv[optind++];
|
||||||
steps = argv[optind++];
|
steps = argv[optind++];
|
||||||
ends = argv[optind++];
|
ends = argv[optind++];
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
starts = argv[optind++];
|
starts = argv[optind++];
|
||||||
/* fallthrough */
|
/* fallthrough */
|
||||||
case 1:
|
case 1:
|
||||||
ends = argv[optind++];
|
ends = argv[optind++];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]] end\n", argv[0]);
|
eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]] end\n", argv[0]);
|
||||||
}
|
}
|
||||||
start = estrtod(starts);
|
start = estrtod(starts);
|
||||||
step = estrtod(steps);
|
step = estrtod(steps);
|
||||||
end = estrtod(ends);
|
end = estrtod(ends);
|
||||||
|
|
||||||
dir = (step > 0) ? 1.0 : -1.0;
|
dir = (step > 0) ? 1.0 : -1.0;
|
||||||
if(step == 0 || start * dir > end * dir)
|
if(step == 0 || start * dir > end * dir)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
if(fmt == ftmp) {
|
if(fmt == ftmp) {
|
||||||
int right = MAX(digitsright(starts),
|
int 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));
|
int left = MAX(digitsleft(starts), digitsleft(ends));
|
||||||
|
|
||||||
snprintf(ftmp, sizeof ftmp, "%%0%d.%df", right+left+(right != 0), right);
|
snprintf(ftmp, sizeof ftmp, "%%0%d.%df", right+left+(right != 0), right);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
snprintf(ftmp, sizeof ftmp, "%%.%df", right);
|
snprintf(ftmp, sizeof ftmp, "%%.%df", right);
|
||||||
}
|
}
|
||||||
for(out = start; out * dir <= end * dir; out += step) {
|
for(out = start; out * dir <= end * dir; out += step) {
|
||||||
if(out != start)
|
if(out != start)
|
||||||
fputs(sep, stdout);
|
fputs(sep, stdout);
|
||||||
printf(fmt, out);
|
printf(fmt, out);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
digitsleft(const char *d)
|
digitsleft(const char *d)
|
||||||
{
|
{
|
||||||
char *exp;
|
char *exp;
|
||||||
int shift;
|
int shift;
|
||||||
|
|
||||||
if(*d == '+')
|
if(*d == '+')
|
||||||
d++;
|
d++;
|
||||||
exp = strpbrk(d, "eE");
|
exp = strpbrk(d, "eE");
|
||||||
shift = exp ? atoi(&exp[1]) : 0;
|
shift = exp ? atoi(&exp[1]) : 0;
|
||||||
|
|
||||||
return MAX(0, strspn(d, "-0123456789")+shift);
|
return MAX(0, strspn(d, "-0123456789")+shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
digitsright(const char *d)
|
digitsright(const char *d)
|
||||||
{
|
{
|
||||||
char *exp;
|
char *exp;
|
||||||
int shift, after;
|
int shift, after;
|
||||||
|
|
||||||
exp = strpbrk(d, "eE");
|
exp = strpbrk(d, "eE");
|
||||||
shift = exp ? atoi(&exp[1]) : 0;
|
shift = exp ? atoi(&exp[1]) : 0;
|
||||||
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
|
after = (d = strchr(d, '.')) ? strspn(&d[1], "0123456789") : 0;
|
||||||
|
|
||||||
return MAX(0, after-shift);
|
return MAX(0, after-shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
double
|
double
|
||||||
estrtod(const char *s)
|
estrtod(const char *s)
|
||||||
{
|
{
|
||||||
char *end;
|
char *end;
|
||||||
double d;
|
double d;
|
||||||
|
|
||||||
d = strtod(s, &end);
|
d = strtod(s, &end);
|
||||||
if(end == s || *end != '\0')
|
if(end == s || *end != '\0')
|
||||||
eprintf("%s: not a real number\n", s);
|
eprintf("%s: not a real number\n", s);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
validfmt(const char *fmt)
|
validfmt(const char *fmt)
|
||||||
{
|
{
|
||||||
int occur = 0;
|
int occur = 0;
|
||||||
|
|
||||||
literal:
|
literal:
|
||||||
while(*fmt)
|
while(*fmt)
|
||||||
if(*fmt++ == '%')
|
if(*fmt++ == '%')
|
||||||
goto format;
|
goto format;
|
||||||
return occur == 1;
|
return occur == 1;
|
||||||
|
|
||||||
format:
|
format:
|
||||||
if(*fmt == '%') {
|
if(*fmt == '%') {
|
||||||
fmt++;
|
fmt++;
|
||||||
goto literal;
|
goto literal;
|
||||||
}
|
}
|
||||||
fmt += strspn(fmt, "-+#0 '");
|
fmt += strspn(fmt, "-+#0 '");
|
||||||
fmt += strspn(fmt, "0123456789");
|
fmt += strspn(fmt, "0123456789");
|
||||||
if(*fmt == '.') {
|
if(*fmt == '.') {
|
||||||
fmt++;
|
fmt++;
|
||||||
fmt += strspn(fmt, "0123456789");
|
fmt += strspn(fmt, "0123456789");
|
||||||
}
|
}
|
||||||
if(*fmt == 'L')
|
if(*fmt == 'L')
|
||||||
fmt++;
|
fmt++;
|
||||||
|
|
||||||
switch(*fmt) {
|
switch(*fmt) {
|
||||||
case 'f': case 'F':
|
case 'f': case 'F':
|
||||||
case 'g': case 'G':
|
case 'g': case 'G':
|
||||||
case 'e': case 'E':
|
case 'e': case 'E':
|
||||||
case 'a': case 'A':
|
case 'a': case 'A':
|
||||||
occur++;
|
occur++;
|
||||||
goto literal;
|
goto literal;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user