]> diplodocus.org Git - nmh/blobdiff - test/getfullname.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / test / getfullname.c
index ec83939f75ab537411194d269bc2ed98f26b406e..9a342ca1a8f9a6ca6f23b33bc6064800da450807 100644 (file)
 #include <sys/types.h>
 #include <pwd.h>
 
-extern void escape_display_name (char *);
+extern void escape_display_name (char *, size_t);
 
 int
 main(int argc, char *argv[])
 {
        struct passwd *pwd;
        char buf[BUFSIZ], *p;
-       char *name = buf;
 
+        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());
@@ -34,12 +36,8 @@ main(int argc, char *argv[])
 
                strncpy(buf, pwd->pw_gecos, sizeof(buf));
                buf[sizeof(buf) - 1] = '\0';
-       } else if (argc == 2) {
-               name = argv[1];
-       } else if (argc > 2) {
-               fprintf (stderr, "usage: %s [name]\n", argv[0]);
-               return 1;
-       }
+       } else
+               strncpy(buf, argv[1], sizeof(buf));
 
        /*
         * Perform the same processing that getuserinfo() does.
@@ -48,15 +46,14 @@ main(int argc, char *argv[])
        /*
         * Stop at the first comma.
         */
-       if ((p = strchr(name, ',')))
+       if ((p = strchr(buf, ',')))
                *p = '\0';
 
        /*
         * Quote the entire string if it has a special character in it.
         */
-       escape_display_name (name);
-
-       printf("%s\n", name);
+       escape_display_name (buf, sizeof(buf));
+       puts(buf);
 
        exit(0);
 }