diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/gen/pw_dup.c | 89 |
1 files changed, 33 insertions, 56 deletions
diff --git a/lib/libc/gen/pw_dup.c b/lib/libc/gen/pw_dup.c index 107734e7363..e212ff9c258 100644 --- a/lib/libc/gen/pw_dup.c +++ b/lib/libc/gen/pw_dup.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pw_dup.c,v 1.5 2003/06/17 21:56:23 millert Exp $ */ +/* $OpenBSD: pw_dup.c,v 1.6 2005/02/27 22:13:10 pat Exp $ */ /* * Copyright (c) 2000, 2002 Todd C. Miller <Todd.Miller@courtesan.com> @@ -21,7 +21,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$OpenBSD: pw_dup.c,v 1.5 2003/06/17 21:56:23 millert Exp $"; +static const char rcsid[] = "$OpenBSD: pw_dup.c,v 1.6 2005/02/27 22:13:10 pat Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -31,6 +31,23 @@ static const char rcsid[] = "$OpenBSD: pw_dup.c,v 1.5 2003/06/17 21:56:23 miller #include <stdio.h> #include <string.h> +#define PW_SIZE(name, size) \ +do { \ + if (pw->name) { \ + size = strlen(pw->name) + 1; \ + total += size; \ + } \ +} while (0) + +#define PW_COPY(name, size) \ +do { \ + if (pw->name) { \ + (void)memcpy(cp, pw->name, size); \ + newpw->name = cp; \ + cp += size; \ + } \ +} while (0) + struct passwd * pw_dup(const struct passwd *pw) { @@ -40,30 +57,13 @@ pw_dup(const struct passwd *pw) /* Allocate in one big chunk for easy freeing */ total = sizeof(struct passwd); - if (pw->pw_name) { - nsize = strlen(pw->pw_name) + 1; - total += nsize; - } - if (pw->pw_passwd) { - psize = strlen(pw->pw_passwd) + 1; - total += psize; - } - if (pw->pw_class) { - csize = strlen(pw->pw_class) + 1; - total += csize; - } - if (pw->pw_gecos) { - gsize = strlen(pw->pw_gecos) + 1; - total += gsize; - } - if (pw->pw_dir) { - dsize = strlen(pw->pw_dir) + 1; - total += dsize; - } - if (pw->pw_shell) { - ssize = strlen(pw->pw_shell) + 1; - total += ssize; - } + PW_SIZE(pw_name, nsize); + PW_SIZE(pw_passwd, psize); + PW_SIZE(pw_class, csize); + PW_SIZE(pw_gecos, gsize); + PW_SIZE(pw_dir, dsize); + PW_SIZE(pw_shell, ssize); + if ((cp = malloc(total)) == NULL) return (NULL); newpw = (struct passwd *)cp; @@ -74,36 +74,13 @@ pw_dup(const struct passwd *pw) */ (void)memcpy(newpw, pw, sizeof(struct passwd)); cp += sizeof(struct passwd); - if (pw->pw_name) { - (void)memcpy(cp, pw->pw_name, nsize); - newpw->pw_name = cp; - cp += nsize; - } - if (pw->pw_passwd) { - (void)memcpy(cp, pw->pw_passwd, psize); - newpw->pw_passwd = cp; - cp += psize; - } - if (pw->pw_class) { - (void)memcpy(cp, pw->pw_class, csize); - newpw->pw_class = cp; - cp += csize; - } - if (pw->pw_gecos) { - (void)memcpy(cp, pw->pw_gecos, gsize); - newpw->pw_gecos = cp; - cp += gsize; - } - if (pw->pw_dir) { - (void)memcpy(cp, pw->pw_dir, dsize); - newpw->pw_dir = cp; - cp += dsize; - } - if (pw->pw_shell) { - (void)memcpy(cp, pw->pw_shell, ssize); - newpw->pw_shell = cp; - cp += ssize; - } + + PW_COPY(pw_name, nsize); + PW_COPY(pw_passwd, psize); + PW_COPY(pw_class, csize); + PW_COPY(pw_gecos, gsize); + PW_COPY(pw_dir, dsize); + PW_COPY(pw_shell, ssize); return (newpw); } |