在给出用户登录名或数值用户ID后,这两个函数就能查看相关项。

#include <sys/types.h>
#include <pwd.h> struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
两个函数返回值:成功返回指针,出错返回NULL
uid:需要获取信息的uid号

getpwuid例程

 #include <pwd.h>
#include <sys/types.h>
#include <stdio.h> int main()
{
uid_t my_uid; struct passwd *my_info;
my_info = getpwuid(getuid()); printf("my name = [%s]\n", my_info->pw_name);
printf("my passwd = [%s]\n", my_info->pw_passwd);
printf("my uid = [%d]\n", my_info->pw_uid);
printf("my gid = [%d]\n", my_info->pw_gid);
printf("my gecos = [%s]\n", my_info->pw_gecos);
printf("my dir = [%s]\n", my_info->pw_dir);
printf("my shell = [%s]\n", my_info->pw_shell); return ;
}

getpwuid

getpwnam例程

 #include <pwd.h>
#include <sys/types.h>
#include <stdio.h> int main()
{
char *username = "pi";
struct passwd *my_info;
my_info = getpwnam(username);
if(!my_info)
{
perror("getpwnam error");
exit();
} printf("my name = [%s]\n", my_info->pw_name);
printf("my passwd = [%s]\n", my_info->pw_passwd);
printf("my uid = [%d]\n", my_info->pw_uid);
printf("my gid = [%d]\n", my_info->pw_gid);
printf("my gecos = [%s]\n", my_info->pw_gecos);
printf("my dir = [%s]\n", my_info->pw_dir);
printf("my shell = [%s]\n", my_info->pw_shell); return ;
}

getpwnam

也有些程序要查看整个口令文件。

#include <sys/types.h>
#include <pwd.h> struct passwd *getpwent(void);
返回值:成功指针,出错或达文件尾端,返回NULL
void setpwent(void);
void endpwent(void);

apue例程

getpwent

有另一组函数可用于访问阴影口令文件

#include <shadow.h>

struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
两个函数返回值:成功返回指针,出错NULL
void setspent(void);
vpid endspent(void);

查看组名或数值组ID

#include <sys/types.h>
#include <grp.h> struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);
返回值:成功指针,出错NULL

针对口令文件的3个函数

#include <sys/types.h>
#include <grp.h> struct group *getgrent(void);
返回值:成功指针,出错或达到文件尾端,返回NULL
void setgrent(void);
void endgrent(void);

获取和设置附属组ID

#include <sys/types.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);
返回值:成功返回附属组ID数量,出错-
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
两个函数的返回值:成功0,出错-

返回与主机和操作系统有关的信息

#include <sys/utsname.h>

int uname(struct utsname *buf);
返回值:成功非负值,出错-

返回主机名

#include <unistd.h>

int gethostname(char *name, int namelen);
返回值:成功0,出错-

时间部分

time函数返回当前时间和日期

#include <time.h>

time_t time(time_t *t);
返回值:成功返回时间值,出错-

把时间表示为秒和纳秒

#include <time.h>

int clock_gettime(clockid_t clk_id, struct timespec *tp);
返回值:成功0,出错-

clock_getres函数把参数

#include <time.h>

int clock_getres(clockid_t clk_id, struct timespec *res);
返回值:成功0,出错-

要对特定的时钟设置时间,可以调用clock_settime函数

#include <time.h>

int clock_settime(clockid_t clk_id, const struct timespec *tp);
返回值:成功0,出错-

SUSv4指定gettimeofday已经弃用,然而一些程序仍然使用这个函数,因为与time相比,提供了更高的精度

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);
返回值:总是0
#include <time.h>

struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
返回值:指向分解的tm结构的指针,出错NULL

localtime和gmtime之间的区别是,localtime是转换成本地时间,gmtime是将时间结构分解成年月日时分秒周日。

以本地时间的年、月、日等作为参数,将其变换成time_t值

#include <time.h>

time_t mktime(struct tm *tm);
返回值:成功返回日历时间,出错-

类似printf的时间值的函数,通过多个参数来定制产生的字符串

#include <time.h>

size_t strftime(char s, size_t max, const char *format, const struct tm *tm);
返回值:若有空间,返回存入数组的字符数,否则返回0

字符串时间转换成分解时间

#include <time.h>

char *strptime(const char *s, const char *format, struct tm *tm);
返回值:指向上次解析的字符的下一个字符的指针,否则返回NULL

apue 第6章 系统数据文件和信息的更多相关文章

  1. apue学习笔记(第六章 系统数据文件和信息)

    UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件. 口令文件 UNIX系统口令文件包含如下字 ...

  2. UNIX环境高级编程 第6章 系统数据文件和信息

    UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...

  3. UNIX系统高级编程——第六章-系统数据文件和信息-总结

    口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...

  4. APUE学习笔记——6 系统数据文件与信息

    1.用户口令:/etc/passwd文件 该文件中包含下列结构体信息.其中,当下主修熊passwd不再这里显示,是使用了一个占位符. struct passwd { char * pw_name; / ...

  5. (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  6. [APUE]系统数据文件与信息

    一.口令文件 UNIX口令文件包含下表中的各个字段,这些字段包含在 由于历史原因,口令文件是/bin/passwd,而且是一个文本文件,每一行都包括了上表中的七个字段,字段之间用":&quo ...

  7. 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)

    1.UNIX系统口令文件包括了下图所看到的的各字段,这些字段包括在<pwd.h>中定义的passwd结构体中 POSIX定义了两个获取口令文件项的函数. 在给出用户登录名或用户ID后.这两 ...

  8. [06]APUE:系统数据文件和信息

    [a] getpwent / setpwent / endpwent #include <pwd.h> struct passwd *getpwent(void) //成功返回指针,出错或 ...

  9. linux c编程:系统数据文件和信息

    linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...

随机推荐

  1. ConcurrentHashMap1.7源码分析

    参考:https://www.cnblogs.com/liuyun1995/p/8631264.html HashMap不是线程安全的,其所有的方法都未同步,虽然可以使用Collections的syn ...

  2. 【Linux】关闭selinux

    vi /etc/selinux/config 将SELINUX=enforcing改为SELINUX=disabled 设置后需要重启才能生效

  3. express中 使用session与cookie

    1.express如何使用session与cookie : https://www.jianshu.com/p/1839e482274e  或  https://www.cnblogs.com/chy ...

  4. 禅道安装--结合openldap

    原文地址: https://blog.csdn.net/plei_yue/article/details/79075298 ldap结合禅道(需要神道不是开源版) https://www.cnblog ...

  5. [CSP-S模拟测试]:math(裴蜀定理)

    题目传送门(内部题22) 输入格式 第一行有$2$个整数$n,k$.第二行有$n$个正整数$a_i$. 输出格式 第一行有一个整数$s$,表示可以生成的非负整数的个数.第二行有$s$个可以生成的非负整 ...

  6. Pipenv管理项目环境--Django项目的一些最佳实践

    virtualenv --- 使用不方便 提升效率,管理更便捷--- pipenv 新建环境:: pip3 install pipenv 在项目下,用pipenv安装 Djagno pipenv in ...

  7. haskell目录层次

    daniel@daniel-mint /usr/lib/ghc/haskell2010-1.1.1.0 $ tree . ├── Control │   └── Monad.hi ├── Data │ ...

  8. STL中的查找

    一.查找 1.头文件 #include <algorithm> 2.使用方法 1.binary_search:查找某个元素是否出现.O(logn) a.函数模板:binary_search ...

  9. LeetCode 最短无序连续子数组

    题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L ...

  10. [fw]IDT表的初始化

    IDT表的初始化  linux内核的中断描述符表IDT是一个全局的数据,在i386平台上被定义为: struct desc_struct idt_table[256] __attribute__((_ ...