2 * Copyright (c) 2000, 2002, 2012-2013
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
26 #include <sys/types.h>
36 #endif /* STDC_HEADERS */
39 #endif /* HAVE_STRING_H */
42 #endif /* HAVE_STRINGS_H */
43 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
45 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
48 #define PW_SIZE(name, size) \
51 size = strlen(pw->name) + 1; \
56 #define PW_COPY(name, size) \
59 (void)memcpy(cp, pw->name, size); \
66 pw_dup(const struct passwd *pw)
68 size_t nsize = 0, psize = 0, gsize = 0, dsize = 0, ssize = 0, total;
69 #ifdef HAVE_LOGIN_CAP_H
75 /* Allocate in one big chunk for easy freeing */
76 total = sizeof(struct passwd);
77 PW_SIZE(pw_name, nsize);
78 PW_SIZE(pw_passwd, psize);
79 #ifdef HAVE_LOGIN_CAP_H
80 PW_SIZE(pw_class, csize);
82 PW_SIZE(pw_gecos, gsize);
83 PW_SIZE(pw_dir, dsize);
84 PW_SIZE(pw_shell, ssize);
86 if ((cp = malloc(total)) == NULL)
88 newpw = (struct passwd *)cp;
91 * Copy in passwd contents and make strings relative to space
92 * at the end of the buffer.
94 (void)memcpy(newpw, pw, sizeof(struct passwd));
95 cp += sizeof(struct passwd);
97 PW_COPY(pw_name, nsize);
98 PW_COPY(pw_passwd, psize);
99 #ifdef HAVE_LOGIN_CAP_H
100 PW_COPY(pw_class, csize);
102 PW_COPY(pw_gecos, gsize);
103 PW_COPY(pw_dir, dsize);
104 PW_COPY(pw_shell, ssize);
108 #endif /* HAVE_PW_DUP */