xref: /netbsd-src/lib/libutil/efun.3 (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1.\"     $NetBSD: efun.3,v 1.6 2007/11/21 09:17:24 rillig Exp $
2.\"
3.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Christos Zoulas.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd July 1, 2007
38.Dt EFUN 3
39.Os
40.Sh NAME
41.Nm esetfunc ,
42.Nm easprintf ,
43.Nm efopen ,
44.Nm emalloc ,
45.Nm ecalloc ,
46.Nm erealloc ,
47.Nm estrdup ,
48.Nm estrndup ,
49.Nm estrlcat ,
50.Nm estrlcpy ,
51.Nm evasprintf
52.Nd error-checked utility functions
53.Sh LIBRARY
54.Lb libutil
55.Sh SYNOPSIS
56.In util.h
57.Ft void (*)(int, const char *, ...)
58.Fn esetfunc "void (*)(int, const char *, ...)"
59.Ft int
60.Fn easprintf "char ** restrict str" "const char * restrict fmt" "..."
61.Ft FILE *
62.Fn efopen "const char *p" "const char *m"
63.Ft void *
64.Fn ecalloc "size_t n" "size_t c"
65.Ft void *
66.Fn emalloc "size_t n"
67.Ft void *
68.Fn erealloc "void *p" "size_t n"
69.Ft char *
70.Fn estrdup "const char *s"
71.Ft char *
72.Fn estrndup "const char *s" "size_t len"
73.Ft size_t
74.Fn estrlcat "char *dst" "const char *src" "size_t len"
75.Ft size_t
76.Fn estrlcpy "char *dst" "const char *src" "size_t len"
77.Ft int
78.Fn evasprintf "char ** restrict str" "const char * restrict fmt" "..."
79.Sh DESCRIPTION
80The
81.Nm easprintf ,
82.Nm efopen ,
83.Nm ecalloc ,
84.Nm emalloc ,
85.Nm erealloc ,
86.Nm estrdup ,
87.Nm estrndup ,
88.Nm estrlcat ,
89.Nm estrlcpy ,
90and
91.Nm evasprintf
92functions
93operate exactly as the correspoding functions that do not start with an
94.Sq e
95except that in case of an error, they call
96the installed error handler that can be configured with
97.Nm esetfunc .
98For the string handling functions, it is an error when the destination
99buffer is not large enough to hold the complete string.
100For functions that allocate memory or open a file, it is an error when
101they would return a null pointer.
102The default error handler is
103.Xr err 3 .
104The function
105.Nm esetfunc
106returns the previous error handler function.
107A
108.Dv NULL
109error handler will just call
110.Xr exit 3 .
111.Sh SEE ALSO
112.Xr asprintf 3 ,
113.Xr calloc 3 ,
114.Xr err 3 ,
115.Xr exit 3 ,
116.Xr fopen 3 ,
117.Xr malloc 3 ,
118.Xr realloc 3 ,
119.Xr strdup 3 ,
120.Xr strndup 3 ,
121.Xr strlcat 3 ,
122.Xr strlcpy 3 ,
123.Xr vasprintf 3
124