whoami.c 663 B

1234567891011121314151617181920212223242526272829303132333435
  1. /* whoami: prints userid on standard output (followed by newline)
  2. mly 850320
  3. Copyright (C) 1985 Richard M. Stallman
  4. ... [generic gnu copyright clause]
  5. */
  6. #include <stdio.h>
  7. #include <pwd.h>
  8. main (argc, argv, envp)
  9. int argc;
  10. char **argv, envp;
  11. {
  12. register struct passwd *p;
  13. register int uid;
  14. if (argc > 1)
  15. {
  16. fprintf (stderr, "%s: No arguments accepted (%d supplied)\n",
  17. argv[0], argc - 1);
  18. exit (1);
  19. }
  20. uid = getuid ();
  21. p = getpwuid (uid);
  22. if (!p)
  23. {
  24. fprintf (stderr,"%s: getuid() error for uid %d.\n",
  25. argv[0], uid);
  26. exit (1);
  27. }
  28. printf ("%s\n", p->pw_name);
  29. exit (0);
  30. }