文章目录
- 我是谁 whoami
- 更多信息
我是谁 whoami
我知道你是谁,但我不知道我是谁,此时whoami
可以帮助你,哈哈。
whoami
将打印当前用户的名字。与id -un
类似。
官方定义为:
whoami - print effective userid
用法为:
$ whoami [option] ..
这命令,基本没有参数。
我暂时。。也没有想到为什么会有这个命令。
唯一的可能使你找管理员来配置个啥,然后他需要知道你是谁,不,我是谁。
我看了一下源码,果然简洁:
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>#include "system.h"
#include "die.h"
#include "error.h"
#include "long-options.h"
#include "quote.h"/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "whoami"#define AUTHORS proper_name ("Richard Mlynarik")void
usage (int status)
{if (status != EXIT_SUCCESS)emit_try_help ();else{printf (_("Usage: %s [OPTION]...\n"), program_name);fputs (_("\
Print the user name associated with the current effective user ID.\n\
Same as id -un.\n\
\n\
"), stdout);fputs (HELP_OPTION_DESCRIPTION, stdout);fputs (VERSION_OPTION_DESCRIPTION, stdout);emit_ancillary_info (PROGRAM_NAME);}exit (status);
}int
main (int argc, char **argv)
{struct passwd *pw;uid_t uid;uid_t NO_UID = -1;initialize_main (&argc, &argv);set_program_name (argv[0]);setlocale (LC_ALL, "");bindtextdomain (PACKAGE, LOCALEDIR);textdomain (PACKAGE);atexit (close_stdout);parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,Version, true, usage, AUTHORS,(char const *) NULL);if (optind != argc){error (0, 0, _("extra operand %s"), quote (argv[optind]));usage (EXIT_FAILURE);}errno = 0;uid = geteuid ();pw = (uid == NO_UID && errno ? NULL : getpwuid (uid));if (!pw)die (EXIT_FAILURE, errno, _("cannot find name for user ID %lu"),(unsigned long int) uid);puts (pw->pw_name);return EXIT_SUCCESS;
}
其中使用的即为uid = geteuid ();
。
更多信息
Hi,XDJM们,更多信息欢迎移步我的主页、CSDN或微信公众号letsProgramming.
- 🐱 github
- 🏠 Homepage https://shaoguangleo.github.io
- CSDN CSDN http://blog.csdn.net/shaoguangleo
- 微信公众号:letsProgramming