rename和renameat函数
#include <stdio.h>
int rename(const char *oldname, const char *newname);
int renameat(int oldfd, const char *oldname, int newfd, const char *newname);
引入符号链接的原因是为了避开硬链接的一些限制。
- 硬链接通常要求链接和文件位于同一文件系统中
只有超级用户才能创建指向目录的硬链接
用symlink或symlinkat函数创建一个符号链接。
#include <unistd.h>
int symlink(const char *actualpath, const char *sympath);
int symlinkat(const char *actualpath, int fd, const char *sympath);
打开该链接本身,并读取该链接中的名字。readlink和readlinkat函数提供了这种功能。
#include <unistd.h>
ssize_t readlink(const char *restrict pathname, char *restrict buf, size_t bufsize);
ssize_t readlinkat(int fd, const char *restrict pathname, char *restrict buf, size_t bufsize);
futimens和utimensat函数可以指定纳秒级精度的时间戳。
#include <unistd.h>
int futimes(int fd, const struct timespec times[2]);
int utimensat(int fd, const char *path, const struct timespec times[2], int flag);
futimes和utimensat函数都包含在POSIX.1中,第3个函数utimes包含在Single UNIX Specification的XSI扩展中。
#include <sys/time.h>
int utimes(const char *pathname, const struct timeval times[2])
int main(int argc, char *argv[])
{
int i,fd;
struct stat statbuf;
struct timespec times[2];
for(i=1;i<argc;i++)
{
if(stat(argv[i],&statbuf)<0)
{
err_ret("%s: stat error",argv[i]);
continue;
}
if((fd=open(argv[i],O_RDWR|O_TRUNC))<0)
{
err_ret("%s: open error",argv[i]);
continue;
}
times[0]=statbuf.st_atim;
times[1]=statbuf.st_mtim;
if(futimens(fd,times)<0)
{
err_ret("%s: futimens error",argv[i]);
}
close(fd);
}
return 0;
}
futimens函数实例
mkdir、mkdirat和rmdir
mkdir和mkdirat函数创建目录,用rmdir函数删除目录。
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
int mkdirat(int fd, const char *pathname, mode_t mode);
用rmdir函数可以删除一个空目。
#include <unistd.h>
int rmdir(const char *pathname);
读目录
#include <direct.h>
DIR *opendir(const char *pathname);
DIR *fdopendir(int fd);
struct dirent *readdir(DIR *dp);
void rewinddir(DIR *dp);
int closedir(DIR *dp);
long telldir(DIR *dp);
void seekdir(DIR *dp, long loc);
fdopendir函数最早出现在SUSv4中,它提供了一种方法,可以把打开文件描述符转换成目录处理函数需要的DIR结构。
telldir和seekdir函数不是基本POSIX.1标准的组成部分。他们是Single UNIX Specification中的XSI扩展,所以可以期望所有符合UNIX系统的实现都会提供这两个函数。