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.消息过滤.消息存储.主从同步.事务消息等等. ...
随机推荐
- nginx的健康检查功能将挂掉的Tomcat舍弃
1.Ngninx自带健康检查功能,能将挂掉的服务器舍弃,不在访问路径里 2.Nginx还有URL重写功能,能将接收到的请求,进行改写,再将新的URL分发到后端服务器上
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...
- liunx下tomcat启动报错
liunx下tomcat启动 Cannot find ./catalina.sh 2013-08-23 11:50 1521人阅读 评论(0) 收藏 举报 Cannot find ./catalina ...
- Python中的两种结构dict和set
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的成绩 如果 ...
- NSRect
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- linux 开机自启动软件(包含xampp方法)
linux设置apache和mysql: linux开启启动的程序一般放在/etc/rc.d/init.d/里面,/etc/init.d/是其软连接. mysql设为linux服务 cp /usr/l ...
- 学习webView控件使用
WebView 对象用于网页显示使用,简单的学习并使用了一下. 1.首先在 layout 中摆一个全屏的 webview 控件 (main.xml ) <?xml version="1 ...
- ios跳转
目标应用程序:打开info.plist,添加一项URL types展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme展开URL Schem ...
- C# 控件双缓冲控制 ControlStyles 枚举详解
ControlStyles 枚举 .NET Framework 4 指定控件的样式和行为. 此枚举有一个 FlagsAttribute 特性,通过该特性可使其成员值按位组合. 命名空间: Sy ...
- Linux下检查是否安装过某软件包
1.rpm包安装的,可以用 rpm -qa 看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字" 2.以deb包安装的,可以用 dpkg -l ...