gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit 980621c45fabef68a9146bb28b61b180c959e19c
parent b80dbab2aeb720e58eb416912c23850715f516c2
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Fri,  1 Jan 2021 22:58:54 -0800

Separate utility functions into util.c

In this case I define utility functions as functions which can
easily be reused for many different things.

Diffstat:
MMakefile | 2+-
Mmain.c | 91+------------------------------------------------------------------------------
Autil.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 97 insertions(+), 91 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ TARGET = gawk -OBJS = main.o +OBJS = main.o util.o # paths PREFIX = /usr/local diff --git a/main.c b/main.c @@ -16,7 +16,6 @@ */ #include <sys/socket.h> -#include <sys/stat.h> #include <dirent.h> #include <err.h> #include <errno.h> @@ -31,6 +30,7 @@ #include <unistd.h> #include "config.h" +#include "util.h" #define LEN(X) (sizeof(X) / sizeof(*X)) @@ -55,53 +55,6 @@ itemtoaddr(const char **request) return request + 1; } -FILE * -wfopen(const char *path, const char *mode) -{ - FILE *fp; - - fp = fopen(path, mode); - if (fp == NULL) - warn("unable to open '%s' ('%s')", path, mode); - return fp; -} - -int -wfclose(FILE *fp) -{ - int error; - - error = ferror(fp); - if (fclose(fp) || error) { - warn("unable to close file"); - return EOF; - } - return 0; -} - -int -wremove(const char *path) -{ - if (remove(path)) { - warn("unable to remove '%s'", path); - return -1; - } - return 0; -} - -int -warg(int min, int max, int argc, const char **ap) -{ - if (max > 0 && argc > max) { - warnx("warning: '%s' and %d more arguments unused.", ap[max], argc - (max + 1)); - } else if (argc < min) { - warnx("error: Not enough arguments."); - return 1; - } - - return 0; -} - int argsplit(char **argv, int size, char *s, const char *sep, int concatenate) { @@ -120,48 +73,6 @@ argsplit(char **argv, int size, char *s, const char *sep, int concatenate) } int -exists(const char *path) -{ - struct stat sb; - - if (stat(path, &sb) == -1 && errno == ENOENT) { - errno = 0; - return 0; - } - return 1; -} - -int -strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s) -{ - char *ep; - unsigned long n; - - n = strtoul(s, &ep, 10); - if (*s == '\0' || *ep != '\0') - errno = EINVAL; - else if (n < min || n > max) - errno = ERANGE; - else { - *r = n; - return 0; - } - warn("'%s'", s); - return 1; -} - -void -tr(char *r, const char *s, int orig, int repl) -{ - for (; *s != '\0'; ++s, ++r) - if (*s == orig) - *r = repl; - else - *r = *s; - *r = '\0'; -} - -int resolve(const char *host, const char *port) { int error, s; diff --git a/util.c b/util.c @@ -0,0 +1,95 @@ +#include <sys/stat.h> +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +FILE * +wfopen(const char *path, const char *mode) +{ + FILE *fp; + + fp = fopen(path, mode); + if (fp == NULL) + warn("unable to open '%s' ('%s')", path, mode); + return fp; +} + +int +exists(const char *path) +{ + struct stat sb; + + if (stat(path, &sb) == -1 && errno == ENOENT) { + errno = 0; + return 0; + } + return 1; +} + +int +strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s) +{ + char *ep; + unsigned long n; + + n = strtoul(s, &ep, 10); + if (*s == '\0' || *ep != '\0') + errno = EINVAL; + else if (n < min || n > max) + errno = ERANGE; + else { + *r = n; + return 0; + } + warn("'%s'", s); + return 1; +} + +int +warg(int min, int max, int argc, const char **ap) +{ + if (max > 0 && argc > max) { + warnx("warning: '%s' and %d more arguments unused.", ap[max], argc - (max + 1)); + } else if (argc < min) { + warnx("error: Not enough arguments."); + return 1; + } + + return 0; +} + +int +wfclose(FILE *fp) +{ + int error; + + error = ferror(fp); + if (fclose(fp) || error) { + warn("unable to close file"); + return EOF; + } + return 0; +} + +int +wremove(const char *path) +{ + if (remove(path)) { + warn("unable to remove '%s'", path); + return -1; + } + return 0; +} + +void +tr(char *r, const char *s, int orig, int repl) +{ + for (; *s != '\0'; ++s, ++r) + if (*s == orig) + *r = repl; + else + *r = *s; + *r = '\0'; +}