xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/sane_strtol.c (revision 67b9b338a7386232ac596b5fd0cd5a9cc8a03c71)
1 /*	$NetBSD: sane_strtol.c,v 1.2 2022/10/08 16:12:50 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	sane_strtol 3
6 /* SUMMARY
7 /*	strtol() with mandatory errno reset
8 /* SYNOPSIS
9 /*	#include <sane_strtol.h>
10 /*
11 /*	long	sane_strtol(
12 /*	const char *start,
13 /*	char **restrict end,
14 /*	int	base)
15 /*
16 /*	unsigned long sane_strtoul(
17 /*	const char *start,
18 /*	char **restrict end,
19 /*	int	base)
20 /* DESCRIPTION
21 /*	These functions are wrappers around the strtol() and strtoul()
22 /*	standard library functions that reset errno first, so that a
23 /*	prior ERANGE error won't cause false errors.
24 /* LICENSE
25 /* .ad
26 /* .fi
27 /*	The Secure Mailer license must be distributed with this software.
28 /* AUTHOR(S)
29 /*	Wietse Venema
30 /*	Google, Inc.
31 /*	111 8th Avenue
32 /*	New York, NY 10011, USA
33 /*--*/
34 
35  /*
36   * System library.
37   */
38 #include <sys_defs.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 
42  /*
43   * Utility library.
44   */
45 #include <sane_strtol.h>
46 
47 /* sane_strtol - strtol() with mandatory initialization */
48 
sane_strtol(const char * start,char ** end,int base)49 long    sane_strtol(const char *start, char **end, int base)
50 {
51     errno = 0;
52     return (strtol(start, end, base));
53 }
54 
55 /* sane_strtoul - strtoul() with mandatory initialization */
56 
sane_strtoul(const char * start,char ** end,int base)57 unsigned long sane_strtoul(const char *start, char **end, int base)
58 {
59     errno = 0;
60     return (strtoul(start, end, base));
61 }
62