Module Name: src
Committed By: christos
Date: Sun Jan 18 18:09:36 UTC 2015
Modified Files:
src/lib/libutil: Makefile efun.3 efun.c
Log Message:
add estro{i,u} (Kamil Rytarowski)
To generate a diff of this commit:
cvs rdiff -u -r1.75 -r1.76 src/lib/libutil/Makefile
cvs rdiff -u -r1.10 -r1.11 src/lib/libutil/efun.3
cvs rdiff -u -r1.8 -r1.9 src/lib/libutil/efun.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libutil/Makefile
diff -u src/lib/libutil/Makefile:1.75 src/lib/libutil/Makefile:1.76
--- src/lib/libutil/Makefile:1.75 Thu Jun 20 16:42:30 2013
+++ src/lib/libutil/Makefile Sun Jan 18 13:09:36 2015
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.75 2013/06/20 20:42:30 christos Exp $
+# $NetBSD: Makefile,v 1.76 2015/01/18 18:09:36 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/4/93
USE_SHLIBDIR= yes
@@ -77,6 +77,8 @@ MLINKS+=efun.3 estrlcpy.3
MLINKS+=efun.3 estrlcat.3
MLINKS+=efun.3 estrdup.3
MLINKS+=efun.3 estrndup.3
+MLINKS+=efun.3 estrtoi.3
+MLINKS+=efun.3 estrtou.3
MLINKS+=efun.3 emalloc.3
MLINKS+=efun.3 ecalloc.3
MLINKS+=efun.3 erealloc.3
Index: src/lib/libutil/efun.3
diff -u src/lib/libutil/efun.3:1.10 src/lib/libutil/efun.3:1.11
--- src/lib/libutil/efun.3:1.10 Mon May 3 01:40:37 2010
+++ src/lib/libutil/efun.3 Sun Jan 18 13:09:36 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: efun.3,v 1.10 2010/05/03 05:40:37 jruoho Exp $
+.\" $NetBSD: efun.3,v 1.11 2015/01/18 18:09:36 christos Exp $
.\"
.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
.\" All rights reserved.
@@ -41,6 +41,8 @@
.Nm estrndup ,
.Nm estrlcat ,
.Nm estrlcpy ,
+.Nm estrtoi ,
+.Nm estrtou ,
.Nm evasprintf
.Nd error-checked utility functions
.Sh LIBRARY
@@ -67,6 +69,10 @@
.Fn estrlcat "char *dst" "const char *src" "size_t len"
.Ft size_t
.Fn estrlcpy "char *dst" "const char *src" "size_t len"
+.Ft intmax_t
+.Fn estrtoi "const char * nptr" "int base" "intmax_t lo" "intmax_t hi"
+.Ft uintmax_t
+.Fn estrtou "const char * nptr" "int base" "uintmax_t lo" "uintmax_t hi"
.Ft int
.Fn evasprintf "char ** restrict str" "const char * restrict fmt" "..."
.Sh DESCRIPTION
@@ -80,6 +86,8 @@ The
.Fn estrndup ,
.Fn estrlcat ,
.Fn estrlcpy ,
+.Fn estrtoi ,
+.Fn estrtou ,
and
.Fn evasprintf
functions
@@ -114,4 +122,13 @@ error handler will just call
.Xr strlcat 3 ,
.Xr strlcpy 3 ,
.Xr strndup 3 ,
+.Xr strtoi 3 ,
+.Xr strtou 3 ,
.Xr vasprintf 3
+.Sh HISTORY
+The
+.Fn estrtoi
+and
+.Fn estrtou
+functions were added in
+.Nx 8 .
Index: src/lib/libutil/efun.c
diff -u src/lib/libutil/efun.c:1.8 src/lib/libutil/efun.c:1.9
--- src/lib/libutil/efun.c:1.8 Sun Dec 30 12:37:13 2012
+++ src/lib/libutil/efun.c Sun Jan 18 13:09:36 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: efun.c,v 1.8 2012/12/30 17:37:13 dholland Exp $ */
+/* $NetBSD: efun.c,v 1.9 2015/01/18 18:09:36 christos Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -35,11 +35,12 @@
#include <sys/cdefs.h>
#ifdef __RCSID
-__RCSID("$NetBSD: efun.c,v 1.8 2012/12/30 17:37:13 dholland Exp $");
+__RCSID("$NetBSD: efun.c,v 1.9 2015/01/18 18:09:36 christos Exp $");
#endif
#include <err.h>
#include <errno.h>
+#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
@@ -156,3 +157,31 @@ evasprintf(char ** __restrict ret, const
(*efunc)(1, "Cannot format string");
return rv;
}
+
+intmax_t
+estrtoi(const char * nptr, int base, intmax_t lo, intmax_t hi)
+{
+ int e;
+ intmax_t rv = strtoi(nptr, NULL, base, lo, hi, &e);
+ if (e != 0) {
+ errno = e;
+ (*efunc)(1,
+ "Cannot convert string value '%s' with base %d to a number in range [%jd .. %jd]",
+ nptr, base, lo, hi);
+ }
+ return rv;
+}
+
+uintmax_t
+estrtou(const char * nptr, int base, uintmax_t lo, uintmax_t hi)
+{
+ int e;
+ uintmax_t rv = strtou(nptr, NULL, base, lo, hi, &e);
+ if (e != 0) {
+ errno = e;
+ (*efunc)(1,
+ "Cannot convert string value '%s' with base %d to a number in range [%ju .. %ju]",
+ nptr, base, lo, hi);
+ }
+ return rv;
+}