| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 软件开发

Linux常用C函数-文件权限控制篇

时间:2006-11-01 10:25:07  来源:Linux联盟收集  作者:
文件权限控制篇b3tLinux联盟
access,b3tLinux联盟
alphasort,b3tLinux联盟
chdir,b3tLinux联盟
chmod,b3tLinux联盟
chown,b3tLinux联盟
chroot,b3tLinux联盟
closedir,b3tLinux联盟
fchdir,b3tLinux联盟
fchmod,b3tLinux联盟
fchown,b3tLinux联盟
fstat,b3tLinux联盟
ftruncate,b3tLinux联盟
getcwd,b3tLinux联盟
link,b3tLinux联盟
lstat,b3tLinux联盟
opendir,b3tLinux联盟
readdir,b3tLinux联盟
readlink,b3tLinux联盟
remove,b3tLinux联盟
rename,b3tLinux联盟
rewinddir,b3tLinux联盟
seekdir,b3tLinux联盟
stat,b3tLinux联盟
symlink,b3tLinux联盟
telldir,b3tLinux联盟
truncate,b3tLinux联盟
umask,b3tLinux联盟
unlink,b3tLinux联盟
utime,b3tLinux联盟
utimes,b3tLinux联盟
access(判断是否具有存取文件的权限)
相关函数
stat,open,chmod,chown,setuid,setgidb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int access(const char * pathname,int mode);b3tLinux联盟
函数说明
access()会检查是否可以读/写某一已存在的文件。参数mode有几种情况组合,R_OK,W_OK,X_OK 和F_OK。R_OK,W_OK与X_OK用来检查文件是否具有读取、写入和执行的权限。F_OK则是用来判断该文件是否存在。由于access()只作权限的核查,并不理会文件形态或文件内容,因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如,你会发现DOS的文件都具有“可执行”权限,但用execve()执行时则会失败。b3tLinux联盟
返回值
若所有欲查核的权限都通过了检查则返回0值,表示成功,只要有一权限被禁止则返回-1。b3tLinux联盟
错误代码
EACCESS 参数pathname 所指定的文件不符合所要求测试的权限。b3tLinux联盟
EROFS 欲测试写入权限的文件存在于只读文件系统内。b3tLinux联盟
EFAULT 参数pathname指针超出可存取内存空间。b3tLinux联盟
EINVAL 参数mode 不正确。b3tLinux联盟
ENAMETOOLONG 参数pathname太长。b3tLinux联盟
ENOTDIR 参数pathname为一目录。b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
ELOOP 参数pathname有过多符号连接问题。b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
附加说明
使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题。b3tLinux联盟
范例
/* 判断是否允许读取/etc/passwd */b3tLinux联盟
#include<unistd.h>b3tLinux联盟
int main()b3tLinux联盟
{b3tLinux联盟
if (access(“/etc/passwd”,R_OK) = =0)b3tLinux联盟
printf(“/etc/passwd can be read\n”);b3tLinux联盟
}b3tLinux联盟
执行
/etc/passwd can be readb3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
alphasort(依字母顺序排序目录结构)
相关函数
scandir,qsortb3tLinux联盟
表头文件
#include<dirent.h>b3tLinux联盟
定义函数
int alphasort(const struct dirent **a,const struct dirent **b);b3tLinux联盟
函数说明
alphasort()为scandir()最后调用qsort()函数时传给qsort()作为判断的函数,详细说明请参考scandir()及qsort()。b3tLinux联盟
返回值
参考qsort()。b3tLinux联盟
范例
/* 读取/目录下所有的目录结构,并依字母顺序排列*/b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
struct dirent **namelist;b3tLinux联盟
int i,total;b3tLinux联盟
total = scandir(“/”,&namelist ,0,alphasort);b3tLinux联盟
if(total <0)b3tLinux联盟
perror(“scandir”);b3tLinux联盟
else{b3tLinux联盟
for(i=0;i<total;i++)b3tLinux联盟
printf(“%s\n”,namelist[i]->d_name);b3tLinux联盟
printf(“total = %d\n”,total);b3tLinux联盟
}b3tLinux联盟
}b3tLinux联盟
执行
..b3tLinux联盟
.gnomeb3tLinux联盟
.gnome_privateb3tLinux联盟
ErrorLogb3tLinux联盟
Weblogb3tLinux联盟
binb3tLinux联盟
bootb3tLinux联盟
devb3tLinux联盟
doscb3tLinux联盟
dosdb3tLinux联盟
etcb3tLinux联盟
homeb3tLinux联盟
libb3tLinux联盟
lost+foundb3tLinux联盟
miscb3tLinux联盟
mntb3tLinux联盟
optb3tLinux联盟
procb3tLinux联盟
rootb3tLinux联盟
sbinb3tLinux联盟
tmpb3tLinux联盟
usrb3tLinux联盟
varb3tLinux联盟
total = 24b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
chdir(改变当前的工作(目录)
相关函数
getcwd,chrootb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int chdir(const char * path);b3tLinux联盟
函数说明
chdir()用来将当前的工作目录改变成以参数path所指的目录。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,errno为错误代码。b3tLinux联盟
范例
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
chdir(“/tmp”);b3tLinux联盟
printf(“current working directory: %s\n”,getcwd(NULL,NULL));b3tLinux联盟
}b3tLinux联盟
执行
current working directory :/tmpb3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
chmod(改变文件的权限)
相关函数
fchmod,stat,open,chownb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<sys/stat.h>b3tLinux联盟
定义函数
int chmod(const char * path,mode_t mode);b3tLinux联盟
函数说明
chmod()会依参数mode 权限来更改参数path 指定文件的权限。b3tLinux联盟
参数
mode 有下列数种组合b3tLinux联盟
S_ISUID 04000 文件的(set user-id on execution)位b3tLinux联盟
S_ISGID 02000 文件的(set group-id on execution)位b3tLinux联盟
S_ISVTX 01000 文件的sticky位b3tLinux联盟
S_IRUSR(S_IREAD) 00400 文件所有者具可读取权限b3tLinux联盟
S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限b3tLinux联盟
S_IXUSR(S_IEXEC) 00100 文件所有者具可执行权限b3tLinux联盟
S_IRGRP 00040 用户组具可读取权限b3tLinux联盟
S_IWGRP 00020 用户组具可写入权限b3tLinux联盟
S_IXGRP 00010 用户组具可执行权限b3tLinux联盟
S_IROTH 00004 其他用户具可读取权限b3tLinux联盟
S_IWOTH 00002 其他用户具可写入权限b3tLinux联盟
S_IXOTH 00001 其他用户具可执行权限b3tLinux联盟
只有该文件的所有者或有效用户识别码为0,才可以修改该文件权限。基于系统安全,如果欲将数据写入一执行文件,而该执行文件具有S_ISUID 或S_ISGID 权限,则这两个位会被清除。如果一目录具有S_ISUID 位权限,表示在此目录下只有该文件的所有者或root可以删除该文件。b3tLinux联盟
返回值
权限改变成功返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
错误代码
EPERM 进程的有效用户识别码与欲修改权限的文件拥有者不同,而且也不具root权限。b3tLinux联盟
EACCESS 参数path所指定的文件无法存取。b3tLinux联盟
EROFS 欲写入权限的文件存在于只读文件系统内。b3tLinux联盟
EFAULT 参数path指针超出可存取内存空间。b3tLinux联盟
EINVAL 参数mode不正确b3tLinux联盟
ENAMETOOLONG 参数path太长b3tLinux联盟
ENOENT 指定的文件不存在b3tLinux联盟
ENOTDIR 参数path路径并非一目录b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
ELOOP 参数path有过多符号连接问题。b3tLinux联盟
EIO I/O 存取错误b3tLinux联盟
范例
/* 将/etc/passwd 文件权限设成S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH */b3tLinux联盟
#include<sys/types.h>b3tLinux联盟
#include<sys/stat.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
chmod(“/etc/passwd”,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
chown(改变文件的所有者)
相关函数
fchown,lchown,chmodb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
定义函数
int chown(const char * path, uid_t owner,gid_t group);b3tLinux联盟
函数说明
chown()会将参数path指定文件的所有者变更为参数owner代表的用户,而将该文件的组变更为参数group组。如果参数owner或group为-1,对应的所有者或组不会有所改变。root与文件所有者皆可改变文件组,但所有者必须是参数group组的成员。当root用chown()改变文件所有者或组时,该文件若具有S_ISUID或S_ISGID权限,则会清除此权限位,此外如果具有S_ISGID权限但不具S_IXGRP位,则该文件会被强制锁定,文件模式会保留。b3tLinux联盟
返回值
成功则返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
错误代码
参考chmod()。b3tLinux联盟
范例
/* 将/etc/passwd 的所有者和组都设为root */b3tLinux联盟
#include<sys/types.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
chown(“/etc/passwd”,0,0);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
chroot(改变根目录)
相关函数
chdirb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int chroot(const char * path);b3tLinux联盟
函数说明
chroot()用来改变根目录为参数path 所指定的目录。只有超级用户才允许改变根目录,子进程将继承新的根目录。b3tLinux联盟
返回值
调用成功则返回0,失败则返-1,错误代码存于errno。b3tLinux联盟
错误代码
EPERM 权限不足,无法改变根目录。b3tLinux联盟
EFAULT 参数path指针超出可存取内存空间。b3tLinux联盟
ENAMETOOLONG 参数path太长。b3tLinux联盟
ENOTDIR 路径中的目录存在但却非真正的目录。b3tLinux联盟
EACCESS 存取目录时被拒绝b3tLinux联盟
ENOMEM 核心内存不足。b3tLinux联盟
ELOOP 参数path有过多符号连接问题。b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
范例
/* 将根目录改为/tmp ,并将工作目录切换至/tmp */b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
chroot(“/tmp”);b3tLinux联盟
chdir(“/”);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
closedir(关闭目录)
相关函数
opendirb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
定义函数
int closedir(DIR *dir);b3tLinux联盟
函数说明
closedir()关闭参数dir所指的目录流。b3tLinux联盟
返回值
关闭成功则返回0,失败返回-1,错误原因存于errno 中。b3tLinux联盟
错误代码
EBADF 参数dir为无效的目录流b3tLinux联盟
范例
参考readir()。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
fchdir(改变当前的工作目录)
相关函数
getcwd,chrootb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int fchdir(int fd);b3tLinux联盟
函数说明
fchdir()用来将当前的工作目录改变成以参数fd 所指的文件描述词。b3tLinux联盟
返回值执
行成功则返回0,失败返回-1,errno为错误代码。b3tLinux联盟
附加说明
b3tLinux联盟
范例
#include<sys/types.h>b3tLinux联盟
#include<sys/stat.h>b3tLinux联盟
#include<fcntl.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
int fd;b3tLinux联盟
fd = open(“/tmp”,O_RDONLY);b3tLinux联盟
fchdir(fd);b3tLinux联盟
printf(“current working directory : %s \n”,getcwd(NULL,NULL));b3tLinux联盟
close(fd);b3tLinux联盟
}b3tLinux联盟
执行
current working directory : /tmpb3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
fchmod(改变文件的权限)
相关函数
chmod,stat,open,chownb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<sys/stat.h>b3tLinux联盟
定义函数
int fchmod(int fildes,mode_t mode);b3tLinux联盟
函数说明
fchmod()会依参数mode权限来更改参数fildes所指文件的权限。参数fildes为已打开文件的文件描述词。参数mode请参考chmod()。b3tLinux联盟
返回值
权限改变成功则返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
错误原因
EBADF 参数fildes为无效的文件描述词。b3tLinux联盟
EPERM 进程的有效用户识别码与欲修改权限的文件所有者不同,而且也不具root权限。b3tLinux联盟
EROFS 欲写入权限的文件存在于只读文件系统内。b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
范例
#include<sys/stat.h>b3tLinux联盟
#include<fcntl.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
int fd;b3tLinux联盟
fd = open (“/etc/passwd”,O_RDONLY);b3tLinux联盟
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);b3tLinux联盟
close(fd);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
fchown(改变文件的所有者)
相关函数
chown,lchown,chmodb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
定义函数
int fchown(int fd,uid_t owner,gid_t group);b3tLinux联盟
函数说明
fchown()会将参数fd指定文件的所有者变更为参数owner代表的用户,而将该文件的组变更为参数group组。如果参数owner或group为-1,对映的所有者或组有所改变。参数fd 为已打开的文件描述词。当root用fchown()改变文件所有者或组时,该文件若具S_ISUID或S_ISGID权限,则会清除此权限位。b3tLinux联盟
返回值
成功则返回0,失败则返回-1,错误原因存于errno。b3tLinux联盟
错误代码
EBADF 参数fd文件描述词为无效的或该文件已关闭。b3tLinux联盟
EPERM 进程的有效用户识别码与欲修改权限的文件所有者不同,而且也不具root权限,或是参数owner、group不正确。b3tLinux联盟
EROFS 欲写入的文件存在于只读文件系统内。b3tLinux联盟
ENOENT 指定的文件不存在b3tLinux联盟
EIO I/O存取错误b3tLinux联盟
范例
#include<sys/types.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
#include<fcntl.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
int fd;b3tLinux联盟
fd = open (“/etc/passwd”,O_RDONLY);b3tLinux联盟
chown(fd,0,0);b3tLinux联盟
close(fd);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
fstat(由文件描述词取得文件状态)
相关函数
stat,lstat,chmod,chown,readlink,utimeb3tLinux联盟
表头文件
#include<sys/stat.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
定义函数
int fstat(int fildes,struct stat *buf);b3tLinux联盟
函数说明
fstat()用来将参数fildes所指的文件状态,复制到参数buf所指的结构中(struct stat)。Fstat()与stat()作用完全相同,不同处在于传入的参数为已打开的文件描述词。详细内容请参考stat()。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,错误代码存于errno。b3tLinux联盟
范例
#include<sys/stat.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
#include<fcntk.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
struct stat buf;b3tLinux联盟
int fd;b3tLinux联盟
fd = open (“/etc/passwd”,O_RDONLY);b3tLinux联盟
fstat(fd,&buf);b3tLinux联盟
printf(“/etc/passwd file size +%d\n “,buf.st_size);b3tLinux联盟
}b3tLinux联盟
执行
/etc/passwd file size = 705b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
ftruncate(改变文件大小)
相关函数
open,truncateb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int ftruncate(int fd,off_t length);b3tLinux联盟
函数说明
ftruncate()会将参数fd指定的文件大小改为参数length指定的大小。参数fd为已打开的文件描述词,而且必须是以写入模式打开的文件。如果原来的文件大小比参数length大,则超过的部分会被删去。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
错误代码
EBADF 参数fd文件描述词为无效的或该文件已关闭。b3tLinux联盟
EINVAL 参数fd 为一socket 并非文件,或是该文件并非以写入模式打开。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
getcwd(取得当前的工作目录)
相关函数
get_current_dir_name,getwd,chdirb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
char * getcwd(char * buf,size_t size);b3tLinux联盟
函数说明
getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内存空间,参数size为buf的空间大小。在调用此函数时,buf所指的内存空间要足够大,若工作目录绝对路径的字符串长度超过参数size大小,则回值NULL,errno的值则为ERANGE。倘若参数buf为NULL,getcwd()会依参数size的大小自动配置内存(使用malloc()),如果参数size也为0,则getcwd()会依工作目录绝对路径的字符串程度来决定所配置的内存大小,进程可以在使用完此字符串后利用free()来释放此空间。b3tLinux联盟
返回值
执行成功则将结果复制到参数buf所指的内存空间,或是返回自动配置的字符串指针。失败返回NULL,错误代码存于errno。b3tLinux联盟
范例
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
char buf[80];b3tLinux联盟
getcwd(buf,sizeof(buf));b3tLinux联盟
printf(“current working directory : %s\n”,buf);b3tLinux联盟
}b3tLinux联盟
执行
current working directory :/tmpb3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
link(建立文件连接)
相关函数
symlink,unlinkb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int link (const char * oldpath,const char * newpath);b3tLinux联盟
函数说明
link()以参数newpath指定的名称来建立一个新的连接(硬连接)到参数oldpath所指定的已存在文件。如果参数newpath指定的名称为一已存在的文件则不会建立连接。b3tLinux联盟
返回值
成功则返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
附加说明
link()所建立的硬连接无法跨越不同文件系统,如果需要请改用symlink()。b3tLinux联盟
错误代码
EXDEV 参数oldpath与newpath不是建立在同一文件系统。b3tLinux联盟
EPERM 参数oldpath与newpath所指的文件系统不支持硬连接b3tLinux联盟
EROFS 文件存在于只读文件系统内b3tLinux联盟
EFAULT 参数oldpath或newpath 指针超出可存取内存空间。b3tLinux联盟
ENAMETOLLONG 参数oldpath或newpath太长b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
EEXIST 参数newpath所指的文件名已存在。b3tLinux联盟
EMLINK 参数oldpath所指的文件已达最大连接数目。b3tLinux联盟
ELOOP 参数pathname有过多符号连接问题b3tLinux联盟
ENOSPC 文件系统的剩余空间不足。b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
范例
/* 建立/etc/passwd 的硬连接为pass */b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
link(“/etc/passwd”,”pass”);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
lstat(由文件描述词取得文件状态)
相关函数
stat,fstat,chmod,chown,readlink,utimeb3tLinux联盟
表头文件
#include<sys/stat.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
定义函数
int lstat (const char * file_name.struct stat * buf);b3tLinux联盟
函数说明
lstat()与stat()作用完全相同,都是取得参数file_name所指的文件状态,其差别在于,当文件为符号连接时,lstat()会返回该link本身的状态。详细内容请参考stat()。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,错误代码存于errno。b3tLinux联盟
范例
参考stat()。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
opendir(打开目录)
相关函数
open,readdir,closedir,rewinddir,seekdir,telldir,scandirb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
定义函数
DIR * opendir(const char * name);b3tLinux联盟
函数说明
opendir()用来打开参数name指定的目录,并返回DIR*形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。b3tLinux联盟
返回值
成功则返回DIR* 型态的目录流,打开失败则返回NULL。b3tLinux联盟
错误代码
EACCESS 权限不足b3tLinux联盟
EMFILE 已达到进程可同时打开的文件数上限。b3tLinux联盟
ENFILE 已达到系统可同时打开的文件数上限。b3tLinux联盟
ENOTDIR 参数name非真正的目录b3tLinux联盟
ENOENT 参数name 指定的目录不存在,或是参数name 为一空字符串。b3tLinux联盟
ENOMEM 核心内存不足。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
readdir(读取目录)
相关函数
open,opendir,closedir,rewinddir,seekdir,telldir,scandirb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
定义函数
struct dirent * readdir(DIR * dir);b3tLinux联盟
函数说明
readdir()返回参数dir目录流的下个目录进入点。b3tLinux联盟
结构dirent定义如下b3tLinux联盟
struct direntb3tLinux联盟
{b3tLinux联盟
ino_t d_ino;b3tLinux联盟
ff_t d_off;b3tLinux联盟
signed short int d_reclen;b3tLinux联盟
unsigned char d_type;b3tLinux联盟
har d_name[256;b3tLinux联盟
};b3tLinux联盟
d_ino 此目录进入点的inodeb3tLinux联盟
d_off 目录文件开头至此目录进入点的位移b3tLinux联盟
d_reclen _name的长度,不包含NULL字符b3tLinux联盟
d_type d_name 所指的文件类型b3tLinux联盟
d_name 文件名b3tLinux联盟
返回值
成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。b3tLinux联盟
附加说明
EBADF参数dir为无效的目录流。b3tLinux联盟
范例
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
DIR * dir;b3tLinux联盟
struct dirent * ptr;b3tLinux联盟
int i;b3tLinux联盟
dir =opendir(“/etc/rc.d”);b3tLinux联盟
while((ptr = readdir(dir))!=NULL)b3tLinux联盟
{b3tLinux联盟
printf(“d_name: %s\n”,ptr->d_name);b3tLinux联盟
}b3tLinux联盟
closedir(dir);b3tLinux联盟
}b3tLinux联盟
执行
d_name:.b3tLinux联盟
d_name:..b3tLinux联盟
d_name:init.db3tLinux联盟
d_name:rc0.db3tLinux联盟
d_name:rc1.db3tLinux联盟
d_name:rc2.db3tLinux联盟
d_name:rc3.db3tLinux联盟
d_name:rc4.db3tLinux联盟
d_name:rc5.db3tLinux联盟
d_name:rc6.db3tLinux联盟
d_name:rcb3tLinux联盟
d_name:rc.localb3tLinux联盟
d_name:rc.sysinitb3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
readlink(取得符号连接所指的文件)
相关函数
stat,lstat,symlinkb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int readlink(const char * path ,char * buf,size_t bufsiz);b3tLinux联盟
函数说明
readlink()会将参数path的符号连接内容存到参数buf所指的内存空间,返回的内容不是以NULL作字符串结尾,但会将字符串的字符数返回。若参数bufsiz小于符号连接的内容长度,过长的内容会被截断。b3tLinux联盟
返回值
执行成功则传符号连接所指的文件路径字符串,失败则返回-1,错误代码存于errno。b3tLinux联盟
错误代码
EACCESS 取文件时被拒绝,权限不够b3tLinux联盟
EINVAL 参数bufsiz 为负数b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
ELOOP 欲打开的文件有过多符号连接问题。b3tLinux联盟
ENAMETOOLONG 参数path的路径名称太长b3tLinux联盟
ENOENT 参数path所指定的文件不存在b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
ENOTDIR 参数path路径中的目录存在但却非真正的目录。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
remove(删除文件)
相关函数
link,rename,unlinkb3tLinux联盟
表头文件
#include<stdio.h>b3tLinux联盟
定义函数
int remove(const char * pathname);b3tLinux联盟
函数说明
remove()会删除参数pathname指定的文件。如果参数pathname为一文件,则调用unlink()处理,若参数pathname为一目录,则调用rmdir()来处理。请参考unlink()与rmdir()。b3tLinux联盟
返回值
成功则返回0,失败则返回-1,错误原因存于errno。b3tLinux联盟
错误代码
EROFS 欲写入的文件存在于只读文件系统内b3tLinux联盟
EFAULT 参数pathname指针超出可存取内存空间b3tLinux联盟
ENAMETOOLONG 参数pathname太长b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
ELOOP 参数pathname有过多符号连接问题b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
rename(更改文件名称或位置)
相关函数
link,unlink,symlinkb3tLinux联盟
表头文件
#include<stdio.h>b3tLinux联盟
定义函数
int rename(const char * oldpath,const char * newpath);b3tLinux联盟
函数说明
rename()会将参数oldpath 所指定的文件名称改为参数newpath所指的文件名称。若newpath所指定的文件已存在,则会被删除。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,错误原因存于errnob3tLinux联盟
范例
/* 设计一个DOS下的rename指令rename 旧文件名新文件名*/b3tLinux联盟
#include <stdio.h>b3tLinux联盟
void main(int argc,char **argv)b3tLinux联盟
{b3tLinux联盟
if(argc<3){b3tLinux联盟
printf(“Usage: %s old_name new_name\n”,argv[0]);b3tLinux联盟
return;b3tLinux联盟
}b3tLinux联盟
printf(“%s=>%s”,argc[1],argv[2]);b3tLinux联盟
if(rename(argv[1],argv[2]<0)b3tLinux联盟
printf(“error!\n”);b3tLinux联盟
elseb3tLinux联盟
printf(“ok!\n”);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
rewinddir(重设读取目录的位置为开头位置)
相关函数
open,opendir,closedir,telldir,seekdir,readdir,scandirb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
定义函数
void rewinddir(DIR *dir);b3tLinux联盟
函数说明
rewinddir()用来设置参数dir 目录流目前的读取位置为原来开头的读取位置。b3tLinux联盟
返回值
b3tLinux联盟
错误代码
EBADF dir为无效的目录流b3tLinux联盟
范例
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
DIR * dir;b3tLinux联盟
struct dirent *ptr;b3tLinux联盟
dir = opendir(“/etc/rc.d”);b3tLinux联盟
while((ptr = readdir(dir))!=NULL)b3tLinux联盟
{b3tLinux联盟
printf(“d_name :%s\n”,ptr->d_name);b3tLinux联盟
}b3tLinux联盟
rewinddir(dir);b3tLinux联盟
printf(“readdir again!\n”);b3tLinux联盟
while((ptr = readdir(dir))!=NULL)b3tLinux联盟
{b3tLinux联盟
printf(“d_name: %s\n”,ptr->d_name);b3tLinux联盟
}b3tLinux联盟
closedir(dir);b3tLinux联盟
}b3tLinux联盟
执行
d_name:.b3tLinux联盟
d_name:..b3tLinux联盟
d_name:init.db3tLinux联盟
d_name:rc0.db3tLinux联盟
d_name:rc1.db3tLinux联盟
d_name:rc2.db3tLinux联盟
d_name:rc3.db3tLinux联盟
d_name:rc4.db3tLinux联盟
d_name:rc5.db3tLinux联盟
d_name:rc6.db3tLinux联盟
d_name:rcb3tLinux联盟
d_name:rc.localb3tLinux联盟
d_name:rc.sysinitb3tLinux联盟
readdir again!b3tLinux联盟
d_name:.b3tLinux联盟
d_name:..b3tLinux联盟
d_name:init.db3tLinux联盟
d_name:rc0.db3tLinux联盟
d_name:rc1.db3tLinux联盟
d_name:rc2.db3tLinux联盟
d_name:rc3.db3tLinux联盟
d_name:rc4.db3tLinux联盟
d_name:rc5.db3tLinux联盟
d_name:rc6.db3tLinux联盟
d_name:rcb3tLinux联盟
d_name:rc.localb3tLinux联盟
d_name:rc.sysinitb3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
seekdir(设置下回读取目录的位置)
相关函数
open,opendir,closedir,rewinddir,telldir,readdir,scandirb3tLinux联盟
表头文件
#include<dirent.h>b3tLinux联盟
定义函数
void seekdir(DIR * dir,off_t offset);b3tLinux联盟
函数说明
seekdir()用来设置参数dir目录流目前的读取位置,在调用readdir()时便从此新位置开始读取。参数offset 代表距离目录文件开头的偏移量。b3tLinux联盟
返回值
b3tLinux联盟
错误代码
EBADF 参数dir为无效的目录流b3tLinux联盟
范例
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
DIR * dir;b3tLinux联盟
struct dirent * ptr;b3tLinux联盟
int offset,offset_5,i=0;b3tLinux联盟
dir=opendir(“/etc/rc.d”);b3tLinux联盟
while((ptr = readdir(dir))!=NULL)b3tLinux联盟
{b3tLinux联盟
offset = telldir(dir);b3tLinux联盟
if(++i = =5) offset_5 =offset;b3tLinux联盟
printf(“d_name :%s offset :%d \n”,ptr->d_name,offset);b3tLinux联盟
}b3tLinux联盟
seekdir(dir offset_5);b3tLinux联盟
printf(“Readdir again!\n”);b3tLinux联盟
while((ptr = readdir(dir))!=NULL)b3tLinux联盟
{b3tLinux联盟
offset = telldir(dir);b3tLinux联盟
printf(“d_name :%s offset :%d\n”,ptr->d_name.offset);b3tLinux联盟
}b3tLinux联盟
closedir(dir);b3tLinux联盟
}b3tLinux联盟
执行
d_name : . offset :12b3tLinux联盟
d_name : .. offset:24b3tLinux联盟
d_name : init.d offset 40b3tLinux联盟
d_name : rc0.d offset :56b3tLinux联盟
d_name :rc1.d offset :72b3tLinux联盟
d_name:rc2.d offset :88b3tLinux联盟
d_name:rc3.d offset 104b3tLinux联盟
d_name:rc4.d offset:120b3tLinux联盟
d_name:rc5.d offset:136b3tLinux联盟
d_name:rc6.d offset:152b3tLinux联盟
d_name:rc offset 164b3tLinux联盟
d_name:rc.local offset :180b3tLinux联盟
d_name:rc.sysinit offset :4096b3tLinux联盟
readdir again!b3tLinux联盟
d_name:rc2.d offset :88b3tLinux联盟
d_name:rc3.d offset 104b3tLinux联盟
d_name:rc4.d offset:120b3tLinux联盟
d_name:rc5.d offset:136b3tLinux联盟
d_name:rc6.d offset:152b3tLinux联盟
d_name:rc offset 164b3tLinux联盟
d_name:rc.local offset :180b3tLinux联盟
d_name:rc.sysinit offset :4096b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
stat(取得文件状态)
相关函数
fstat,lstat,chmod,chown,readlink,utimeb3tLinux联盟
表头文件
#include<sys/stat.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
定义函数
int stat(const char * file_name,struct stat *buf);b3tLinux联盟
函数说明
stat()用来将参数file_name所指的文件状态,复制到参数buf所指的结构中。b3tLinux联盟
下面是struct stat内各参数的说明b3tLinux联盟
struct statb3tLinux联盟
{b3tLinux联盟
dev_t st_dev; /*device*/b3tLinux联盟
ino_t st_ino; /*inode*/b3tLinux联盟
mode_t st_mode; /*protection*/b3tLinux联盟
nlink_t st_nlink; /*number of hard links */b3tLinux联盟
uid_t st_uid; /*user ID of owner*/b3tLinux联盟
gid_t st_gid; /*group ID of owner*/b3tLinux联盟
dev_t st_rdev; /*device type */b3tLinux联盟
off_t st_size; /*total size, in bytes*/b3tLinux联盟
unsigned long st_blksize; /*blocksize for filesystem I/O */b3tLinux联盟
unsigned long st_blocks; /*number of blocks allocated*/b3tLinux联盟
time_t st_atime; /* time of lastaccess*/b3tLinux联盟
time_t st_mtime; /* time of last modification */b3tLinux联盟
time_t st_ctime; /* time of last change */b3tLinux联盟
};b3tLinux联盟
st_dev 文件的设备编号b3tLinux联盟
st_ino 文件的i-nodeb3tLinux联盟
st_mode 文件的类型和存取的权限b3tLinux联盟
st_nlink 连到该文件的硬连接数目,刚建立的文件值为1。b3tLinux联盟
st_uid 文件所有者的用户识别码b3tLinux联盟
st_gid 文件所有者的组识别码b3tLinux联盟
st_rdev 若此文件为装置设备文件,则为其设备编号b3tLinux联盟
st_size 文件大小,以字节计算b3tLinux联盟
st_blksize 文件系统的I/O 缓冲区大小。b3tLinux联盟
st_blcoks 占用文件区块的个数,每一区块大小为512 个字节。b3tLinux联盟
st_atime 文件最近一次被存取或被执行的时间,一般只有在用mknod、utime、read、write与tructate时改变。b3tLinux联盟
st_mtime 文件最后一次被修改的时间,一般只有在用mknod、utime和write时才会改变b3tLinux联盟
st_ctime i-node最近一次被更改的时间,此参数会在文件所有者、组、权限被更改时更新先前所描述的st_mode 则定义了下列数种情况b3tLinux联盟
S_IFMT 0170000 文件类型的位遮罩b3tLinux联盟
S_IFSOCK 0140000 scoketb3tLinux联盟
S_IFLNK 0120000 符号连接b3tLinux联盟
S_IFREG 0100000 一般文件b3tLinux联盟
S_IFBLK 0060000 区块装置b3tLinux联盟
S_IFDIR 0040000 目录b3tLinux联盟
S_IFCHR 0020000 字符装置b3tLinux联盟
S_IFIFO 0010000 先进先出b3tLinux联盟
S_ISUID 04000 文件的(set user-id on execution)位b3tLinux联盟
S_ISGID 02000 文件的(set group-id on execution)位b3tLinux联盟
S_ISVTX 01000 文件的sticky位b3tLinux联盟
S_IRUSR(S_IREAD) 00400 文件所有者具可读取权限b3tLinux联盟
S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限b3tLinux联盟
S_IXUSR(S_IEXEC) 00100 文件所有者具可执行权限b3tLinux联盟
S_IRGRP 00040 用户组具可读取权限b3tLinux联盟
S_IWGRP 00020 用户组具可写入权限b3tLinux联盟
S_IXGRP 00010 用户组具可执行权限b3tLinux联盟
S_IROTH 00004 其他用户具可读取权限b3tLinux联盟
S_IWOTH 00002 其他用户具可写入权限b3tLinux联盟
S_IXOTH 00001 其他用户具可执行权限b3tLinux联盟
上述的文件类型在POSIX 中定义了检查这些类型的宏定义b3tLinux联盟
S_ISLNK (st_mode) 判断是否为符号连接b3tLinux联盟
S_ISREG (st_mode) 是否为一般文件b3tLinux联盟
S_ISDIR (st_mode)是否为目录b3tLinux联盟
S_ISCHR (st_mode)是否为字符装置文件b3tLinux联盟
S_ISBLK (s3e) 是否为先进先出b3tLinux联盟
S_ISSOCK (st_mode) 是否为socketb3tLinux联盟
若一目录具有sticky 位(S_ISVTX),则表示在此目录下的文件只能被该文件所有者、此目录所有者或root来删除或改名。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,错误代码存于errnob3tLinux联盟
错误代码
ENOENT 参数file_name指定的文件不存在b3tLinux联盟
ENOTDIR 路径中的目录存在但却非真正的目录b3tLinux联盟
ELOOP 欲打开的文件有过多符号连接问题,上限为16符号连接b3tLinux联盟
EFAULT 参数buf为无效指针,指向无法存在的内存空间b3tLinux联盟
EACCESS 存取文件时被拒绝b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
ENAMETOOLONG 参数file_name的路径名称太长b3tLinux联盟
范例
#include<sys/stat.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
mian()b3tLinux联盟
{b3tLinux联盟
struct stat buf;b3tLinux联盟
stat (“/etc/passwd”,&buf);b3tLinux联盟
printf(“/etc/passwd file size = %d \n”,buf.st_size);b3tLinux联盟
}b3tLinux联盟
执行
/etc/passwd file size = 705b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
symlink(建立文件符号连接)
相关函数
link,unlinkb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int symlink( const char * oldpath,const char * newpath);b3tLinux联盟
函数说明
symlink()以参数newpath指定的名称来建立一个新的连接(符号连接)到参数oldpath所指定的已存在文件。参数oldpath指定的文件不一定要存在,如果参数newpath指定的名称为一已存在的文件则不会建立连接。b3tLinux联盟
返回值
成功则返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
错误代码
EPERM 参数oldpath与newpath所指的文件系统不支持符号连接b3tLinux联盟
EROFS 欲测试写入权限的文件存在于只读文件系统内b3tLinux联盟
EFAULT 参数oldpath或newpath指针超出可存取内存空间。b3tLinux联盟
ENAMETOOLONG 参数oldpath或newpath太长b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
EEXIST 参数newpath所指的文件名已存在。b3tLinux联盟
EMLINK 参数oldpath所指的文件已达到最大连接数目b3tLinux联盟
ELOOP 参数pathname有过多符号连接问题b3tLinux联盟
ENOSPC 文件系统的剩余空间不足b3tLinux联盟
EIO I/O 存取错误b3tLinux联盟
范例
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
symlink(“/etc/passwd”,”pass”);b3tLinux联盟
}b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
telldir(取得目录流的读取位置)
相关函数
open,opendir,closedir,rewinddir,seekdir,readdir,scandirb3tLinux联盟
表头文件
#include<dirent.h>b3tLinux联盟
定义函数
off_t telldir(DIR *dir);b3tLinux联盟
函数说明
telldir()返回参数dir目录流目前的读取位置。此返回值代表距离目录文件开头的偏移量返回值返回下个读取位置,有错误发生时返回-1。b3tLinux联盟
错误代码
EBADF参数dir为无效的目录流。b3tLinux联盟
范例
#include<sys/types.h>b3tLinux联盟
#include<dirent.h>b3tLinux联盟
#include<unistd.h>b3tLinux联盟
main()b3tLinux联盟
{b3tLinux联盟
DIR *dir;b3tLinux联盟
struct dirent *ptr;b3tLinux联盟
int offset;b3tLinux联盟
dir = opendir(“/etc/rc.d”);b3tLinux联盟
while((ptr = readdir(dir))!=NULL)b3tLinux联盟
{b3tLinux联盟
offset = telldir (dir);b3tLinux联盟
printf(“d_name : %s offset :%d\n”, ptr->d_name,offset);b3tLinux联盟
}b3tLinux联盟
closedir(dir);b3tLinux联盟
}b3tLinux联盟
执行
d_name : . offset :12b3tLinux联盟
d_name : .. offset:24b3tLinux联盟
d_name : init.d offset 40b3tLinux联盟
d_name : rc0.d offset :56b3tLinux联盟
d_name :rc1.d offset :72b3tLinux联盟
d_name:rc2.d offset :88b3tLinux联盟
d_name:rc3.d offset 104b3tLinux联盟
d_name:rc4.d offset:120b3tLinux联盟
d_name:rc5.d offset:136b3tLinux联盟
d_name:rc6.d offset:152b3tLinux联盟
d_name:rc offset 164b3tLinux联盟
d_name:rc.local offset :180b3tLinux联盟
d_name:rc.sysinit offset :4096b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
truncate(改变文件大小)
相关函数
open,ftruncateb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int truncate(const char * path,off_t length);b3tLinux联盟
函数说明
truncate()会将参数path 指定的文件大小改为参数length 指定的大小。如果原来的文件大小比参数length大,则超过的部分会被删去。b3tLinux联盟
返回值
执行成功则返回0,失败返回-1,错误原因存于errno。b3tLinux联盟
错误代码
EACCESS 参数path所指定的文件无法存取。b3tLinux联盟
EROFS 欲写入的文件存在于只读文件系统内b3tLinux联盟
EFAULT 参数path指针超出可存取内存空间b3tLinux联盟
EINVAL 参数path包含不合法字符b3tLinux联盟
ENAMETOOLONG 参数path太长b3tLinux联盟
ENOTDIR 参数path路径并非一目录b3tLinux联盟
EISDIR 参数path 指向一目录b3tLinux联盟
ETXTBUSY 参数path所指的文件为共享程序,而且正被执行中b3tLinux联盟
ELOOP 参数path’有过多符号连接问题b3tLinux联盟
EIO I/O 存取错误。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
umask(设置建立新文件时的权限遮罩)
相关函数
creat,openb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<sys/stat.h>b3tLinux联盟
定义函数
mode_t umask(mode_t mask);b3tLinux联盟
函数说明
umask()会将系统umask值设成参数mask&0777后的值,然后将先前的umask值返回。在使用open()建立新文件时,该参数mode并非真正建立文件的权限,而是(mode&~umask)的权限值。例如,在建立文件时指定文件权限为0666,通常umask值默认为022,则该文件的真正权限则为0666&~022=0644,也就是rw-r--r--返回值此调用不会有错误值返回。返回值为原先系统的umask值。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
unlink(删除文件)
相关函数
link,rename,removeb3tLinux联盟
表头文件
#include<unistd.h>b3tLinux联盟
定义函数
int unlink(const char * pathname);b3tLinux联盟
函数说明
unlink()会删除参数pathname指定的文件。如果该文件名为最后连接点,但有其他进程打开了此文件,则在所有关于此文件的文件描述词皆关闭后才会删除。如果参数pathname为一符号连接,则此连接会被删除。b3tLinux联盟
返回值
成功则返回0,失败返回-1,错误原因存于errnob3tLinux联盟
错误代码
EROFS 文件存在于只读文件系统内b3tLinux联盟
EFAULT 参数pathname指针超出可存取内存空间b3tLinux联盟
ENAMETOOLONG 参数pathname太长b3tLinux联盟
ENOMEM 核心内存不足b3tLinux联盟
ELOOP 参数pathname 有过多符号连接问题b3tLinux联盟
EIO I/O 存取错误b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
utime(修改文件的存取时间和更改时间)
相关函数
utimes,statb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<utime.h>b3tLinux联盟
定义函数
int utime(const char * filename,struct utimbuf * buf);b3tLinux联盟
函数说明
utime()用来修改参数filename文件所属的inode存取时间。b3tLinux联盟
结构utimbuf定义如下b3tLinux联盟
struct utimbuf{b3tLinux联盟
time_t actime;b3tLinux联盟
time_t modtime;b3tLinux联盟
};b3tLinux联盟
返回值
如果参数buf为空指针(NULL),则该文件的存取时间和更改时间全部会设为目前时间。b3tLinux联盟
执行成功则返回0,失败返回-1,错误代码存于errno。b3tLinux联盟
错误代码
EACCESS 存取文件时被拒绝,权限不足b3tLinux联盟
ENOENT 指定的文件不存在。b3tLinux联盟
 
b3tLinux联盟
b3tLinux联盟
b3tLinux联盟
utimes(修改文件的存取时间和更改时间)
相关函数
utime,statb3tLinux联盟
表头文件
#include<sys/types.h>b3tLinux联盟
#include<utime.h>b3tLinux联盟
定义函数
int utimes(char * filename.struct timeval *tvp);b3tLinux联盟
函数说明
utimes()用来修改参数filename文件所属的inode存取时间和修改时间。b3tLinux联盟
结构timeval定义如下b3tLinux联盟
struct timeval {b3tLinux联盟
long tv_sec;b3tLinux联盟
long tv_usec; /* 微妙*/b3tLinux联盟
};b3tLinux联盟
返回值
参数tvp 指向两个timeval 结构空间,和utime()使用的utimebuf结构比较,tvp[0].tc_sec 则为utimbuf.actime,tvp]1].tv_sec 为utimbuf.modtime。b3tLinux联盟
执行成功则返回0。失败返回-1,错误代码存于errno。b3tLinux联盟
错误代码
EACCESS 存取文件时被拒绝,权限不足b3tLinux联盟
ENOENT 指定的文件不存在b3tLinux联盟
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门