aboutsummaryrefslogtreecommitdiffstats
path: root/man3
diff options
context:
space:
mode:
authorMichael Kerrisk <mtk.manpages@gmail.com>2008-11-07 17:05:13 -0500
committerMichael Kerrisk <mtk.manpages@gmail.com>2008-11-07 17:51:33 -0500
commitc6ec368541cca9847e45adef3e6ef44423d2599b (patch)
tree5f59830182907359c7fce204d0cbdb109fbc134e /man3
parent3b11a49415c37347c91b801656d05ffdd46b3529 (diff)
downloadman-pages-c6ec368541cca9847e45adef3e6ef44423d2599b.tar.gz
getpwnam.3: Add an EXAMPLE program for getpwnam_r()
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
Diffstat (limited to 'man3')
-rw-r--r--man3/getpwnam.355
1 files changed, 55 insertions, 0 deletions
diff --git a/man3/getpwnam.3 b/man3/getpwnam.3
index 65727c36b5..b0c6354c9a 100644
--- a/man3/getpwnam.3
+++ b/man3/getpwnam.3
@@ -1,4 +1,6 @@
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
+.\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
+.\" <mtk.manpages@gmail.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
@@ -28,6 +30,7 @@
.\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
.\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
.\" Modified 2003-11-15 by aeb
+.\" 2008-11-07, mtk, Added an example program for getpwnam_r().
.\"
.TH GETPWNAM 3 2008-11-07 "GNU" "Linux Programmer's Manual"
.SH NAME
@@ -232,6 +235,58 @@ To determine the (initial) home directory of another user,
it is necessary to use
.I getpwnam("username")\->pw_dir
or similar.
+.SH EXAMPLE
+The program below demonstrates the use of
+.BR getpwnam_r ()
+to find the full username and user ID for the username
+supplied as a command-line argument.
+
+.nf
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+int
+main(int argc, char *argv[])
+{
+ struct passwd pwd;
+ struct passwd *result;
+ char *buf;
+ size_t bufsize;
+ int s;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s username\\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if (bufsize == \-1) /* Value was indeterminate */
+ bufsize = 16384; /* Should be more than enough */
+
+ buf = malloc(bufsize);
+ if (buf == NULL) {
+ perror("malloc");
+ exit(EXIT_FAILURE);
+ }
+
+ s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
+ if (result == NULL) {
+ if (s == 0)
+ printf("Not found\\n");
+ else {
+ errno = s;
+ perror("getpwnam_r");
+ }
+ exit(EXIT_FAILURE);
+ }
+
+ printf("Name: %s; UID: %ld\n", pwd.pw_gecos, (long) pwd.pw_uid);
+ exit(EXIT_SUCCESS);
+}
+.fi
.SH "SEE ALSO"
.BR endpwent (3),
.BR fgetpwent (3),