#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define VERSION "1.0"
#define ZERO '0'
#define SPACE ' '
#define MAXIMUM(a, b) (((a) < (b))? (b) : (a))
#define ISSIGN(c) ((int)(c) == '-' || (int)(c) == '+')
#define ISEXP(c) ((int)(c) == 'e' || (int)(c) == 'E')
#define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
static const char *decimal_point = ".";
static char default_format[] = { "%g" };
static const struct option long_opts[] = {
{"format", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"separator", required_argument, NULL, 's'},
{"version", no_argument, NULL, 'v'},
{"equal-width", no_argument, NULL, 'w'},
{NULL, no_argument, NULL, 0}
};
static double e_atof(const char *);
static int decimal_places(const char *);
static int numeric(const char *);
static int valid_format(const char *);
static char *generate_format(double, double, double, int, char);
static __dead void usage(int error);
int
main(int argc, char *argv[])
{
int c = 0;
int equalize = 0;
double first = 1.0;
double last = 0.0;
double incr = 0.0;
double prev = 0.0;
double cur, step;
struct lconv *locale;
char *fmt = NULL;
const char *sep = "\n";
const char *term = "\n";
char *cur_print, *last_print, *prev_print;
char pad = ZERO;
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
locale = localeconv();
if (locale && locale->decimal_point && locale->decimal_point[0] != '\0')
decimal_point = locale->decimal_point;
while ((optind < argc) && !numeric(argv[optind]) &&
(c = getopt_long(argc, argv, "+f:s:w", long_opts, NULL)) != -1) {
switch (c) {
case 'f':
fmt = optarg;
equalize = 0;
break;
case 's':
sep = optarg;
break;
case 'v':
printf("seq version %s\n", VERSION);
return 0;
case 'w':
if (fmt == NULL) {
if (equalize++)
pad = SPACE;
}
break;
case 'h':
usage(0);
break;
default:
usage(1);
break;
}
}
argc -= optind;
argv += optind;
if (argc < 1 || argc > 3)
usage(1);
last = e_atof(argv[argc - 1]);
if (argc > 1)
first = e_atof(argv[0]);
if (argc > 2) {
incr = e_atof(argv[1]);
if (incr == 0.0)
errx(1, "zero %screment", (first < last) ? "in" : "de");
}
if (incr == 0.0)
incr = (first < last) ? 1.0 : -1.0;
if (incr <= 0.0 && first < last)
errx(1, "needs positive increment");
if (incr >= 0.0 && first > last)
errx(1, "needs negative decrement");
if (fmt != NULL) {
if (!valid_format(fmt))
errx(1, "invalid format string: `%s'", fmt);
} else
fmt = generate_format(first, incr, last, equalize, pad);
for (step = 1, cur = first; incr > 0 ? cur <= last : cur >= last;
cur = first + incr * step++) {
if (cur != first)
fputs(sep, stdout);
printf(fmt, cur);
prev = cur;
}
if (asprintf(&cur_print, fmt, cur) == -1 ||
asprintf(&last_print, fmt, last) == -1 ||
asprintf(&prev_print, fmt, prev) == -1)
err(1, "asprintf");
if (strcmp(cur_print, last_print) == 0 &&
strcmp(cur_print, prev_print) != 0) {
if (cur != first)
fputs(sep, stdout);
fputs(last_print, stdout);
}
free(cur_print);
free(last_print);
free(prev_print);
fputs(term, stdout);
return 0;
}
static int
numeric(const char *s)
{
int seen_decimal_pt, decimal_pt_len;
if (ISSIGN((unsigned char)*s))
s++;
seen_decimal_pt = 0;
decimal_pt_len = strlen(decimal_point);
while (*s) {
if (!isdigit((unsigned char)*s)) {
if (!seen_decimal_pt &&
strncmp(s, decimal_point, decimal_pt_len) == 0) {
s += decimal_pt_len;
seen_decimal_pt = 1;
continue;
}
if (ISEXP((unsigned char)*s)) {
s++;
if (ISSIGN((unsigned char)*s) ||
isdigit((unsigned char)*s)) {
s++;
continue;
}
}
break;
}
s++;
}
return *s == '\0';
}
static int
valid_format(const char *fmt)
{
unsigned conversions = 0;
while (*fmt != '\0') {
if (*fmt != '%') {
fmt++;
continue;
}
fmt++;
if (*fmt == '%') {
fmt++;
continue;
}
while (*fmt != '\0' && strchr("#0- +'", *fmt)) {
fmt++;
}
while (*fmt != '\0' && strchr("0123456789", *fmt)) {
fmt++;
}
if (*fmt == '.') {
fmt++;
while (*fmt != '\0' && strchr("0123456789", *fmt)) {
fmt++;
}
}
switch (*fmt) {
case 'A':
case 'a':
case 'E':
case 'e':
case 'F':
case 'f':
case 'G':
case 'g':
conversions++;
break;
default:
return 0;
}
}
return conversions == 1;
}
static double
e_atof(const char *num)
{
char *endp;
double dbl;
errno = 0;
dbl = strtod(num, &endp);
if (errno == ERANGE)
err(2, "%s", num);
else if (*endp != '\0')
errx(2, "invalid floating point argument: %s", num);
if (dbl == -0.0)
dbl = 0;
return dbl;
}
static int
decimal_places(const char *number)
{
int places = 0;
char *dp;
if ((dp = strstr(number, decimal_point))) {
dp += strlen(decimal_point);
while (isdigit((unsigned char)*dp++))
places++;
}
return places;
}
static char *
generate_format(double first, double incr, double last, int equalize, char pad)
{
static char buf[256];
char cc = '\0';
int precision, width1, width2, places;
if (equalize == 0)
return default_format;
if (first > last)
last = first - incr * floor((first - last) / incr);
else
last = first + incr * floor((last - first) / incr);
snprintf(buf, sizeof(buf), "%g", incr);
if (strchr(buf, 'e'))
cc = 'e';
precision = decimal_places(buf);
width1 = snprintf(buf, sizeof(buf), "%g", first);
if (strchr(buf, 'e'))
cc = 'e';
if ((places = decimal_places(buf)))
width1 -= (places + strlen(decimal_point));
precision = MAXIMUM(places, precision);
width2 = snprintf(buf, sizeof(buf), "%g", last);
if (strchr(buf, 'e'))
cc = 'e';
if ((places = decimal_places(buf)))
width2 -= (places + strlen(decimal_point));
if (precision) {
snprintf(buf, sizeof(buf), "%%%c%d.%d%c", pad,
MAXIMUM(width1, width2) + (int)strlen(decimal_point) +
precision, precision, (cc) ? cc : 'f');
} else {
snprintf(buf, sizeof(buf), "%%%c%d%c", pad,
MAXIMUM(width1, width2), (cc) ? cc : 'g');
}
return buf;
}
static __dead void
usage(int error)
{
fprintf(stderr,
"usage: %s [-w] [-f format] [-s string] [first [incr]] last\n",
getprogname());
exit(error);
}