OpenBSD manual page server

Manual Page Search Parameters

PW_DUP(3) Library Functions Manual PW_DUP(3)

pw_dupmake a copy of a struct passwd

#include <pwd.h>

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

The () 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 (), (), and () will overwrite the data they returned from previous calls.

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

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");
	exit(1);
}
if ((pw_save = pw_dup(pw)) == NULL) {
	fprintf(stderr, "Out of memory.\n");
	exit(1);
}

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.

January 25, 2019 OpenBSD-7.1