Linux系统调用九、与目录操作相关的系统API


在这里插入图片描述


1. getcwd函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
char *getcwd(char *buf, size_t size);
char *getwd(char *buf);
char *get_current_dir_name(void);
  • 函数功能

    These functions return a null-terminated string containing an absolute pathname that is the current working directory of the calling process. 获得进程的当前工作路径。

  • 函数参数

    • buf:缓冲区,用于存放目录。The pathname is returned as the function result and via the argument buf, if present.
    • size:缓冲区大小
  • 函数返回值

    • On success, these functions return a pointer to a string containing the pathname of the current working directory. In the case getcwd() and getwd() this is the same value as buf. 成功返回指向路径名字符串的指针。
    • On failure, these functions return NULL, and errno is set to indicate the error. The contents of the array pointed to by buf are undefined on error. 失败返回NULL。

2. chdir函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int chdir(const char *path);
int fchdir(int fd);
  • 函数功能

    chdir() changes the current working directory of the calling process to the directory specified in path.改变进程的工作路径。

  • 函数参数

    • path:目标路径,也就是将当前进程的工作路径改为path。
  • 函数返回值

    • 成功返回0。On success, zero is returned.
    • 失败返回-1且设置errno。On error, -1 is returned, and errno is set appropriately.

示例:改变进程当前工作路径并打屏

/************************************************************
  >File Name  : chdir_test.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月16日 星期一 21时36分37秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char*argv[])
{
	/*切换当前进程的工作路径,注意是当前进程,也就是main进程的工作路径*/
	chdir(argv[1]);
	/*在新的路径创建一个文件来证明路径切换成功*/
	int fd = open("hello.txt", O_CREAT | O_RDONLY);
	close(fd);
	char buf[512];
	getcwd(buf, sizeof(buf));
	printf("current path : %s\n", buf);
	return 0;
}

在这里插入图片描述
首先,看测试结果中打印的内容,getcwd()函数获取到的路径确实是我们通过chdir()函数指定的路径,并且test路径从无到有增加了一个文件,也证明我们切换工作路径成功了。但是有些人可能会有一个疑惑,看途中的红色箭头标记,既然当前工作路径已经切换为①所标识的路径了,那么为什么②处显示的工作路径还是原来的路径呢?这是因为,我们在程序中使用chdir()函数切换的路径是进程 ./chdir_test 的路径,也就是说 ./chdir_test 的路径被切换为①了,并且测试结果(hello.txt文件的创建)也证明切换成功。而②所标识的路径是当前shell进程的工作路径,它们俩根本不是一回事。

3. mkdir函数

  • 包含头文件
#include <sys/stat.h>
#include <sys/types.h>
  • 函数原型
int mkdir(const char *pathname, mode_t mode);
  • 函数功能

    mkdir() attempts to create a directory named pathname. 创建一个目录。

  • 函数参数

    • pathname:路径名
    • mode:(mode & ~umask & 0777),和文件不同的是,目录必须有可执行权限才能进入目录,所以新建的目录必须要有可执行权限。(如果目录没有可执行权限,那么将无法进入该目录,所以新建时可以给满权限777)
  • 函数返回值

    mkdir() returns zero on success, or -1 if an error occurred (in which case, errno is set appropriately).

示例:使用mkdir命令和mkdir()函数分别新建一个目录

/************************************************************
  >File Name  : mkdir_test.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月16日 星期一 22时11分29秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("not found filename\n");
		return -1;	
	}
	mkdir(argv[1], 0777);
	return 0;
}

在这里插入图片描述

通过这个例子可以得到,如果想要使用mkdir()函数创建的目录达到和mkdir命令创建目录有一样的权限,可以给mode参数0777满权限。

4. rmdir函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int rmdir(const char *pathname);
  • 函数功能

    rmdir() deletes a directory, which must be empty. 只能删除空目录。

  • 函数参数

    • pathname:文件名及路径
  • 函数返回值

    • 成功返回0。On success, zero is returned.
    • 失败返回-1且设置errno。On error, -1 is returned, and errno is set appropriately.

5.opendir函数

  • 包含头文件
#include <sys/types.h>
#include <dirent.h>
  • 函数原型
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
  • 函数功能

    The opendir() function opens a directory stream corresponding to the directory name, and returns a pointer to the directory stream. The stream is positioned at the first entry in the directory. 打开目录。

  • 函数参数

    • name:目录名字。
  • 函数返回值

    • The opendir() and fdopendir() functions return a pointer to the directory stream. 返回一个指向目录流的指针DIR*,指向目录项的信息。

    • On error, NULL is returned, and errno is set appropriately.

6. readdir函数

  • 包含头文件
#include <dirent.h>
  • 函数原型
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
  • 函数功能

    读目录。

  • 函数参数

    • dirp:传入opendir返回的指针。
  • 函数返回值

    • On success, readdir() returns a pointer to a dirent structure. If the end of the directory stream is reached, NULL is returned and errno is not changed. 成功返回一个结构体,结构体定义如下

      struct dirent {
                     ino_t          d_ino;       /* inode number */
                     off_t          d_off;       /* offset to the next dirent */
                     unsigned short d_reclen;    /* length of this record */
                     unsigned char  d_type;      /* type of file; not supported
                                                    by all file system types */
                     char           d_name[256]; /* filename */
                 };
      

      判断读到内容的类型,依据是结构体成员d_type,类型如下

      /*
      d_type:
             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. socket
             DT_UNKNOWN  The file type is unknown. 未知
      */
      
    • On error, NULL is returned and errno is set appropriately.

7. closedir函数

  • 包含头文件
#include <sys/types.h>
#include <dirent.h>
  • 函数原型
int closedir(DIR *dirp);
  • 函数功能

    关闭目录。

  • 函数参数

    • dirp:传入opendir返回的指针。
  • 函数返回值

    • The closedir() function returns 0 on success.
    • On error, -1 is returned, and errno is set appropriately.

示例:递归子目录统计普通文件个数

/************************************************************
  >File Name  : count_file.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月17日 星期二 12时40分35秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>

int gFileNum = 0;

void file_count(char* dirname)
{
	/*打开目录*/
	DIR* dir = opendir(dirname);
	if(dir == NULL) /*打开失败则退出*/
	{
		perror("opendir err");
		exit(-1);	
	}	
	struct dirent* dentp = NULL;
	while((dentp = readdir(dir)) != NULL) /*不为NULL说明未读到末尾*/
	{
		if(dentp->d_type == DT_DIR) /*如果是目录则递归进入目录*/
		{
			if((strcmp(".", dentp->d_name) == 0) || (strcmp("..", dentp->d_name) == 0)) /*如果是当前目录或上一级目录则跳过*/
			{
				continue;	
			}
			/*每次递归都要把上一级目录加上,不然的话,进程找不到子目录所在的路径*/
			char namebuf[512];
			memset(namebuf, 0, sizeof(namebuf));
			sprintf(namebuf, "%s/%s", dirname, dentp->d_name);
			file_count(namebuf);
		}
		if(dentp->d_type == DT_REG) /*如果是文件,则计数*/
		{
			gFileNum++;
		}
	}
	closedir(dir);
}

int main(int argc, char* argv[])
{
	if(argc != 2)
	{
		printf("tip: ./exe dirname\n");
		return -1;	
	}
	/*递归目录,统计普通文件个数*/
	file_count(argv[1]);
	printf("num: %d\n", gFileNum);
	return 0;
}

编译运行,并通过shell命令find来验证结果

在这里插入图片描述

shell命令统计文件个数的命令

find ./ -type f | wc -l

8. 目录读写位置函数

8.1 rewinddir()把目录指针恢复到起始位置

  • 包含头文件
#include <sys/types.h>
#include <dirent.h>     
  • 函数原型
void rewinddir(DIR *dirp);

8.2 telldir()获取目录读写位置

  • 包含头文件
#include <dirent.h>
  • 函数原型
long telldir(DIR *dirp);

8.3 seekdir()修改目录读写位置

  • 包含头文件
#include <dirent.h>
  • 函数原型
void seekdir(DIR *dirp, long offset);
  • 函数返回值

    On success, the telldir() function returns the current location in the directory stream. On error, -1 is returned, and errno is set appropriately.


在这里插入图片描述
在这里插入图片描述


posted @ 2022-05-27 11:31  Mindtechnist  阅读(50)  评论(0编辑  收藏  举报  来源