1.\" $OpenBSD: pw_dup.3,v 1.2 2000/12/24 00:30:50 aaron Exp $ 2.\" 3.\" Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 20.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd November 15, 2000 29.Dt PW_DUP 3 30.Os 31.Sh NAME 32.Nm pw_dup 33.Nd make a copy of a struct passwd 34.Sh SYNOPSIS 35.Fd #include <pwd.h> 36.Ft struct passwd * 37.Fn pw_dup "cost struct passwd *pw" 38.Sh DESCRIPTION 39The 40.Fn pw_dup 41function allocates sufficient memory for a copy of the struct passwd 42.Fa pw , 43does the copy, and returns a pointer to it. 44This is useful as subsequent calls to 45.Fn getpwent , 46.Fn getpwnam , 47and 48.Fn getpwuid 49will overwrite the data from previous calls. 50.Pp 51The returned pointer may be deallocated by a single call to 52.Xr free 3 . 53Since 54.Fn pw_dup 55allocates space for the copy in one chunk it is not necessary to free 56the individual strings contained in the returned struct passwd. 57.Pp 58If insufficient memory is available, 59.Dv NULL 60is returned. 61.Sh EXAMPLES 62The following will make a copy of the struct passwd for root and 63store it in 64.Qq pw_save : 65.Bd -literal -offset indent 66struct passwd *pw, *pw_save; 67 68if ((pw = getpwnam("root")) == NULL) { 69 fprintf(stderr, "Cannot find root in the password file.\en"); 70 exit(1); 71} 72if ((pw_save = pw_dup(pw)) == NULL) { 73 fprintf(stderr, "Out of memory.\en"); 74 exit(1); 75} 76.Ed 77.Sh ERRORS 78.Fn pw_dup 79function may fail and set the external variable 80.Va errno 81for any of the errors specified for the library function 82.Xr malloc 3 . 83.Sh SEE ALSO 84.Xr free 3 , 85.Xr getpwent 3 , 86.Xr malloc 3 87.Sh HISTORY 88The 89.Fn pw_dup 90function first appeared in 91.Ox 2.9 . 92