]> diplodocus.org Git - nmh/blobdiff - test/getfullname.c
Fix invalid pointer arithmetic.
[nmh] / test / getfullname.c
index 3afa00f87ea685df8801a8e17a5eab60681da4bd..9a342ca1a8f9a6ca6f23b33bc6064800da450807 100644 (file)
@@ -9,24 +9,51 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #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 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.
+        */
+
+       /*
+        * Stop at the first comma.
+        */
+       if ((p = strchr(buf, ',')))
+               *p = '\0';
 
-       printf("%s\n", pwd->pw_gecos);
+       /*
+        * Quote the entire string if it has a special character in it.
+        */
+       escape_display_name (buf, sizeof(buf));
+       puts(buf);
 
        exit(0);
 }