Added function escape_display_name() to double quote, if not already,
[mmh] / 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 *);
18
19 int
20 main(int argc, char *argv[])
21 {
22         struct passwd *pwd;
23         char buf[BUFSIZ], *p;
24         char *name = buf;
25
26         if (argc < 2) {
27                 pwd = getpwuid(getuid());
28
29                 if (! pwd) {
30                         fprintf(stderr, "Unable to retrieve user info for "
31                                 "userid %ld\n", (long) getuid());
32                         exit(1);
33                 }
34
35                 strncpy(buf, pwd->pw_gecos, sizeof(buf));
36                 buf[sizeof(buf) - 1] = '\0';
37         } else if (argc == 2) {
38                 name = argv[1];
39         } else if (argc > 2) {
40                 fprintf (stderr, "usage: %s [name]\n", argv[0]);
41                 return 1;
42         }
43
44         /*
45          * Perform the same processing that getuserinfo() does.
46          */
47
48         /*
49          * Stop at the first comma.
50          */
51         if ((p = strchr(name, ',')))
52                 *p = '\0';
53
54         /*
55          * Quote the entire string if it has a special character in it.
56          */
57         escape_display_name (name);
58
59         printf("%s\n", name);
60
61         exit(0);
62 }