coreutils8.32 whoami命令和源码分析
命令功能:显示当前操作用户(不是登陆用户,和logname不一样。登陆用户之后可以su切换操作用户)
执行流程:先用geteuid获取调用进程的用户uid,再用getpwuid通过uid获取完整的passwd结构再输出用户名
/* whoami -- print effective useridCopyright (C) 1989-2020 Free Software Foundation, Inc.This program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program. If not, see <https://www.gnu.org/licenses/>. *//* Equivalent to 'id -un'. */
/* Written by Richard Mlynarik. */#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);/* whoami不能传入选项和参数 */if (optind != argc){error (0, 0, _("extra operand %s"), quote (argv[optind]));usage (EXIT_FAILURE);}errno = 0;uid = geteuid (); /* 获取当前调用进程的用户uid */pw = (uid == NO_UID && errno ? NULL : getpwuid (uid)); /* c语言写法:如果geteuid成功获取用户uid的话就调用getpwuid获取passwd文件下uid的完整用户信息 */if (!pw) /* getpwuid获取失败 */die (EXIT_FAILURE, errno, _("cannot find name for user ID %lu"),(unsigned long int) uid);puts (pw->pw_name); /* 输出用户名 */return EXIT_SUCCESS;
}
coreutils8.32 whoami命令和源码分析相关推荐
- 【原创】【专栏】《Linux设备驱动程序》--- LDD3源码目录结构和源码分析经典链接
http://blog.csdn.net/geng823/article/details/37567557 [原创][专栏]<Linux设备驱动程序>--- LDD3源码目录结构和源码分析 ...
- 并发编程五:java并发线程池底层原理详解和源码分析
文章目录 java并发线程池底层原理详解和源码分析 线程和线程池性能对比 Executors创建的三种线程池分析 自定义线程池分析 线程池源码分析 继承关系 ThreadPoolExecutor源码分 ...
- JAVA-LocalDateTime时间格式化,转换时间戳和源码分析
JAVA-LocalDateTime时间格式化,转换时间戳和源码分析 LocalDateTime LocalDateTime作为java8新加的时间类型,也是后面开发中常用的时间类型.因为没用过之前的 ...
- Google Mock(Gmock)简单使用和源码分析——源码分析
源码分析 通过<Google Mock(Gmock)简单使用和源码分析--简单使用>中的例子,我们发现被mock的相关方法在mock类中已经被重新实现了,否则它们也不会按照我们的期待的行为 ...
- java.lang.ThreadLocal实现原理和源码分析
java.lang.ThreadLocal实现原理和源码分析 1.ThreadLocal的原理:为每一个线程维护变量的副本.某个线程修改的只是自己的副本. 2.ThreadLocal是如何做到把变量变 ...
- java校验框架源码解析_Spring Boot原理剖析和源码分析
Spring Boot原理剖析和源码分析 依赖管理 问题一:为什么导入dependency时不需要指定版本? spring-boot-starter-parent依赖 org.springframew ...
- SRS流媒体服务器——Forward集群搭建和源码分析
SRS流媒体服务器--Forward集群搭建和源码分析 目录 Forward集群原理 RTMP流转发(Forward)部署实例 Forward集群源码分析 1. Forward集群原理 Forward ...
- Android RxJava(一) create操作符的用法和源码分析
RxJava(一) create操作符的用法和源码分析 转载于:https://www.cnblogs.com/zhujiabin/p/7291901.html
- android lottie字体json,从json文件到炫酷动画-Lottie实现思路和源码分析
从json文件到炫酷动画-Lottie实现思路和源码分析,Lottie是最近Airbnb开源的动画项目,支持Android.iOS.ReactNaitve三个平台,本文分析主要Lottie把json文 ...
最新文章
- 独家|一文解读合成数据在机器学习技术下的表现
- Logistic回归总结
- 除留余数法构造哈希表_哈希表算法原理
- GBK、GB2312、GB18030
- Android的sqlite使用外部,Android 使用外部已经建立好的sqlite数据库
- 如何用c 控制mysql数据库_用C语言操作MySQL数据库
- python中max()、min()获得最大值与最小值_(Python基础教程之十)Python max()和min()–在列表或数组中查找最大值和最小值...
- 方便好用的在线CPU性能调优工具--perf的常见用法
- 通过配置文件连接mysql_利用配置文件连接数据库
- 《机器学习基石》笔记:第一讲
- [印刷工艺]从正度纸,大度纸说起
- 根据表达式序列(前缀、中缀、后缀)构建表达式树
- Serval的试卷答案(线段树)
- javafx 教程_新的JMetro JavaFX 11兼容版本
- pull request 时遇到 conflicted 的解决方法
- [论文分享] Overcoming Catastrophic Forgetting in Incremental Few-Shot Learning by Finding Flat Minima
- iOS 如何让APP 删除后不接受 APNS 推送消息
- bootstrap--表格(table的各种样式)
- 安装anaconda时需要卸载python么_怎么卸载python 安装 anaconda
- ESP32CAM--st7789--240x240--1.3inch
热门文章
- WindowsPhone-GameBoy模拟器开发五--使用XNA初略实现Gameboy显示系统
- 大学计算机作业与实验报告东北农业大学,大学课程数字化测图实习报告.doc
- python音乐电台推荐系统的设计与实现毕业设计源码301210
- Vue3 PC桌面端聊天室|vue3.0+elementPlus仿微信/QQ界面
- Jquery简单实现2048小游戏
- font在html语言中是什么意思,font在html中的意思
- netty中pipeline分析_4(异常事件)
- 中文域名目前的注册情况怎么样?
- 梅斯大学计算机类,研究称“世界首台计算机”或诞生于公元前205年
- 开发油猴脚本:给任意网页的选中文字涂色