chmod
int chmod(const char *path, mode_t mode)
将文件read的文件权限变成所有者有读写权限,其他用户只有读的权限。这和shell指令中的chmod有相同的功效。
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h> /* for chmod */
#include <sys/types.h> /* for chmod */
int main(){
system("touch read; ls -l |grep read");
chmod("./read",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
system("ls -l |grep read");
return 0;
}
umask
mode_t umask(mode_t mask)
先不讨论掩码,
创建文件默认最大权限为666 (-rw-rw-rw-),默认创建的文件没有可执行权限x位。
创建目录默认最大权限777(-rwx-rwx-rwx),默认创建的目录属主是有x权限。
linux系统每一个新建的文件都被赋予某种默认的权限,这由权限掩码设置函数umask决定。umask()h函数还将返回上一个mask
umask()会将系统umask值设成参数mask&0777后的值,然后将先前的umask值返回.
$ cat file.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(){
mode_t mask = 0755;
mode_t oldmod = umask(mask);
printf("old mask is 0%o\n",oldmod);
printf("new mask is 0%o\n",umask(mask));
return 0;
}
$ ./file
old mask is 02
new mask is 0755
$ cat file1.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(){
system("touch f1; ls f1 -l");
mode_t mask = 0123;
umask(mask);
system("touch f2; ls f2 -l");
return 0;
}
$ ./file1
-rw-rw-r--. 1 edemon edemon 0 Nov 20 23:06 f1
-rw-r--r--. 1 edemon edemon 0 Nov 20 23:06 f2
分析: 文件默认的权限是val1= 0666,默认的mask是val2 = 02,两者进行运算:
0666&~0002 = 0666&0775 = 0664
第二次,mask是0123
0666&~0123 = 0666&0654 = 0644
stat
int stat(const char *file_name, struct stat *buf)
stat()用来将参数file_name所指的文件状态,复制到参数buf所指的结构中
关于结构体 stat:
[edemon@CentOS include]$ sed -n '39,94p' bits/stat.h
struct stat
{
__dev_t st_dev; /* Device. */
unsigned short int __pad1;
#ifndef __USE_FILE_OFFSET64
__ino_t st_ino; /* File serial number. */
#else
__ino_t __st_ino; /* 32bit file serial number. */
#endif
__mode_t st_mode; /* File mode. */
__nlink_t st_nlink; /* Link count. */
__uid_t st_uid; /* User ID of the file's owner. */
__gid_t st_gid; /* Group ID of the file's group.*/
__dev_t st_rdev; /* Device number, if device. */
unsigned short int __pad2;
#ifndef __USE_FILE_OFFSET64
__off_t st_size; /* Size of file, in bytes. */
#else
__off64_t st_size; /* Size of file, in bytes. */
#endif
__blksize_t st_blksize; /* Optimal block size for I/O. */
#ifndef __USE_FILE_OFFSET64
__blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */
#else
__blkcnt64_t st_blocks; /* Number 512-byte blocks allocated. */
#endif
#if defined __USE_MISC || defined __USE_XOPEN2K8
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */
struct timespec st_atim; /* Time of last access. */
struct timespec st_mtim; /* Time of last modification. */
struct timespec st_ctim; /* Time of last status change. */
# define st_atime st_atim.tv_sec /* Backward compatibility. */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
__time_t st_atime; /* Time of last access. */
unsigned long int st_atimensec; /* Nscecs of last access. */
__time_t st_mtime; /* Time of last modification. */
unsigned long int st_mtimensec; /* Nsecs of last modification. */
__time_t st_ctime; /* Time of last status change. */
unsigned long int st_ctimensec; /* Nsecs of last status change. */
#endif
#ifndef __USE_FILE_OFFSET64
unsigned long int __unused4;
unsigned long int __unused5;
#else
__ino64_t st_ino; /* File serial number. */
#endif
};
<