1 /* $OpenBSD: pw_dup.c,v 1.1 2000/11/21 00:49:58 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char rcsid[] = "$OpenBSD: pw_dup.c,v 1.1 2000/11/21 00:49:58 millert Exp $"; 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include <sys/types.h> 35 36 #include <stdlib.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <pwd.h> 40 41 struct passwd * 42 pw_dup(pw) 43 const struct passwd *pw; 44 { 45 struct passwd *newpw; 46 char *cp; 47 size_t siz; 48 49 /* Allocate in one big chunk for easy freeing */ 50 siz = sizeof(struct passwd); 51 if (pw->pw_name) 52 siz += strlen(pw->pw_name) + 1; 53 if (pw->pw_passwd) 54 siz += strlen(pw->pw_passwd) + 1; 55 if (pw->pw_class) 56 siz += strlen(pw->pw_class) + 1; 57 if (pw->pw_gecos) 58 siz += strlen(pw->pw_gecos) + 1; 59 if (pw->pw_dir) 60 siz += strlen(pw->pw_dir) + 1; 61 if (pw->pw_shell) 62 siz += strlen(pw->pw_shell) + 1; 63 if ((cp = malloc(siz)) == NULL) 64 return(NULL); 65 newpw = (struct passwd *)cp; 66 67 /* 68 * Copy in passwd contents and make strings relative to space 69 * at the end of the buffer. 70 */ 71 memcpy(newpw, pw, sizeof(struct passwd)); 72 cp += sizeof(struct passwd); 73 if (pw->pw_name) { 74 siz = strlen(pw->pw_name) + 1; 75 memcpy(cp, pw->pw_name, siz); 76 newpw->pw_name = cp; 77 cp += siz; 78 } 79 if (pw->pw_passwd) { 80 siz = strlen(pw->pw_passwd) + 1; 81 memcpy(cp, pw->pw_passwd, siz); 82 newpw->pw_passwd = cp; 83 cp += siz; 84 } 85 if (pw->pw_class) { 86 siz = strlen(pw->pw_class) + 1; 87 memcpy(cp, pw->pw_class, siz); 88 newpw->pw_class = cp; 89 cp += siz; 90 } 91 if (pw->pw_gecos) { 92 siz = strlen(pw->pw_gecos) + 1; 93 memcpy(cp, pw->pw_gecos, siz); 94 newpw->pw_gecos = cp; 95 cp += siz; 96 } 97 if (pw->pw_dir) { 98 siz = strlen(pw->pw_dir) + 1; 99 memcpy(cp, pw->pw_dir, siz); 100 newpw->pw_dir = cp; 101 cp += siz; 102 } 103 if (pw->pw_shell) { 104 siz = strlen(pw->pw_shell) + 1; 105 memcpy(cp, pw->pw_shell, siz); 106 newpw->pw_shell = cp; 107 cp += siz; 108 } 109 110 return(newpw); 111 } 112