util: ship a clean-room version of strtonum for convenience - ics2txt - convert… | |
git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
commit 94173d57d064a613633e0cdd0b1a6da35d28fee8 | |
parent 599d47ef952d535e775263772cfbbbe9a2704971 | |
Author: Josuah Demangeon <[email protected]> | |
Date: Thu, 24 Jun 2021 23:48:52 +0200 | |
util: ship a clean-room version of strtonum for convenience | |
Diffstat: | |
M Makefile | 2 +- | |
D strtonum.c | 65 -----------------------------… | |
M util.c | 37 +++++++++++++++++++++++++----… | |
M util.h | 8 +------- | |
4 files changed, 32 insertions(+), 80 deletions(-) | |
--- | |
diff --git a/Makefile b/Makefile | |
@@ -7,7 +7,7 @@ CFLAGS = $D $W -g | |
PREFIX = /usr/local | |
MANPREFIX = ${PREFIX}/man | |
-SRC = ical.c base64.c util.c strtonum.c | |
+SRC = ical.c base64.c util.c | |
HDR = ical.h base64.h util.h | |
OBJ = ${SRC:.c=.o} | |
AWK = tsv2ics.awk | |
diff --git a/strtonum.c b/strtonum.c | |
@@ -1,65 +0,0 @@ | |
-/* $OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $ … | |
- | |
-/* | |
- * Copyright (c) 2004 Ted Unangst and Todd Miller | |
- * All rights reserved. | |
- * | |
- * Permission to use, copy, modify, and distribute this software for any | |
- * purpose with or without fee is hereby granted, provided that the above | |
- * copyright notice and this permission notice appear in all copies. | |
- * | |
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
- */ | |
- | |
-#include <errno.h> | |
-#include <limits.h> | |
-#include <stdlib.h> | |
- | |
-#define INVALID 1 | |
-#define TOOSMALL 2 | |
-#define TOOLARGE 3 | |
- | |
-long long | |
-strtonum(const char *numstr, long long minval, long long maxval, | |
- const char **errstrp) | |
-{ | |
- long long ll = 0; | |
- int error = 0; | |
- char *ep; | |
- struct errval { | |
- const char *errstr; | |
- int err; | |
- } ev[4] = { | |
- { NULL, 0 }, | |
- { "invalid", EINVAL }, | |
- { "too small", ERANGE }, | |
- { "too large", ERANGE }, | |
- }; | |
- | |
- ev[0].err = errno; | |
- errno = 0; | |
- if (minval > maxval) { | |
- error = INVALID; | |
- } else { | |
- ll = strtoll(numstr, &ep, 10); | |
- if (numstr == ep || *ep != '\0') | |
- error = INVALID; | |
- else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) | |
- error = TOOSMALL; | |
- else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) | |
- error = TOOLARGE; | |
- } | |
- if (errstrp != NULL) | |
- *errstrp = ev[error].errstr; | |
- errno = ev[error].err; | |
- if (error) | |
- ll = 0; | |
- | |
- return (ll); | |
-} | |
diff --git a/util.c b/util.c | |
@@ -2,6 +2,7 @@ | |
#include <assert.h> | |
#include <errno.h> | |
#include <stdint.h> | |
+#include <limits.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
@@ -9,8 +10,6 @@ | |
char *arg0; | |
-/** logging **/ | |
- | |
static void | |
_log(char const *fmt, va_list va) | |
{ | |
@@ -54,8 +53,6 @@ debug(char const *fmt, ...) | |
_log(fmt, va); | |
} | |
-/** strings **/ | |
- | |
size_t | |
strlcpy(char *d, char const *s, size_t sz) | |
{ | |
@@ -137,7 +134,35 @@ strsplit(char *s, char **array, size_t len, char const *se… | |
return i; | |
} | |
-/** memory **/ | |
+long long | |
+strtonum(char const *s, long long min, long long max, char const **errstr) | |
+{ | |
+ long long ll = 0; | |
+ char *end; | |
+ | |
+ assert(min < max); | |
+ errno = 0; | |
+ ll = strtoll(s, &end, 10); | |
+ if ((errno == ERANGE && ll == LLONG_MIN) || ll < min) { | |
+ if (errstr != NULL) | |
+ *errstr = "too small"; | |
+ return 0; | |
+ } | |
+ if ((errno == ERANGE && ll == LLONG_MAX) || ll > max) { | |
+ if (errstr != NULL) | |
+ *errstr = "too large"; | |
+ return 0; | |
+ } | |
+ if (errno == EINVAL || *end != '\0') { | |
+ if (errstr != NULL) | |
+ *errstr = "invalid"; | |
+ return 0; | |
+ } | |
+ assert(errno == 0); | |
+ if (errstr != NULL) | |
+ *errstr = NULL; | |
+ return ll; | |
+} | |
void * | |
reallocarray(void *mem, size_t n, size_t sz) | |
@@ -147,8 +172,6 @@ reallocarray(void *mem, size_t n, size_t sz) | |
return realloc(mem, n * sz); | |
} | |
-/** time **/ | |
- | |
time_t | |
tztime(struct tm *tm, char const *tz) | |
{ | |
diff --git a/util.h b/util.h | |
@@ -8,13 +8,11 @@ | |
#define LEN(x) (sizeof (x) / sizeof *(x)) | |
-/** logging **/ | |
extern char *arg0; | |
+ | |
void err(int, char const *fmt, ...); | |
void warn(char const *fmt, ...); | |
void debug(char const *fmt, ...); | |
- | |
-/** strings **/ | |
size_t strlcpy(char *, char const *, size_t); | |
char *strsep(char **, char const *); | |
void strchomp(char *); | |
@@ -22,11 +20,7 @@ char *strappend(char **, char const *); | |
size_t strlcat(char *, char const *, size_t); | |
long long strtonum(const char *, long long, long long, const char **); | |
size_t strsplit(char *, char **, size_t, char const *); | |
- | |
-/** memory **/ | |
void *reallocarray(void *, size_t, size_t); | |
- | |
-/** time **/ | |
time_t tztime(struct tm *, char const *); | |
#endif |