gawk

Sed-like interface to the Gopher protocol
git clone git://jacobedwards.org/gawk
Log | Files | Refs | LICENSE

util.c (2489B)


      1 /* Copyright 2021 Jacob R. Edwards
      2  *
      3  * This file is part of gawk.
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17  */
     18 
     19 #include <sys/stat.h>
     20 #include <errno.h>
     21 #include <stdarg.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 
     25 #include "util.h"
     26 
     27 FILE *
     28 wfopen(char const *path, char const *mode)
     29 {
     30 	FILE *fp;
     31 
     32 	fp = fopen(path, mode);
     33 	if (fp == NULL)
     34 		warn(errno, "unable to open '%s' (%s)", path, mode);
     35 	return fp;
     36 }
     37 
     38 int
     39 argsplit(char **argv, int size, char *s, char const *sep, int cat)
     40 {
     41         int argc;
     42 
     43         for (argc = 0; argc < size &&
     44             (argv[argc] = strsep(&s, sep)) != NULL;) {
     45                 if (!cat || *argv[argc] != '\0')
     46                         ++argc;
     47         }
     48 
     49         if (argc == size &&
     50             argv[argc - 1][strcspn(argv[argc - 1], sep)] == '\0')
     51                 return -1;
     52         return argc;
     53 }
     54 
     55 int
     56 exists(char const *path)
     57 {
     58 	struct stat sb;
     59 
     60 	if (stat(path, &sb) == -1 && errno == ENOENT) {
     61 		errno = 0;
     62 		return 0;
     63 	}
     64 	return 1;
     65 }
     66 
     67 int
     68 wfclose(FILE *fp)
     69 {
     70 	int error;
     71 
     72 	error = ferror(fp);
     73 	if (fclose(fp) || error) {
     74 		warn(errno, "unable to close file");
     75 		return EOF;
     76 	}
     77 	return 0;
     78 }
     79 
     80 int
     81 wremove(char const *path)
     82 {
     83 	if (remove(path)) {
     84 		warn(errno, "unable to remove '%s'", path);
     85 		return -1;
     86 	}
     87 	return 0;
     88 }
     89 
     90 long long
     91 wstrtonum(const char *ns, long long min, long long max, const char **err)
     92 {
     93 	long long n;
     94 
     95 	n = strtonum(ns, min, max, err);
     96 	if (*err != NULL)
     97 		warn(0, "numeric string '%s' is %s", ns, *err);
     98 	return n;
     99 }
    100 
    101 void
    102 tr(char *r, char const *s, int orig, int repl)
    103 {
    104 	for (; *s != '\0'; ++s, ++r)
    105 		if (*s == orig)
    106 			*r = repl;
    107 		else
    108 			*r = *s;
    109 	*r = '\0';
    110 }
    111 
    112 void
    113 warn(int error, char const *format, ...)
    114 {
    115 	va_list ap;
    116 
    117 	fprintf(stderr, "%s: ", getprogname());
    118 	va_start(ap, format);
    119 	vfprintf(stderr, format, ap);
    120 	va_end(ap);
    121 
    122 	if (error)
    123 		fprintf(stderr, ": %s", strerror(error));
    124 	fprintf(stderr, ".\n");
    125 }