]> diplodocus.org Git - nmh/blobdiff - test/getfullname.c
m_gmprot.c: Move interface to own file.
[nmh] / test / getfullname.c
index 3dc221e530866139f5c58f3bb89c04784cf0ad06..14f454238a78f412636f143635bdec3595b87346 100644 (file)
@@ -1,5 +1,4 @@
-/*
- * getfullname.c - Extract a user's name out of the GECOS field in
+/* getfullname.c - Extract a user's name out of the GECOS field in
  *                the password file.
  *
  * This code is Copyright (c) 2012, by the authors of nmh.  See the
 #include <sys/types.h>
 #include <pwd.h>
 
+extern void escape_display_name (char *, size_t);
+
 int
 main(int argc, char *argv[])
 {
        struct passwd *pwd;
-       char name[BUFSIZ], *p;
-
-       if (argc > 1) {
-               fprintf (stderr, "usage: %s\n", argv[0]);
-       }
+       char buf[BUFSIZ], *p;
 
-       pwd = getpwuid(getuid());
-
-       if (! pwd) {
-               fprintf(stderr, "Unable to retrieve user info for "
-                       "userid %d\n", getuid());
-               exit(1);
+        if (argc > 2) {
+               fprintf (stderr, "usage: %s [name]\n", argv[0]);
+               return 1;
        }
+       if (argc < 2) {
+               pwd = getpwuid(getuid());
+               if (! pwd) {
+                       fprintf(stderr, "Unable to retrieve user info for "
+                               "userid %ld\n", (long) getuid());
+                       exit(1);
+               }
+
+               strncpy(buf, pwd->pw_gecos, sizeof(buf));
+               buf[sizeof(buf) - 1] = '\0';
+       } else
+               strncpy(buf, argv[1], sizeof(buf));
 
        /*
         * Perform the same processing that getuserinfo() does.
         */
 
-       strncpy(name, pwd->pw_gecos, sizeof(name));
-
-       name[sizeof(name) - 1] = '\0';
-
        /*
-        * Stop at the first comma
+        * Stop at the first comma.
         */
-
-       if ((p = strchr(name, ',')))
+       if ((p = strchr(buf, ',')))
                *p = '\0';
 
        /*
-        * Quote the entire string if it has a "." in it
+        * Quote the entire string if it has a special character in it.
         */
-
-       if (strchr(name, '.')) {
-               char tmp[BUFSIZ];
-
-               snprintf(tmp, sizeof(tmp), "\"%s\"", name);
-               strncpy(name, tmp, sizeof(name));
-
-               name[sizeof(name) - 2] = '"';
-               name[sizeof(name) - 1] = '\0';
-       }
-
-       printf("%s\n", name);
+       escape_display_name (buf, sizeof(buf));
+       puts(buf);
 
        exit(0);
 }