1.\" $NetBSD: efun.3,v 1.10 2010/05/03 05:40:37 jruoho 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.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd April 26, 2011 31.Dt EFUN 3 32.Os 33.Sh NAME 34.Nm esetfunc , 35.Nm emalloc , 36.Nm ecalloc , 37.Nm erealloc , 38.Nm estrdup , 39.Nm estrndup , 40.Nm estrlcat , 41.Nm estrlcpy , 42.Nm easprintf 43.Nd error-checked utility functions 44.Sh LIBRARY 45.Lb libutil 46.Sh SYNOPSIS 47.In sys/types.h 48.In libutil.h 49.Ft void (*)(int, const char *, ...) 50.Fn esetfunc "void (*)(int, const char *, ...)" 51.Ft void * 52.Fn ecalloc "size_t n" "size_t c" 53.Ft void * 54.Fn emalloc "size_t n" 55.Ft void * 56.Fn erealloc "void *p" "size_t n" 57.Ft char * 58.Fn estrdup "const char *s" 59.Ft char * 60.Fn estrndup "const char *s" "size_t len" 61.Ft size_t 62.Fn estrlcat "char *dst" "const char *src" "size_t len" 63.Ft size_t 64.Fn estrlcpy "char *dst" "const char *src" "size_t len" 65.Ft int 66.Fn easprintf "char ** restrict str" "const char * restrict fmt" "..." 67.Sh DESCRIPTION 68The 69.Fn ecalloc , 70.Fn emalloc , 71.Fn erealloc , 72.Fn estrdup , 73.Fn estrndup , 74.Fn estrlcat , 75.Fn estrlcpy , 76and 77.Fn easprintf 78functions 79operate exactly as the corresponding functions that do not start with an 80.Sq e 81except that in case of an error, they call 82the installed error handler that can be configured with 83.Fn esetfunc . 84.Pp 85For the string handling functions, it is an error when the destination 86buffer is not large enough to hold the complete string. 87For functions that allocate memory or open a file, it is an error when 88they would return a null pointer. 89The default error handler is 90.Xr err 3 . 91The function 92.Fn esetfunc 93returns the previous error handler function. 94A 95.Dv NULL 96error handler will just call 97.Xr exit 3 . 98.Sh SEE ALSO 99.Xr asprintf 3 , 100.Xr calloc 3 , 101.Xr err 3 , 102.Xr exit 3 , 103.Xr fopen 3 , 104.Xr malloc 3 , 105.Xr realloc 3 , 106.Xr strdup 3 , 107.Xr strlcat 3 , 108.Xr strlcpy 3 , 109.Xr strndup 3 110.\".Xr vasprintf 3 111.Sh HISTORY 112The error functions first appeared in 113.Dx 2.11 . 114They are originally from 115.Nx . 116