在给出用户登录名或数值用户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. linux 文件及目录结构体系

    linux 目录的特点: 1). /是所有目录的顶点 2).目录结构像一颗倒挂的树 3).目录和磁盘分区是没有关联的 4)./下不同的目录可能对应不同的分区或磁盘 5).所有的目录都是按照一定的类别有 ...

  2. webpack 4.+版本需要注意的地方

    在webpack打包的时候需要npm下载webpack-cli,而且打包JS的命令从以前的webpack .\src\main.js  .\dist\boundle.js 要编程 webpack .\ ...

  3. Design:设计目录

    ylbtech-Design:设计目录 1.返回顶部 1.0 蚂蚁设计 https://design.alipay.com 1.1 Ant Design - 一个 UI 设计语言 https://an ...

  4. java常用排序

    1.冒泡排序 public static int[] bubble(int[] a){ for(int i=0;i<a.length-1;i++){ int tmp=0; for(int j=0 ...

  5. 小程序BUTTON点击,去掉背景色

    添加hover-class <button form-type="submit"  hover-class="btn-hover"></but ...

  6. CF1241 D Sequence Sorting(离散化+DP)

    题意: 给定数组a[n],用两种操作: 1.将数组中所有值为x的数移至开头 2.将数组中所有值为x的数移至末尾 问,经过最少多少次操作,能将数组a[n]变为非递减的有序数列? (1<=n< ...

  7. (转)关于SimpleDateFormat安全的时间格式化线程安全问题

    想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调 ...

  8. UVA1608_Non-boring sequences

    Non-boring sequences 大致题意: 给你一个字符串,问你他的任一子串是否都包含一个唯一的字符 思路: 看似简单,实际一丁点思路都没有 后面看汝佳的讲解都看了好长时间 大概思路就是,先 ...

  9. 【JavaScript性能优化】------理解Script标签的加载和执行

    1.script标签是如何加载的?当浏览器遇到一个 < script>标签时,浏览器会停下来,运行JavaScript代码,然后再继续解析.翻译页面.同样的事情发生在使用 src 属性加载 ...

  10. Cocos2d 之FlyBird开发---GameUnit类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 这节来实现GameUnit类中的一些函数方法,其实这个类一般是一个边写边完善的过程,因为一般很难一次性想全所有的能够供多个类共用的方法.下 ...