Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini logname implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2000 Edward Betts <edward@debian.org>. |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 8 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 16 | * a diagnostic message and an error return. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 18 | //config:config LOGNAME |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 19 | //config: bool "logname (1.1 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 20 | //config: default y |
| 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: logname is used to print the current user's login name. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 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 */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 42 | /* This is a NOFORK applet. Be very careful! */ |
| 43 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 44 | int logname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 45 | int logname_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | { |
Denys Vlasenko | d069e53 | 2009-09-09 23:12:10 +0200 | [diff] [blame] | 47 | char buf[64]; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 49 | if (argv[1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 53 | /* Using _r function - avoid pulling in static buffer from libc */ |
| 54 | if (getlogin_r(buf, sizeof(buf)) == 0) { |
| 55 | puts(buf); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 56 | return fflush_all(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | } |
| 58 | |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 59 | bb_simple_perror_msg_and_die("getlogin"); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 60 | } |