获取linux系统的用户名

1. 获取用户信息
        1.1 获取用户名和uid
        1.2 getuid与getlogin实例
        1.3 获取详细的用户信息
        1.4 getpwuid, getpwnam函数实例
        1.5. getpwuid简单实现
2. 获取系统信息
        2.1 相关函数
        2.2 实例


        本文简单的对linux中获取用户信息和系统相关信息进行学习。

1. 获取用户信息

        1.1 获取用户名和uid
  1. #include <sys/types.h>
  2. #include <unistd.h>

  3. uid_t getuid(void);
  4. char *getlogin(void);

  5. getuid函数返回程序关联的UID,它通常是启动程序的用户的UID。
  6. getlogin函数返回与当前用户关联的登录名。
复制代码
1.2 getuid与getlogin实例
  1. filename:1.c
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <stdio.h>

  5. int main(void)
  6. {
  7.         printf("The UID is %d \n", getuid());
  8.         printf("The login name is %s\n", getlogin());
  9.         return 0;
  10. }
复制代码
测试:
  1. [test@test_net code]$ ./1
  2. The UID is 500
  3. The login name is test
  4. [test@test_net code]$
复制代码
1.3 获取详细的用户信息

        系统文件/etc/passwd包含一个用户帐号数据库。它由行组成,每行对应一个用户,包括:
        用户名、加密口令、用户标识符(UID)、组标识符(GID)、全名、主目录和默认shell。
        编程接口的数据结构:
        strcut passwd {
                char *pw_name;
                char *pw_passwd;
                uid_t pw_uid;
                gid_t pw_gid;
                char *pw_gecos;
                char *pw_dir;
                char *pw_shell;
        }
  1. #include <sys/types.h>
  2. #include <pwd.h>
  3. struct passwd *getpwuid(uid_t uid);
  4. struct passwd *getpwnam(const char *name);

  5.         这两个函数都返回一个指针,指向passwd结构。出错时,返回NULL,并设置errno。
  6. void endpwent(void);
  7. void setpwent(void);
  8. struct passwd *getpwent(void);

  9.         这一组函数能够扫描整个文件。getpwent函数依次返回每个用户的信息项,并且将指针后移,当到达文件尾时,返回一个空指针。endpwent终止,并释放资源。setpwent函数重置读指针到密码文件的开始位置。
复制代码
1.4 getpwuid, getpwnam函数实例
  1. filename:2.c
  2. #include <assert.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <pwd.h>
  6. #include <stdio.h>

  7. int main(void)
  8. {
  9.         struct passwd *pw;
  10.         uid_t        uid;
  11.         char        *login;

  12.         uid = getuid();
  13.         assert(uid != -1);
  14.         printf("UID is %d\n", uid);

  15.         login = getlogin();
  16.         assert(login != NULL);
  17.         printf("User is %s\n", login);
  18.        
  19.         pw = getpwuid(uid);
  20.         assert(pw != NULL);
  21.         printf("\n\n\n");
  22.         printf("getpwuid:\n");
  23.         printf("name = %s\nuid = %d\ngid = %d\nhome = %s\nshell = %s\n",\
  24.                         pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);

  25.         pw = getpwnam("root");
  26.         assert(pw);
  27.         printf("\n\n\n");
  28.         printf("getpwnam(root):\n");
  29.         printf("name = %s\nuid = %d\ngid = %d\nhome = %s\nshell = %s\n",\
  30.                         pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);

  31.         return 0;
  32. }
复制代码
测试:
  1. [test@test_net code]$ ./2
  2. UID is 500
  3. User is test



  4. getpwuid:
  5. name = test
  6. uid = 500
  7. gid = 500
  8. home = /home/test
  9. shell = /bin/bash



  10. getpwnam(root):
  11. name = root
  12. uid = 0
  13. gid = 0
  14. home = /root
  15. shell = /bin/bash
  16. [test@test_net code]$
复制代码
1.5. getpwuid简单实现
  1. filename:my_getpwuid.c

  2. #include <pwd.h>

  3. static struct passwd *
  4. my_getpwuid(uid_t uid)
  5. {
  6.         struct passwd *ptr;

  7.         setpwent();
  8.         while ((ptr = getpwent()) != 0) {
  9.                 if (uid == ptr->pw_uid)
  10.                         break;
  11.         }
  12.         endpwent();

  13.         return ptr;
  14. }
复制代码
2. 获取系统信息

        2.1 相关函数
  1. #include <unistd.h>
  2. int gethostname(char *name, size_t namelen);
  3.         该函数把机器的网络名写入name字符串。
  4. #include <sys/utsname.h>
  5. int uname(struct utsname *name);
  6.         uname函数把主机信息写入name参数指向的结构。
  7. struct utsname {
  8.         char sysname[];
  9.         char nodename[];
  10.         char release[];
  11.         char version[];
  12.         char machine[];
  13. };
复制代码
2.2 实例
  1. filename:3.c
  2. #include <unistd.h>
  3. #include <sys/utsname.h>
  4. #include <stdio.h>

  5. int main(void)
  6. {
  7.         char computer[256];
  8.         struct utsname uts;

  9.         if (gethostname(computer, 255) != 0 ||  uname(&uts) != 0) {
  10.                 fprintf(stderr, "can not get host information\n");
  11.                 return 1;
  12.         }

  13.         printf("host name: %s\n", computer);
  14.         printf("system name: %s\n", uts.sysname);
  15.         printf("hardware : %s\n", uts.machine);
  16.         printf("nodename : %s\n", uts.nodename);
  17.         printf("release: %s\n", uts.release);
  18.         printf("version: %s\n", uts.version);

  19.         return 0;
  20. }
复制代码
  1. [test@test_net code]$ ./3
  2. host name: test_net
  3. system name: Linux
  4. hardware : x86_64
  5. nodename : test_net
  6. release: 2.6.40.4-5.fc15.x86_64
  7. version: #1 SMP Tue Aug 30 14:38:32 UTC 2011
  8. [test@test_net code]$
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值