]> diplodocus.org Git - nmh/blob - test/getfullname.c
getpass.c: Move interface to own file.
[nmh] / test / getfullname.c
1 /* getfullname.c - Extract a user's name out of the GECOS field in
2 * the password file.
3 *
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <pwd.h>
15
16 extern void escape_display_name (char *, size_t);
17
18 int
19 main(int argc, char *argv[])
20 {
21 struct passwd *pwd;
22 char buf[BUFSIZ], *p;
23
24 if (argc > 2) {
25 fprintf (stderr, "usage: %s [name]\n", argv[0]);
26 return 1;
27 }
28 if (argc < 2) {
29 pwd = getpwuid(getuid());
30 if (! pwd) {
31 fprintf(stderr, "Unable to retrieve user info for "
32 "userid %ld\n", (long) getuid());
33 exit(1);
34 }
35
36 strncpy(buf, pwd->pw_gecos, sizeof(buf));
37 buf[sizeof(buf) - 1] = '\0';
38 } else
39 strncpy(buf, argv[1], sizeof(buf));
40
41 /*
42 * Perform the same processing that getuserinfo() does.
43 */
44
45 /*
46 * Stop at the first comma.
47 */
48 if ((p = strchr(buf, ',')))
49 *p = '\0';
50
51 /*
52 * Quote the entire string if it has a special character in it.
53 */
54 escape_display_name (buf, sizeof(buf));
55 puts(buf);
56
57 exit(0);
58 }