logname.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini logname implementation for busybox
  4. *
  5. * Copyright (C) 2000 Edward Betts <edward@debian.org>.
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/logname.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * SUSv3 specifies the string used is that returned from getlogin().
  14. * The previous implementation used getpwuid() for geteuid(), which
  15. * is _not_ the same. Erik apparently made this change almost 3 years
  16. * ago to avoid failing when no utmp was available. However, the
  17. * correct course of action wrt SUSv3 for a failing getlogin() is
  18. * a diagnostic message and an error return.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include "busybox.h"
  24. int logname_main(int argc, char ATTRIBUTE_UNUSED **argv)
  25. {
  26. const char *p;
  27. if (argc > 1) {
  28. bb_show_usage();
  29. }
  30. if ((p = getlogin()) != NULL) {
  31. puts(p);
  32. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  33. }
  34. bb_perror_msg_and_die("getlogin");
  35. }