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

简介: 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.


目录
打赏
0
0
0
0
13
分享
相关文章
|
1月前
|
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
74 24
Linux系统之whereis命令的基本使用
|
2天前
|
Linux系统中如何查看CPU信息
本文介绍了查看CPU核心信息的方法,包括使用`lscpu`命令和读取`/proc/cpuinfo`文件。`lscpu`能快速提供逻辑CPU数量、物理核心数、插槽数等基本信息;而`/proc/cpuinfo`则包含更详细的配置数据,如核心ID和处理器编号。此外,还介绍了如何通过`lscpu`和`dmidecode`命令获取CPU型号、制造商及序列号,并解释了CPU频率与缓存大小的相关信息。最后,详细解析了`lscpu`命令输出的各项参数含义,帮助用户更好地理解CPU的具体配置。
29 8
【Linux】进程IO|系统调用|open|write|文件描述符fd|封装|理解一切皆文件
本文详细介绍了Linux中的进程IO与系统调用,包括 `open`、`write`、`read`和 `close`函数及其用法,解释了文件描述符(fd)的概念,并深入探讨了Linux中的“一切皆文件”思想。这种设计极大地简化了系统编程,使得处理不同类型的IO设备变得更加一致和简单。通过本文的学习,您应该能够更好地理解和应用Linux中的进程IO操作,提高系统编程的效率和能力。
75 34
深度体验阿里云系统控制台:SysOM 让 Linux 服务器监控变得如此简单
作为一名经历过无数个凌晨三点被服务器报警电话惊醒的运维工程师,我对监控工具有着近乎苛刻的要求。记得去年那次大型活动,我们的主站流量暴增,服务器内存莫名其妙地飙升到90%以上,却找不到原因。如果当时有一款像阿里云 SysOM 这样直观的监控工具,也许我就不用熬通宵排查问题了。今天,我想分享一下我使用 SysOM 的亲身体验,特别是它那令人印象深刻的内存诊断功能。
|
1月前
|
linux中的目录操作函数
本文详细介绍了Linux系统编程中常用的目录操作函数,包括创建目录、删除目录、读取目录内容、遍历目录树以及获取和修改目录属性。这些函数是进行文件系统操作的基础,通过示例代码展示了其具体用法。希望本文能帮助您更好地理解和应用这些目录操作函数,提高系统编程的效率和能力。
115 26
|
22天前
|
Linux文件与目录的日常
目录的切换 一般使用(”pwd“)显示当前所在的目录 比如:当前目录是在home下面的,与用户名相同的文件夹,可以使用(”cd“)命令来切换目录; 进入下载目录(”cd home/a/下载“)这种从给目录开头的一长串路经”叫做绝对路径“; 进入图片目录(”cd .. /图片/“)".."代表当前路径的上级路径,相对于当前的目录而言的”叫做相对路径“,(”.“)代表当前路径; 如果,想快速切换,上一个所在目录可以(”cd - / cd..“); 如果,想快速切换,追原始的目录可以(”cd --“); 查看目录及文件
38 14
Linux系统查看操作系统版本信息、CPU信息、模块信息
在Linux系统中,常用命令可帮助用户查看操作系统版本、CPU信息和模块信息
162 23
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
423 8
|
7天前
|
Linux od命令
本文详细介绍了Linux中的 `od`命令,包括其基本语法、常用选项和示例。通过这些内容,你可以灵活地使用 `od`命令查看文件内容,提高分析和调试效率。确保理解每一个选项和示例的实现细节,应用到实际工作中时能有效地处理各种文件查看需求。
43 19
|
18天前
|
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
106 25

热门文章

最新文章