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.消息过滤.消息存储.主从同步.事务消息等等. ...
随机推荐
- SpringMVC学习总结(四)——基于注解的SpringMVC简单介绍
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request ...
- Orcle数据库查询练习复习:三
一.题目 1.与“张三”同乡的男生姓名 select * from student where snativeplace=(select snativeplace from student where ...
- alias 命令
功能说明:设置指令的别名. 语 法:alias[别名]=[指令名称] 参 数 :若不加任何参数,则列出目前所有的别名设置. 举 例 :ermao@lost-desktop:~$ alias ...
- Buffer数据结构和new IO的Memory-mapped files
一.Buffer类 java.nio.Buffer这个类是用来干什么的?有怎样的结构? "Core Java"中是这样定义的“A buffer is array of values ...
- shell编程基础(5)---循环指令
while类型的循环 while类型的循环是不定循环的一种,每一次循环都会验证给出的循环条件,判断是否要进行下一次循环.linux中while循环的写法和c语言中很想,但是条件给出的方式有些区别. 首 ...
- opencv 画延长线
hough变换可以让我们检测到直线,这在前面已有详解,对于车道检测,我们需要其到图像边界的延长线一遍之后数据帧分析. 以下代码帮助我们在opencv中画延长线,本来想用虚线表示延长线的,无奈参数调不好 ...
- jQuery 表格排序插件 Tablesorter 使用
jQuery 表格排序插件 Tablesorter 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面): <script src="lib/jquery-1.8.3.m ...
- Learning Lua Programming (2) Lua编程基础
开始学习Lua编程,首先从一些简单的语法开始. 一.编辑环境 下面推荐一个Lua编程的IDE,感觉是很强大的.ZeroBrane Studio,windows平台,mac平台都有.点击打开链接 官方 ...
- WIFI环境下Android手机和电脑通信
前面已经写过一篇java实现最基础的socket网络通信,这篇和之前那篇大同小异,只是将客户端代码移植到手机中,然后获取本机IP的方法略有不同. 先讲一下本篇中用到Android studio的使用吧 ...
- hdu4576 概率dp n^2的矩阵
这个题目看网上好多题解都是直接O(n*m)卡过.我是这么做的. 对于m次操作,统计每个w的次数.然后对每个w做矩阵乘法. 这样直接做矩阵乘法是会TLE的. 又由于这里的矩阵很特殊,一次乘法可以降维成O ...