From dbd7618c9fb002152f86d37cd7fdf74b216dc71d Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 8 Mar 2020 21:24:10 +0000 Subject: [PATCH] Use utility functions --- Makefile | 13 +++--- criticals.c | 6 +-- fractal.c | 13 +----- fun-gen.c | 61 ++++++---------------------- julia.c | 112 +++++++--------------------------------------------- palette.c | 30 +++----------- pnmout.c | 14 +++---- 7 files changed, 48 insertions(+), 201 deletions(-) diff --git a/Makefile b/Makefile index 36e75c0..2e5e7aa 100644 --- a/Makefile +++ b/Makefile @@ -19,19 +19,20 @@ FRACTAL_CFLAGS = -O3 -ffast-math -march=native -mtune=native \ ${CFLAGS} BIN = fractal fun-gen -FRACTAL_OBJ = criticals.o fractal.o julia.o palette.o pnmout.o util.o +FRACTAL_OBJ = criticals.o fractal.o julia.o palette.o pnmout.o FUN_GEN_OBJ = fun-gen.o -OBJ = ${FRACTAL_OBJ} ${FUN_GEN_OBJ} +UTIL_OBJ = util.o +OBJ = ${FRACTAL_OBJ} ${FUN_GEN_OBJ} ${UTIL_OBJ} all: fractal julia.o: fun.h -fractal: ${FRACTAL_OBJ} - ${CC} ${LDFLAGS} -o $@ ${FRACTAL_OBJ} -lm +fractal: ${FRACTAL_OBJ} ${UTIL_OBJ} + ${CC} ${LDFLAGS} -o $@ ${FRACTAL_OBJ} ${UTIL_OBJ} -lm -fun-gen: ${FUN_GEN_OBJ} - ${CC} ${LDFLAGS} -o $@ ${FUN_GEN_OBJ} -lm +fun-gen: ${FUN_GEN_OBJ} ${UTIL_OBJ} + ${CC} ${LDFLAGS} -o $@ ${FUN_GEN_OBJ} ${UTIL_OBJ} -lm clean: rm -f ${OBJ} ${BIN} fun.h diff --git a/criticals.c b/criticals.c index e3ad995..8111232 100644 --- a/criticals.c +++ b/criticals.c @@ -62,9 +62,7 @@ ncriticals(void) long double complex get_critical(size_t i) { - if (i >= idx) { - fprintf(stderr, "get_critical: index %zu out of range", i); - exit(1); - } + if (i >= idx) + errx(1, "get_critical: index %zu out of range", i); return criticals[i]; } diff --git a/fractal.c b/fractal.c index 40ee0fd..b1aebd2 100644 --- a/fractal.c +++ b/fractal.c @@ -15,8 +15,6 @@ #define _DEFAULT_SOURCE #endif -#include -#include #include #include #include @@ -41,7 +39,6 @@ int main(int argc, char *argv[]) { char *end; - intmax_t v; long seed; int ch; @@ -49,15 +46,7 @@ main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "s:")) != -1) switch (ch) { case 's': - errno = 0; - v = strtoimax(optarg, &end, 0); - if (*end != '\0' || errno != 0 || v > LONG_MAX || - v < LONG_MIN) { - fprintf(stderr, "-s: Invalid value \"%s\".", - optarg); - exit(1); - } - seed = (long)v; + seed = parse_integer(optarg, LONG_MIN, LONG_MAX); break; default: usage(); diff --git a/fun-gen.c b/fun-gen.c index a86c562..be6ebf2 100644 --- a/fun-gen.c +++ b/fun-gen.c @@ -15,17 +15,18 @@ #define _DEFAULT_SOURCE #endif -#include -#include -#include #include #include #include #include +#include "util.h" + #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MAX_DEGREE 1000 + static char *argv0; struct poly { @@ -37,10 +38,8 @@ struct poly { static void poly_init(struct poly *p, unsigned int degree, const char *expr) { - if ((p->coefs = malloc((degree + 1) * sizeof(double))) == NULL) { - fprintf(stderr, "out of memory\n"); - exit(1); - } + if ((p->coefs = malloc((degree + 1) * sizeof(double))) == NULL) + errx(1, "out of memory"); p->degree = degree; p->expr = expr; @@ -158,9 +157,6 @@ print_funs(struct poly p, struct poly q) "- q * (2.0L * dp * dq + p * ddq) " "+ 2.0L * p * dq * dq" ") / (q * q * q);\n"); - /*printf("\treturn (ddp " - "- 2.0 * ((dp * q - p * dq) / (q * q)) * dq " - "- (p / q) * ddq) / q;\n");*/ print_fun_footer(); poly_free(&dp); @@ -169,43 +165,13 @@ print_funs(struct poly p, struct poly q) poly_free(&ddq); } -static unsigned int -parse_uint(const char *s) -{ - char *end; - uintmax_t v; - - errno = 0; - v = strtoumax(s, &end, 10); - if (s == end || *end != '\0' || errno != 0 || v > UINT_MAX) { - fprintf(stderr, "%s: invalid value: %s\n", argv0, s); - exit(1); - } - - return (unsigned int)v; -} - -static double -parse_double(const char *s) -{ - char *end; - double d; - - errno = 0; - d = strtod(s, &end); - if (s == end || *end != '\0' || errno != 0 || !isfinite(d)) { - fprintf(stderr, "%s: invalid value: %s\n", argv0, s); - exit(1); - } - - return d; -} - static void usage(void) { - fprintf(stderr, "Usage: %s p-degree q-degree p-coefs q-coefs\n" - " %s -n p-degree p-coefs\n", argv0, argv0); + const char *p = xgetprogname(); + fprintf(stderr, "Usage:\n" + "\t%s p-degree q-degree p-coefs q-coefs\n" + "\t%s -n p-degree p-coefs\n", p, p); exit(1); } @@ -218,8 +184,8 @@ do_poly_div(int argc, char *argv[]) if (argc < 2) usage(); - p_degree = parse_uint(argv[0]); - q_degree = parse_uint(argv[1]); + p_degree = parse_integer(argv[0], 0, MAX_DEGREE); + q_degree = parse_integer(argv[1], 0, MAX_DEGREE); if (argc != p_degree + q_degree + 2 + 2) usage(); @@ -249,7 +215,7 @@ do_newton(int argc, char *argv[]) if (argc < 1) usage(); - f_degree = parse_uint(argv[0]); + f_degree = parse_integer(argv[0], 0, MAX_DEGREE); if (argc != f_degree + 1 + 1) usage(); @@ -275,7 +241,6 @@ main(int argc, char *argv[]) { struct poly p, q; char *end; - uintmax_t v; unsigned int i, j, p_degree, q_degree; int ch, nflag; diff --git a/julia.c b/julia.c index 253609f..8a1da3a 100644 --- a/julia.c +++ b/julia.c @@ -16,8 +16,6 @@ #endif #include -#include -#include #include #include #include @@ -164,123 +162,43 @@ julia_main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "a:b:c:D:d:h:Ll:n:w:x:y:z:")) != -1) switch (ch) { case 'a': - errno = 0; - cen_x = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-a: invalid value \"%s\"", - optarg); - exit(1); - } + cen_x = parse_double(optarg); break; case 'b': - errno = 0; - cen_y = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-b: invalid value \"%s\"", - optarg); - exit(1); - } + cen_y = parse_double(optarg); break; case 'c': - errno = 0; - contour = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0 - || contour <= 0) { - fprintf(stderr, "-c: invalid value \"%s\"", - optarg); - exit(1); - } + contour = parse_double(optarg); break; case 'D': - errno = 0; - displacement = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-d: invalid value \"%s\"", - optarg); - exit(1); - } + displacement = parse_double(optarg); break; case 'd': - errno = 0; - density = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-d: invalid value \"%s\"", - optarg); - exit(1); - } + density = parse_double(optarg); break; case 'h': - errno = 0; - v = strtoimax(optarg, &end, 0); - if (end == optarg || *end != '\0' || errno != 0 || - v <= 0 || v >= 65536) { - fprintf(stderr, "-h: invalid value \"%s\"", - optarg); - exit(1); - } - height = (unsigned int)v + 1; + height = parse_integer(optarg, 1, 65536); break; case 'L': do_light = 1; break; case 'l': - errno = 0; - light_intensity = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-l: invalid value \"%s\"", - optarg); - exit(1); - } + light_intensity = parse_double(optarg); break; case 'n': - errno = 0; - v = strtoimax(optarg, &end, 0); - if (end == optarg || *end != '\0' || errno != 0 || - v <= 0 || v >= ULONG_MAX) { - fprintf(stderr, "-n: invalid value \"%s\"", - optarg); - exit(1); - } - niters = (unsigned long)v; + niters = parse_integer(optarg, 1, ULONG_MAX); break; case 'w': - errno = 0; - v = strtoimax(optarg, &end, 0); - if (end == optarg || *end != '\0' || errno != 0 || - v <= 0 || v >= 65536) { - fprintf(stderr, "-w: invalid value \"%s\"", - optarg); - exit(1); - } - width = (unsigned int)v + 1; + width = parse_integer(optarg, 1, 65536); break; case 'x': - errno = 0; - julia_x = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-x: invalid value \"%s\"", - optarg); - exit(1); - } + julia_x = parse_double(optarg); break; case 'y': - errno = 0; - julia_y = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0) { - fprintf(stderr, "-y: invalid value \"%s\"", - optarg); - exit(1); - } + julia_y = parse_double(optarg); break; case 'z': - errno = 0; - zoom = strtold(optarg, &end); - if (end == optarg || *end != '\0' || errno != 0 - || zoom <= 0) { - fprintf(stderr, "-z: invalid value \"%s\"", - optarg); - exit(1); - } + zoom = parse_double(optarg); break; default: usage(); @@ -292,10 +210,8 @@ julia_main(int argc, char *argv[]) palette = palette_gen(palette_size); - if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL) { - fprintf(stderr, "out of memory\n"); - exit(1); - } + if ((rescache = malloc(sizeof(*rescache) * width * height)) == NULL) + errx(1, "out of memory"); if (do_light) { long double angle_xy, angle_z; diff --git a/palette.c b/palette.c index f2ced23..76c1c25 100644 --- a/palette.c +++ b/palette.c @@ -15,8 +15,6 @@ #define _DEFAULT_SOURCE #endif -#include -#include #include #include #include @@ -57,10 +55,8 @@ palette_gen(size_t n) double r, g, b, t; size_t i; - if (!(palette = malloc(n * sizeof(*palette)))) { - fprintf(stderr, "out of memory"); - exit(1); - } + if ((palette = malloc(n * sizeof(*palette))) == NULL) + errx(1, "out of memory"); for (i = 0; i < n; i++) { t = i / (double)n; @@ -97,25 +93,11 @@ palette_main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "h:w:")) != -1) switch (ch) { case 'h': - errno = 0; - v = strtoumax(optarg, &end, 0); - if (*end != '\0' || errno != 0 || v == 0 - || v > UINT_MAX) { - fprintf(stderr, "-h: Invalid value \"%s\"", - optarg); - exit(1); - } - height = v; + height = parse_integer(optarg, 1, 65535); + break; case 'w': - errno = 0; - v = strtoumax(optarg, &end, 0); - if (*end != '\0' || errno != 0 || v == 0 - || v > UINT_MAX) { - fprintf(stderr, "-w: Invalid value \"%s\"", - optarg); - exit(1); - } - width = v; + width = parse_integer(optarg, 1, 65535); + break; default: usage(); } diff --git a/pnmout.c b/pnmout.c index 7be0db3..2cdc392 100644 --- a/pnmout.c +++ b/pnmout.c @@ -15,14 +15,13 @@ #include "palette.h" #include "pnmout.h" +#include "util.h" void pnmout_header(FILE *fp, unsigned int width, unsigned int height) { - if (fprintf(fp, "P6\n%u %u 255\n", width, height) < 0) { - perror("fprintf"); - exit(1); - } + if (fprintf(fp, "P6\n%u %u 255\n", width, height) < 0) + err(1, "fprintf"); } void @@ -32,9 +31,6 @@ pnmout_pixel(FILE *fp, struct color c) v[0] = c.r / 256; v[1] = c.g / 256; v[2] = c.b / 256; - if (fwrite(v, sizeof(v[0]), sizeof(v) / sizeof(v[0]), fp) != 3) { - perror("fwrite"); - exit(1); - } - + if (fwrite(v, sizeof(v[0]), sizeof(v) / sizeof(v[0]), fp) != 3) + err(1, "fwrite"); }