UNIX系统文件
密码文件
密码文件又称用户数据库,一般为/etc/passwd,对应的结构为struct passwd,该文件内容大体如下:
| 描述 | passwd字段 | 
| 用户名 | char* pw_name | 
| 加密密码 | char* pw_passwd | 
| UID | uid_t pw_uid | 
| GID | gid_t pw_gid | 
| 注释 | char* pw_gecos | 
| 初始工作目录 | char* pw_dir | 
| shell | char* pw_shell | 
加密密码一般一个字符,如果没有值说明该用户没有密码,这个字符只是表象,不是真正的加密密码,加密密码在/etc/shadow中
shell值为shell解释器程序的绝对路径,shell为/bin/false /bin/true /dev/null 都是禁止登录的
用户名为nobody的用户,系统允许向系统写日志,只能访问那些能被其他访问的文件
通过下面函数可以获取对应的struct passwd结构:
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
//Both return: pointer if OK, NULL on error
这两个函数返回值都是静态存储的,所以在此调用内容会被覆盖,在多线程环境下应该使用可重入的替代函数:
int getpwnam_r(const char *name, struct passwd *pwd,char *buf, size_t buflen, struct passwd **result); int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, size_t buflen, struct passwd **result);
成功返回0,*result值pwd,失败返回0,*result 为NULL
如果想遍历所有结构,使用下面的函数:
#include <pwd.h>
struct passwd *getpwent(void);//返回一条密码数据库记录
//Returns: pointer if OK, NULL on error or end of file
void setpwent(void);//返回到密码数据的开始
void endpwent(void);//关闭密码数据库
下面遍历数据库,查找对应的结构:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h> int serchsth(const char* name)
{
struct passwd *ptr;
int ret = -1;
setpwent(); while((ptr = getpwent())!= NULL )
{ if(strcmp(name,ptr->pw_name) == 0 )
{
printf("we got something:%s\n",ptr->pw_name);
ret = 0;
break;
}
}
endpwent();
return ret;
} int main(int argc,char* argv[])
{
return serchsth("hero");
}
加密密码存在/etc/shadow中,加密算法是单向加密,所以很通过加密密码难猜测出原始密码,对应的有函数获取对应的加密记录以及遍历所有记录:
#include <shadow.h>
struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
//Both return: pointer if OK, NULL on error
//下面是上面两个可重入对应函数
int getspent_r(struct spwd *spbuf, char *buf, size_t buflen, struct spwd **spbufp);
int getspnam_r(const char *name, struct spwd *spbuf,char *buf, size_t buflen, struct spwd **spbufp); void setspent(void);
void endspent(void);
同样地有相似获取组信息的函数
#include <grp.h>
struct group *getgrent(void);
//Returns: pointer if OK, NULL on error or end of file
void setgrent(void);
void endgrent(void);
一个用户拥有一个组之外还可以属于多个组,这些组称为附加组,通过下面函数可以获取和设置附加组:
#include <sys/types.h>
#include <unistd.h> int getgroups(int size, gid_t list[]); #include <grp.h> int setgroups(size_t size, const gid_t *list); #include <sys/types.h>
#include <grp.h> int initgroups(const char *user, gid_t group); //return 0 sucess -1 failed
getgroups 获取size指定个数的附加组,值存在list中,当size为0的时候,返回附加组个数。这可以用来确定要开辟多大的数组。
setgroups设置附加组,initgroups获取用户对应的组信息,只有特权才能操作这两个函数。
系统信息
获取当前主机和操作系统信息:
#include <sys/utsname.h>
       int uname(struct utsname *buf);
      // On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
获取主机名:
#include <unistd.h>
int gethostname(char *name, int namelen);
//Returns: 0 if OK, −1 on error
下面的程序获取系统信息和主机信息:
#include <stdio.h>
#include <sys/utsname.h>
#include <unistd.h> int main(int argc,char* argv[])
{
struct utsname buf;
char name[128]; if(uname(&buf))
{
printf("get sys info faild:\n");
return -1;
} if(gethostname(name,128))
{
printf("get hostname failed\n");
return -1;
} printf("system name:%s,node name:%s,release:%s,version:%s,hardware identifier:%s\n",\
buf.sysname,buf.nodename,buf.release,buf.version,buf.machine); printf("hostname:%s\n",name); return 0;
}
时间相关
UNIX使用UTC时间而不是本地时间,自动转换夏令时,时间和日期在一起表示,获取日历时间:
#include <time.h>
time_t time(time_t *calptr);
Returns: value of time if OK, −1 on error
更高精度的时间处理:
#include <time.h>
       int clock_getres(clockid_t clk_id, struct timespec *res);
       int clock_gettime(clockid_t clk_id, struct timespec *tp);
       int clock_settime(clockid_t clk_id, const struct timespec *tp);
      // Link with -lrt (only for glibc versions before 2.17).
// clock_gettime(),  clock_settime()  and  clock_getres() return 0 for success, or -1 for failure
clk_id取值如下:
CLOCK_REALTIME //real system time
CLOCK_MONOTIME //real system time without negative jumps
CLOCK_PROCESS_CPUTIME_ID //CPU time for calling process
CLOCK_THREAD_CPUTIME_ID //CPU time for calling thread
clock_gerres获取时间精度,clock_gettime获取更精确的时间,clock_settime设置时间,需要特权。
不鼓励用但是大面积用的函数:
#include <sys/time.h>
       int gettimeofday(struct timeval *tv, struct timezone *tz); //tz 一般取NULL不用管
       int settimeofday(const struct timeval *tv, const struct timezone *tz);//tz或者tv为NULL函数不会起作用
//0 sucess,-1 error
时间本地化和UTC使用的结构:
struct tm { /* a broken-down time */
int  tm_sec; /* seconds after the minute: [0 - 60] */
int  tm_min; /* minutes after the hour: [0 - 59] */
int  tm_hour;  /* hours after midnight: [0 - 23] */
int  tm_mday;  /* day of the month: [1 - 31] */
int  tm_mon; /* months since January: [0 - 11] */
int  tm_year;  /* years since 1900 */
int  tm_wday;  /* days since Sunday: [0 - 6] */
int  tm_yday;  /* days since January 1: [0 - 365] */
int  tm_isdst; /* daylight saving time flag: <0, 0, >0 */
};
日历时间转为本地或UTC时间对应的函数:
#include <time.h>
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);
//Both return: pointer to broken-down time, NULL on error
本地转为日历时间:
#include <time.h>
time_t mktime(struct tm *tmptr);
//Returns: calendar time if OK, −1 on error
时间格式化:
#include <time.h>
size_t strftime(char *restrict buf, size_t maxsize,const char *restrict format,const struct tm *restrict tmptr);
size_t strftime_l(char *restrict buf, size_t maxsize,const char *restrict format,const struct tm *restrict tmptr, locale_t locale);
//Both return: number of characters stored in array if room, 0 otherwise
字符串转化为时间结构:
#include <time.h>
char *strptime(const char *restrict buf, const char *restrict format,struct tm *restrict tmptr);
//Returns: pointer to one character past last character parsed, NULL otherwise
看一个综合的小程序:
#include <stdio.h>
#include <time.h>
#include <sys/time.h> int main(int argc,char* argv[])
{
time_t curtime;
struct tm* st_tm;
struct timespec res;
char buf[128]; curtime = time(NULL); printf("big value time:%lld\n",(long long)curtime); if(curtime == -1)
{
printf("get curtime failed\n");
return -1;
} if(clock_gettime(CLOCK_REALTIME,&res))
{
printf("get high resolution time failed\n");
return -1;
} if((st_tm = localtime(&curtime))==NULL)
{
printf("gettimeofday failed\n");
return -1;
} if(strftime(buf,128,"%F %T",st_tm)== 0)
{
printf("format time failed\n");
return -1;
} printf("now:%s\n",buf); if(!strptime(buf,"%F %T",st_tm))
{
printf("convert time back failed\n");
return -1;
} curtime = mktime(st_tm); printf("backed time:%lld\n",(long long )curtime); return 0; }
UNIX系统文件的更多相关文章
- Unix command 积累
		UNIX is a multi-user multitasking-optimized operating system that can run on various hardware platfo ... 
- Socket为什么要翻译成套接字
		作者:陈振玥链接:https://www.zhihu.com/question/21383903/answer/64103663来源:知乎著作权归作者所有,转载请联系作者获得授权. 作为一条刻(wu) ... 
- django开发者模式中的autoreload是怎样实现的
		在开发django应用的过程中,使用开发者模式启动服务是特别方便的一件事,只需要 python manage.py runserver 就可以运行服务,并且提供了非常人性化的autoreload机制, ... 
- Socket套接字
		socket编程,熟悉吧,那到底什么是socket呢?: 我们经常把socket翻译为套接字,socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口,来供应用 ... 
- Python网络编程篇之socket
		1 socket 插座?呵呵,想多了,翻译过来意思是套接字! A network socket is an internal endpoint for sending or receiving dat ... 
- Vim配置及使用技巧
		要说Linux下比较好用的文本编辑器,我推荐vim(当然很多人都用emacs,可我没用过),用vim也有一年左右,有些心得体会想与诸位分享.在我的学习过程中,借鉴了不少优秀的博客,其中有csdn大神n ... 
- UID, EUID, SUID, FSUID
		摘自:https://blog.csdn.net/wh8_2011/article/details/50825340 UID, EUID, SUID, FSUID 2016年03月08日 10:40: ... 
- ftp - Internet 文件传输程序 (file transfer program)
		概述 (SYNOPSIS) ftp [-pinegvd ] [host ] pftp [-inegvd ] [host ] 说明 (DESCRIPTION) 用户通过 Ftp 这个程序来使用 Inte ... 
- 【RocketMQ源码分析】深入消息存储(1)
		最近在学习RocketMQ相关的东西,在学习之余沉淀几篇笔记. RocketMQ有很多值得关注的设计点,消息发送.消息消费.路由中心NameServer.消息过滤.消息存储.主从同步.事务消息等等. ... 
随机推荐
- Zookeeper安装部署
			Zookeeper安装 1. 安装 wget http://www.apache.org/dist//zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz ... 
- 使用List,Dictionary加载数据库中的数据
			情景描述:数据库中有一张设备表,字段DWDM存放的是各个厂编号,字段ZNBH存放的是设备编号.其中DWDM跟ZNBH是一对多的关系.需要将数据库中的值加载到List<Dictionary< ... 
- spring 定时任务的 执行时间设置规则(转)
			spring 定时任务的 执行时间设置规则 单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运 ... 
- C++:虚函数的详解
			5.4.2 虚函数详解 1.虚函数的定义 虚函数就是在基类中被关键字virtual说明,并在派生类重新定义的函数.虚函数的作用是允许在派生类中重新定义与基类同名的函数,并且可以通过基类指针或引用来访问 ... 
- 极客编程必备的五大PHP开发应用
			有了PHP应用可以帮助编码爱好者事半功倍,提升项目质量:有了这些最新的且灵活的PHP应用使创建编码项目更加简单.便捷.本文,我们收集了五大最新的PHP开发应用. PHP应用在网络上并不多见.最重要的是 ... 
- VB 语言学习笔记.
			暂时用到,学习学习. 变量声明 Dim 变量名 as 数据类型类型 Set 实例 = new 类名 自定义数据类型 Type 数据类型标识符 域名 As 数据类型; 域名 As 数据类型; 域名 As ... 
- vim编程 插入  保存不退出   保存退出   退出不保存 另存为其他文件名   保存覆盖现有文件
			---恢复内容开始--- 在xshell里写代码,如果需要编辑代码,可以输入 vim+ xxx.py ,进入vim编辑界面 这里的xxx.py表示 python的存储文件,后缀名是.py. 1. ... 
- C# 按拼音/笔划 排序的简单示例(转)
			class Program { static void Main(string[] args) { string[] arr = { "趙(ZHAO)", "錢(QIAN ... 
- AVL的旋转
			转自http://blog.csdn.net/gabriel1026/article/details/6311339 平衡二叉树在进行插入操作的时候可能出现不平衡的情况,AVL树即是一种自平衡的二叉树 ... 
- URAL 1992
			CVS Description Yoda: Visit I will the cloners on Kamino... And see this army they have created for ... 
