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