summaryrefslogtreecommitdiff
path: root/coreutils/logname.c (plain)
blob: a9b1c956dd470e0d3a25180a362ec6eb5d1b1608
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"
20//config: default y
21//config: help
22//config: logname is used to print the current user's login name.
23
24//applet:IF_LOGNAME(APPLET_NOFORK(logname, logname, BB_DIR_USR_BIN, BB_SUID_DROP, logname))
25
26//kbuild:lib-$(CONFIG_LOGNAME) += logname.o
27
28/* BB_AUDIT SUSv3 compliant */
29/* http://www.opengroup.org/onlinepubs/007904975/utilities/logname.html */
30
31//usage:#define logname_trivial_usage
32//usage: ""
33//usage:#define logname_full_usage "\n\n"
34//usage: "Print the name of the current user"
35//usage:
36//usage:#define logname_example_usage
37//usage: "$ logname\n"
38//usage: "root\n"
39
40#include "libbb.h"
41
42/* This is a NOFORK applet. Be very careful! */
43
44int logname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45int logname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
46{
47 char buf[64];
48
49 if (argv[1]) {
50 bb_show_usage();
51 }
52
53 /* Using _r function - avoid pulling in static buffer from libc */
54 if (getlogin_r(buf, sizeof(buf)) == 0) {
55 puts(buf);
56 return fflush_all();
57 }
58
59 bb_perror_msg_and_die("getlogin");
60}
61