MirBSD manpage: pw_dup(3)

PW_DUP(3)                  BSD Programmer's Manual                   PW_DUP(3)


     pw_dup - make a copy of a struct passwd


     #include <pwd.h>

     struct passwd *
     pw_dup(const struct passwd *pw);


     The pw_dup() function allocates sufficient memory for a copy of the
     struct passwd pw, does the copy, and returns a pointer to it. This is
     useful as subsequent calls to getpwent(), getpwnam(), and getpwuid() will
     overwrite the data they returned from previous calls.

     The returned pointer may be deallocated by a single call to free(3).
     Since pw_dup() allocates space for the copy in one chunk, it is not
     necessary to free the individual strings contained in the returned struct

     If insufficient memory is available, NULL is returned.


     The following will make a copy of the struct passwd for root and store it
     in "pw_save":

           struct passwd *pw, *pw_save;

           if ((pw = getpwnam("root")) == NULL) {
                   fprintf(stderr, "Cannot find root in the password file.\n");
           if ((pw_save = pw_dup(pw)) == NULL) {
                   fprintf(stderr, "Out of memory.\n");


     pw_dup() function may fail and set the external variable errno for any of
     the errors specified for the library function malloc(3).


     free(3), getpwent(3), malloc(3)


     The pw_dup() function first appeared in OpenBSD 2.9.

MirBSD #10-current            November 15, 2000                              1

Generated on 2022-12-24 01:00:14 by $MirOS: src/scripts/roff2htm,v 1.113 2022/12/21 23:14:31 tg Exp $ — This product includes material provided by mirabilos.

These manual pages and other documentation are copyrighted by their respective writers; their sources are available at the project’s CVSweb, AnonCVS and other mirrors. The rest is Copyright © 2002–2022 MirBSD.

This manual page’s HTML representation is supposed to be valid XHTML/1.1; if not, please send a bug report — diffs preferred.

Kontakt / Impressum & Datenschutzerklärung