blob: 31ce61f0c2fbbb9f8fa42483bbe0a1317c98c52d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen31638212000-01-15 22:28:50 +00002/*
3 * Mini logname implementation for busybox
4 *
5 * Copyright (C) 2000 Edward Betts <edward@debian.org>.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen31638212000-01-15 22:28:50 +00008 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00009/* 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
Eric Andersenaff114c2004-04-14 17:51:38 +000016 * a diagnostic message and an error return.
Manuel Novoa III cad53642003-03-19 09:13:01 +000017 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018//config:config LOGNAME
Denys Vlasenkob097a842018-12-28 03:20:17 +010019//config: bool "logname (1.1 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010020//config: default y
21//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: logname is used to print the current user's login name.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010023
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 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000030
Pere Orga34425382011-03-31 14:43:25 +020031//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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
Erik Andersen31638212000-01-15 22:28:50 +000041
Denis Vlasenko99912ca2007-04-10 15:43:37 +000042/* This is a NOFORK applet. Be very careful! */
43
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000044int logname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010045int logname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Erik Andersene49d5ec2000-02-08 19:58:47 +000046{
Denys Vlasenkod069e532009-09-09 23:12:10 +020047 char buf[64];
Erik Andersen31638212000-01-15 22:28:50 +000048
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010049 if (argv[1]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000052
Denis Vlasenko99912ca2007-04-10 15:43:37 +000053 /* Using _r function - avoid pulling in static buffer from libc */
54 if (getlogin_r(buf, sizeof(buf)) == 0) {
55 puts(buf);
Denys Vlasenko8131eea2009-11-02 14:19:51 +010056 return fflush_all();
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 }
58
59 bb_perror_msg_and_die("getlogin");
Erik Andersen31638212000-01-15 22:28:50 +000060}