APUE: 系统相关的系统调用和库函数

本文主要总结和系统相关的文件和信息的系统调用和库函数。

#######################################################
系统相关的系统调用
#######################################################
 
#include
<sys/types.h>
#include
<unistd.h>
int getgroups(int size, gid_t list[]);
获取附加组ID,成功返回相应值,失败返回-1.
将附加组ID填写到数组list中,size为该数组中最多元素个数。
Size=0,函数返回附加组ID数。
 
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
设置附加组ID表。
 
#include <sys/utsname.h>
int uname(struct utsname *buf);
成功返回非负,将信息填入结构,失败返回-1.
 
struct utsname {
    char sysname[]; // 操作系统名称
    char nodename[]; //节点名称
    char release[]; //操作系统发型版
    char version[]; //操作系统版本
    char machine[]; //硬件类型
#下面是可选
    char domainname[]
};
 
#include <unistd.h>
int gethostname(char *name, size_t len);
获取主机名,len限制name的长度,name返回主机名,成功返回0,失败返回-1.
int sethostname(const char *name, size_t len);
设置主机名。
 
#include <time.h>
time_t time(time_t   *t);
返回从19700101000000进过的秒数,如果t不是NULL也存放在t中。
  
#include <sys/time.h>
int gettimeofday(struct timeval *tp, void *tzp);
tzp必须等于NULL,获取从19700101000000进过的秒数和微妙数,存放在tp结构中。总是返回0.
 
struct timeval {
    time_t tv_sec;
    time_t tv_usec;
};
 
#######################################################
系统相关的库函数
#######################################################
#include <sys/types.h>
#include <pwd.h>
/etc/passwd普通口令文件的操作。
 
struct passwd  *getpwnam(const char *name);
根据用户名返回结构指针,出错返回NULL
struct passwd  *getpwuid(uid_t uid);
根据用户ID返回结构指针,出错返回NULL
 
struct passwd *getpwent(void);
返回口令文件中下一个记录项,失败返回NULL
void setpwent(void);
反绕有关文件,使它们定位到文件开始处。
void endpwent(void);
使用完要关闭这些文件。
 
struct passwd {
    char *pw_name;
    char *pw_passwd;
    uid_t pw_uid;
    gid_t pw_gid;
    char *pw_gecos;//用户信息
    char *pw_dir;//home路径
    char *pw_shell;//登陆的shell
};
 
#include <shadow.h>
对阴影口令文件/etc/shadow的操作
 
struct spwd *getspnam(const char *name);
根据名字返回结构指针,失败返回NULL
 
struct spwd *getspent(void);
返回结构的指针,失败返回NULL
void setspent(void);
反绕有关文件,使它们定位到文件开始处。
void endspent(void);
使用完要关闭这些文件。
 
struct spwd {
//下面两个是必须的
    char *sp_namp; //登录名
    char *sp_pwdp; //加密口令
//下面可选
    long sp_lstchg; //上次更改口令经过的时间
    long sp_min;  //经过多少天后允许更改
    long sp_max; //要求更改尚余天数
    long sp_warm; //到期警告天数
    long sp_inact; //账户不活动之前尚余天数
    long sp_expire; //账户到期天数
    unsigned long sp_flag; //保留
}
 
#include <sys/types.h>
#include <grp.h>
对组文件/etc/group的操作
 
struct group *getgrnam(const char *name);
根据组名返回结构指针,失败返回NULL
struct group *getgrgid(gid_t gid);
根据组ID返回结构指针,失败返回NULL
 
struct group *getgrent(void);
返回结构的指针,失败返回NULL
void setgrent(void);
反绕有关文件,使它们定位到文件开始处。
void endgrent(void);
使用完要关闭这些文件。
 
struct group {
    char *gr_name; //组名
    char *gr_passwd; //组密码
    gid_t gr_gid; //ID
    char **gr_mem; //组成员
};
    
#include <sys/types.h>
#include <grp.h>
int initgroups(const char *user, gid_t group);
实际调用setgroups函数为user用户设置附加组ID
 
#include <time.h>
时间处理函数。
 
struct tm *gmtime(const time_t *timep);
传入从1970开始的日历时间timep,返回国际标准时间(UTC)结构的指针。
utc时间+8小时才是CST时间。
struct tm *localtime(const time_t *timep);
传入从1970开始的日历时间timep,返回本地时间(CST)结构指针。
 
time_t mktime(struct tm *tm);
将本地时间结构转换成1970开始的日历时间秒,返回日历时间秒数。
 
struct tm {
    int tm_sec;//0-60
    int tm_min;//0-59
    int tm_hour;//0-23,Beijing=utc+8
    int tm_mday;//1-31
    int tm_mon;//0-11,+1
    int tm_year;//+1900
    int tm_wday;//0-6,0=sunday
    int tm_yday;//0-365
    int tm_isdst;//>0(夏时制),<0(不用),=0(非夏时制)
};
 
char *ctime(const time_t *timep);
根据日历时间timep,返回本地时间字符串。
 
char *asctime(const struct tm *tm);
根据本地时间或国际时间结构,返回时间字符串。
 
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
传入tm,格式化之后放入max大小的字符串s中。成功返回存入s中的字符数,否则返回0.
 
format
%%:打印%
%c:输出日期和时间
%FYYYY-MM-DD
...
 
#define _XOPEN_SOURCE
char *strptime(const char *s, const char *format, struct tm *tm);
 
double difftime(time_t time1, time_t time0);
 
clock_t clock(void);
CLOCKS_PER_SEC

未完待续......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值