Linux C语言目录操作

阅读量: searchstar 2021-08-02 01:05:28
Categories: Tags:

系统调用(man 2)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode)
int creat(const char *pathname, mode_t mode);

int openat(int dirfd, const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);
#include <unistd.h>
int close(int fd);
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
// 在指定目录下创建目录
int mkdirat(int dirfd, const char *pathname, mode_t mode);
#include <unistd.h>
int chdir(const char *path);
int fchdir(int fd);


#include <unistd.h>
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
ssize_t readlinkat(int dirfd, const char *pathname,
    char *buf, size_t bufsiz);


#include <unistd.h>
int symlink(const char *target, const char *linkpath);

#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
int symlinkat(const char *target, int newdirfd, const char *linkpath);

glibc (man 3)

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 */


DT_BLK      This is a block device.
DT_CHR      This is a character device.
DT_DIR      This is a directory.
DT_FIFO     This is a named pipe (FIFO).
DT_LNK      This is a symbolic link.
DT_REG      This is a regular file.
DT_SOCK     This is a UNIX domain socket.
DT_UNKNOWN  The file type could not be determined.
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
#include <sys/types.h>
#include <dirent.h>
int dirfd(DIR *dirp);
#include <dirent.h>
struct dirent *readdir(DIR *dirp);


#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
#include <libgen.h>
char *dirname(char *path);
char *basename(char *path);



If path does not contain a slash, dirname() returns the string "." while basename() returns a copy of path. If path is the string "/", then both dirname() and basename() return the string "/". If path is a null pointer or points to an empty string, then both dirname() and basename() return the string ".".