I forgot to malloc space for the NULL.
[oweals/busybox.git] / whoami.c
index 5c3fea13f673923311261599d725906772600b5a..c3b1140e6c940ddd40460eb7900cb5fcf90bf11d 100644 (file)
--- a/whoami.c
+++ b/whoami.c
  *
  */
 
-#include "internal.h"
-#include <stdio.h>
-#include <pwd.h>
+/* getopt not needed */
 
-static const char whoami_usage[] = "whoami\n\n"
-       "Prints the user name associated with the current effective user id.\n";
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "busybox.h"
 
 extern int whoami_main(int argc, char **argv)
 {
-       struct passwd *pw;
-       uid_t uid;
+       char user[9];
+       uid_t uid = geteuid();
 
        if (argc > 1)
-               usage(whoami_usage);
+               show_usage();
 
-       uid = geteuid();
-       pw = getpwuid(uid);
-       if (pw) {
-               puts(pw->pw_name);
-               exit(TRUE);
+       my_getpwuid(user, uid);
+       if (*user) {
+               puts(user);
+               return EXIT_SUCCESS;
        }
-       fprintf(stderr, "%s: cannot find username for UID %u\n", argv[0],
-                       (unsigned) uid);
-       exit(FALSE);
+       error_msg_and_die("cannot find username for UID %u", (unsigned) uid);
 }