1.\"	$OpenBSD: pw_dup.3,v 1.10 2019/01/25 00:19:25 millert Exp $
2.\"
3.\" Copyright (c) 2000 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: January 25 2019 $
18.Dt PW_DUP 3
19.Os
20.Sh NAME
21.Nm pw_dup
22.Nd make a copy of a struct passwd
23.Sh SYNOPSIS
24.In pwd.h
25.Ft struct passwd *
26.Fn pw_dup "const struct passwd *pw"
27.Sh DESCRIPTION
28The
29.Fn pw_dup
30function allocates sufficient memory for a copy of the struct passwd
31.Fa pw ,
32does the copy, and returns a pointer to it.
33This is useful as subsequent calls to
34.Fn getpwent ,
35.Fn getpwnam ,
36and
37.Fn getpwuid
38will overwrite the data they returned from previous calls.
39.Pp
40The returned pointer should be deallocated by a single call to
41.Xr free 3 .
42Since
43.Fn pw_dup
44allocates space for the copy in one chunk, it is not necessary to free
45the individual strings contained in the returned struct passwd.
46.Pp
47If insufficient memory is available,
48.Dv NULL
49is returned.
50.Sh EXAMPLES
51The following will make a copy of the struct passwd for root and
52store it in
53.Qq pw_save :
54.Bd -literal -offset indent
55struct passwd *pw, *pw_save;
56
57if ((pw = getpwnam("root")) == NULL) {
58	fprintf(stderr, "Cannot find root in the password file.\en");
59	exit(1);
60}
61if ((pw_save = pw_dup(pw)) == NULL) {
62	fprintf(stderr, "Out of memory.\en");
63	exit(1);
64}
65.Ed
66.Sh ERRORS
67.Fn pw_dup
68function may fail and set the external variable
69.Va errno
70for any of the errors specified for the library function
71.Xr malloc 3 .
72.Sh SEE ALSO
73.Xr free 3 ,
74.Xr getpwent 3 ,
75.Xr malloc 3
76.Sh HISTORY
77The
78.Fn pw_dup
79function first appeared in
80.Ox 2.9 .
81