logname.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 source tree.
  8. */
  9. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * SUSv3 specifies the string used is that returned from getlogin().
  12. * The previous implementation used getpwuid() for geteuid(), which
  13. * is _not_ the same. Erik apparently made this change almost 3 years
  14. * ago to avoid failing when no utmp was available. However, the
  15. * correct course of action wrt SUSv3 for a failing getlogin() is
  16. * a diagnostic message and an error return.
  17. */
  18. //config:config LOGNAME
  19. //config: bool "logname (1.1 kb)"
  20. //config: default y
  21. //config: help
  22. //config: logname is used to print the current user's login name.
  23. //applet:IF_LOGNAME(APPLET_NOFORK(logname, logname, BB_DIR_USR_BIN, BB_SUID_DROP, logname))
  24. //kbuild:lib-$(CONFIG_LOGNAME) += logname.o
  25. /* BB_AUDIT SUSv3 compliant */
  26. /* http://www.opengroup.org/onlinepubs/007904975/utilities/logname.html */
  27. //usage:#define logname_trivial_usage
  28. //usage: ""
  29. //usage:#define logname_full_usage "\n\n"
  30. //usage: "Print the name of the current user"
  31. //usage:
  32. //usage:#define logname_example_usage
  33. //usage: "$ logname\n"
  34. //usage: "root\n"
  35. #include "libbb.h"
  36. /* This is a NOFORK applet. Be very careful! */
  37. int logname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int logname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  39. {
  40. char buf[64];
  41. if (argv[1]) {
  42. bb_show_usage();
  43. }
  44. /* Using _r function - avoid pulling in static buffer from libc */
  45. if (getlogin_r(buf, sizeof(buf)) == 0) {
  46. puts(buf);
  47. return fflush_all();
  48. }
  49. bb_perror_msg_and_die("getlogin");
  50. }