Chinaunix首页 | 论坛 | 博客
  • 博客访问: 912863
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-10-27 19:42:52

/* whoami -- print effective userid 打印实际的(有效的)用户id

   Copyright (C) 89,90, 1991-1997, 1999-2002, 2004, 2005 Free Software
   Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, 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 of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

/* Equivalent to `id -un'. */
/* Written by Richard Mlynarik. */
//注意包含的头文件里多了个pwd.h
#include
#include
#include
#include
#include
//包含进来本地的4个头文件
#include "system.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" //定义宏PROGRAM_NAME,其值为一个字符串

#define AUTHORS "Richard Mlynarik" //定义宏PROGRAM_NAME,其值为一个字符串

/* The name this program was run with. */
char *program_name; //声明全局字符指针变量

void
usage (int status)//帮助函数
{
  if (status != EXIT_SUCCESS)//如果传入的形参不等于EXIT_SUCCESS,则:
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);//向标准错误打印一串字符串
  else//否则,如果status等于EXIT_SUCCESS,则打印下面的字符串文本
    {
      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);//这个打印的字符串简单地说明了这个程序的用途,打印当前用户的eid,有效用户id
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);//函数退出的状态值是传入的形参的值
}

int
main (int argc, char **argv)//标准的main函数定义
{
  struct passwd *pw;
  /*
struct passwd 这个结构体是在头文件pwd.h里面定义的,详细如下:
  struct passwd
  {
       char *pw_name; //用户名
 
       char *pw_passwd; //用户密码
 
       uid_t pw_uid; //用户id
 
       uid_t pw_gid; //用户组id
 
       char *pw_gecos; //用户描述
 
       char *pw_dir; //用户家目录
 
       char *pw_shell; //用户登录shell
 
  };


  */
  uid_t uid;//uid_t类型是在标准头文件sys/types.h里面定义的,我还特意进去看了看,结果没有发现它到底是啥类型,待会我测试一下用int或者size_t类型可不可以

  initialize_main (&argc, &argv);//初始化main函数得到的命令行参数
  program_name = argv[0];//将命令行参数的第一个参数赋给program_name
  setlocale (LC_ALL, "");//以下的3行设置本机环境
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);//登记出口函数

  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
              usage, AUTHORS, (char const *) NULL);//解析命令行选项,如果是-h -v的话,就会打印帮助信息
  if (getopt_long (argc, argv, "", NULL, NULL) != -1)//如果getopt_long没有正常返回(因为返回-1才代表它解析完了所有参数,到达了命令行参数的最后了)
    usage (EXIT_FAILURE);//则调用usage函数,传递给usage函数的形参是EXIT_FAILURE

  if (optind != argc)//如果调用getopt_long后得到的值和argc的值不相等,则调用error函数,并调用usage函数
    {
      error (0, 0, _("extra operand %s"), quote (argv[optind]));
      usage (EXIT_FAILURE);
    }

  uid = geteuid ();//uid变量在这里得到值,是通过系统调用geteuid()得到的,其定义为:
//   uid_t geteuid(void);  这个系统调用据说永远不会失败
  pw = getpwuid (uid);
/*
getpwuid库函数的man解释如下:
The getpwuid() function returns a pointer to a structure containing the broken out fields of a line from  /etc/passwd  for
       the entry that matches the user uid uid.
改库函数一个指向/etc/passwd文件的响应uid的行     

*/
  if (pw)//pw是getpwuid()库函数的返回,这个指针返回是空指针的情况为:
  //NULL if the  matching  entry  is  not found  or  an error occurs 就是传入的uid不存在的情况
    {//如果得到的指针pw不是空指针,则输出此结构体里的用户名
      puts (pw->pw_name);
      exit (EXIT_SUCCESS);//此时main函数正常返回
    }
  fprintf (stderr, _("%s: cannot find name for user ID %lu\n"),
       program_name, (unsigned long int) uid);//如果执行流程走到这里,就说明没有正常获取到pw,此时打印错误
  exit (EXIT_FAILURE);//这时候退出状态肯定不是成功了,而是失败
}
 
--------------------
好了,自己简单地实现一下:

[root@bjxdurs235 20091027]# cat my_whoami.c
#include
#include
#include


int main(void)
{
        uid_t uid;
        struct passwd *pw;
        uid = geteuid();
        pw = getpwuid(uid);
        printf("%s\n",pw->pw_name);

}
[root@bjxdurs235 20091027]# ./a.out
root

这whoami是通过geteuid()系统调用、getpwuid()库函数这两个核心操作来实现的,这个程序和logname不同之处是他获取到的是有效用户id对应的用户名,所以如果用户执行了suid程序等之后,结果还是有效用户名。而logname调用的库函数getlogin()按说应该也是得到的有效用户id啊,不过这两个之一肯定有的不能获得一致的,特别是用户的用户id和有效用户id不一致的情况下。




阅读(1480) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~