赞
踩
stat()函数获取文件信息
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
stat结构体:
truct stat { dev_t st_dev; /* ID of device containing file */文件使用的设备号 ino_t st_ino; /* inode number */ 索引节点号 mode_t st_mode; /* protection */ 文件类型和权限 nlink_t st_nlink; /* number of hard links */ 文件的硬连接数 uid_t st_uid; /* user ID of owner */ 用户识别号 gid_t st_gid; /* group ID of owner */ 组识别号 dev_t st_rdev; /* device ID (if special file) */ 设备文件的设备号 off_t st_size; /* total size, in bytes */ 以字节为单位的文件容量 blksize_t st_blksize; /* blocksize for file system I/O */ 包含该文件的磁盘块的大小 blkcnt_t st_blocks; /* number of 512B blocks allocated */ 该文件所占的磁盘块 time_t st_atime; /* time of last access */ 最后一次访问该文件的时间 time_t st_mtime; /* time of last modification */ /最后一次修改该文件的时间 time_t st_ctime; /* time of last status change */ 最后一次改变该文件状态的时间 };
st_mode如下表,其中15-12 位保存文件类型,11-9 位保存执行文件时设置的信息, 8-0 位保存文件访问权限
S_IFMT 0170000 bit mask for the file type bit field (和st_mode按位与使用:xx.st_mode & S_IFMT) S_IFSOCK 0140000 socket S_IFLNK 0120000 symbolic link S_IFREG 0100000 regular file S_IFBLK 0060000 block device S_IFDIR 0040000 directory S_IFCHR 0020000 character device S_IFIFO 0010000 FIFO S_ISUID 04000 set-user-ID bit S_ISGID 02000 set-group-ID bit (see below) S_ISVTX 01000 sticky bit (see below) S_IRWXU 00700 owner has read, write, and execute permission S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission S_IRWXG 00070 group has read, write, and execute permission S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 others (not in group) have read, write, and execute permission S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission
POSIX中定义了检查这些文件类型的宏定义:
S_ISREG(m) is it a regular file? 一般文件
S_ISDIR(m) directory? 目录
S_ISCHR(m) character device? 字符设备
S_ISBLK(m) block device? 块设备
S_ISFIFO(m) FIFO (named pipe)? 命名管道
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) 符号链接
S_ISSOCK(m) socket? (Not in POSIX.1-1996.) 套接字
utime()函数可用于修改文件最后读写的时间
#include <sys/types.h>
#include <utime.h>
int utime(const char *filename, const struct utimbuf *times);
struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};
chmod更改文件权限 直接使用shell命令 chmod xxx或函数chmod()更改
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
umask设置了用户创建文件的默认权限,为权限的“补码”,终端上用umask命令查看,umask xxx可修改,一般默认为0022
所以一般文件的默认权限为0666 & ~022 = 0644, 目录的默认权限为0777 & ~022 = 0755
函数umask()也可修改umask
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t mask);
硬链接可以看成是指向inode号的路径名,一个inode对应的多个文件名就是若干个硬链接,不能对目录进行创建也不能跨文件系统。硬链接数为0文件节点才会真正被释放。可以用命令ln创建或者函数link()创建,unlink删除硬链接。
#include <unistd.h>
int link(const char *oldpath, const char *newpath);
int unlink(const char *pathname);
符号连接与windows下的快捷方式类似,实际上是一个文本文件,其中包含的有另一文件的位置信息。可以用命令ln -s创建
使用命令mkdir或函数mkdir()创建一个目录
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
使用命令rmdir或函数rmdir()(为空时才能成功)删除一个目录
#include <unistd.h>
int rmdir(const char *pathname);
使用命令cd或函数chdir更改工作路径
#include <unistd.h>
int chdir(const char *path);
int fchdir(int fd);
使用命令pwd或函数getcwd()得到工作路径
#include <unistd.h>
char *getcwd(char *buf, size_t size);
char *getwd(char *buf);
char *get_current_dir_name(void);
glob()函数解析目录,flags能选择匹配模式,errfunc查看错误信息可置为NULL。
#include <glob.h>
int glob(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob);
void globfree(glob_t *pglob);
typedef struct {
size_t gl_pathc; /* Count of paths matched so far */
char **gl_pathv; /* List of matched pathnames. */
size_t gl_offs; /* Slots to reserve in gl_pathv. */
} glob_t;
例如用glob()查看etc下的隐藏文件
#include <stdio.h> #include <stdlib.h> #include <glob.h> #define PAT "/etc/.*" int main() { glob_t globres; int i,err; err = glob(PAT,0,NULL,&globres); if(err) { printf("Error code = %d\n",err); exit(1); } for(i = 0; i < globres.gl_pathc; i++) puts(globres.gl_pathv[i]); globfree(&globres); //释放globres return 0; }
或者用目录流的方式进行操作:
#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd); /******************************************************************/ #include <dirent.h> struct dirent *readdir(DIR *dirp); int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); struct dirent { ino_t d_ino; /* Inode number */ off_t d_off; /* Not an offset; see below */ unsigned short d_reclen; /* Length of this record */ unsigned char d_type; /* Type of file; not supported by all filesystem types */ char d_name[256]; /* Null-terminated filename */ }; /******************************************************************/ #include <sys/types.h> #include <dirent.h> void rewinddir(DIR *dirp); /******************************************************************/ #include <dirent.h> void seekdir(DIR *dirp, long loc); /******************************************************************/ #include <dirent.h> long telldir(DIR *dirp); /******************************************************************/ #include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp);
/etc/passwd文件存储用户的帐户信息,getpwnam()可查询用户相关信息
#include <sys/types.h> #include <pwd.h> struct passwd *getpwnam(const char *name); struct passwd *getpwuid(uid_t uid); 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); struct passwd { char *pw_name; /* username */ char *pw_passwd; /* user password */ uid_t pw_uid; /* user ID */ gid_t pw_gid; /* group ID */ char *pw_gecos; /* user information */ char *pw_dir; /* home directory */ char *pw_shell; /* shell program */ };
/etc/group文件中记录了用户所属的主组,getgrnam()可查组相关信息
#include <sys/types.h> #include <grp.h> struct group *getgrnam(const char *name); struct group *getgrgid(gid_t gid); int getgrnam_r(const char *name, struct group *grp, char *buf, size_t buflen, struct group **result); int getgrgid_r(gid_t gid, struct group *grp, char *buf, size_t buflen, struct group **result); struct group { char *gr_name; /* group name */ char *gr_passwd; /* group password */ gid_t gr_gid; /* group ID */ char **gr_mem; /* NULL-terminated array of pointers to names of group members */ };
/etc/shadow 文存储 了Linux 系统中用户的密码信息,用下面函数可对密码操作
#include <shadow.h> struct spwd *getspnam(const char *name); //指定一个用户名,返回用户信息 struct spwd *getspent(void); //打开影子文件并按顺序读取各项 void setspent(void); //回到影子文件开头 void endspent(void); //关闭影子文件 struct spwd { char *sp_namp; /* Login name */ char *sp_pwdp; /* Encrypted password */ long sp_lstchg; /* Date of last change (measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */ long sp_min; /* Min # of days between changes */ long sp_max; /* Max # of days between changes */ long sp_warn; /* # of days before password expires to warn user to change it */ long sp_inact; /* # of days after password expires until account is disabled */ long sp_expire; /* Date when account expires (measured in days since 1970-01-01 00:00:00 +0000 (UTC)) */ unsigned long sp_flag; /* Reserved */ }; /******************************************************************/ #define _XOPEN_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> char *crypt(const char *key, const char *salt);//crypt()算法会接受一个最长可达8字符的密钥(即key),并施以数据加密算法(DES)的一种变体。salt参数指向一个两个字符的字符串,用来扰动(改变)DES算法。该函数返回一个指针,指向长度13个字符的字符串。编译时链接crypt库。 /******************************************************************/ #include <unistd.h> char *getpass(const char *prompt);//关闭回显,读取一行字符串返回并打印prompt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。