代码编织梦想


在这里插入图片描述


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.


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


无所不能的怪哥哥
本文链接:https://blog.csdn.net/qq_43471489/article/details/125000546

linux中关于api函数与系统调用-爱代码爱编程

相信大家对 API并不陌生,对系统调用也不陌生,但是,对两者之间的区别于联系可能并不是十分清楚。 API API(Application Programming Interface,应用程序编程接口),指的是我们用户程序

linux调用系统api出错,如何查看错误原因?_洪水猛兽行的博客-爱代码爱编程

当调用系统api出错后,可以通过打印errno这个变量来获得错误码值, errno变量定义在/usr/include/errno.h里, #ifndef errno extern int errno; #endif 可以看到这是个全局变量, 当调用系统api出错后,系统会把错误码赋值给errno,那么知道了错误码,怎么知道对应意思呢? 最笨的办

系统调用、api之间的关系(图)__kimcho的博客-爱代码爱编程_系统调用关系

1.为什么用户程序不能直接访问系统内核模式提供的服务?      在linux中,将程序的运行空间分为内核空间与用户空间(内核态和用户态),在逻辑上它们之间是相互隔离的,因此用户程序不能访问内核数据,也无法使用内核函数。当用户进程必须访问内核或使用某个内核函数时,就得使用系统调用(System Call)。在Linux中,系统调

一、系统调用和api(用户编程接口)的理解_攻城lion的博客-爱代码爱编程_系统调用和api的区别

一、系统调用和API 1.1、我们先说一下为什么用户应用程序无法直接获得操作系统内核所提供的一些服务。 ​ 在Linux中为了更好的保护内核空间 ,将程序的运行空间分为用户态和内核态。他们分别运行在不同的级别上,二者在逻

超详细!操作系统实验三 系统调用(哈工大李治军)-爱代码爱编程

实验3 系统调用 提醒 这次实验涉及的宏过于复杂,加上本人能力有限,我也没有花大量时间去研究每一段代码,只是理解到每一段代码做了什么这一程度。 实验目的 此次实验的基本内容是:在 Linux 0.11 上添加两个系统调用,并编写两个简单的应用程序测试它们。 iam() 第一个系统调用是 iam(),其原型为: int iam(const cha

Linux 系统调用详解-爱代码爱编程

系统调用 系统调用是操作系统提供给用户程序调用系统服务的一组“特殊”接口。 目录 系统调用前言系统调用系统调用的作用中断实现系统调用系统调用号和系统调用表系统调用号:系统调用表:执行系统调用glibc库syscall 调用glibc库glibc函数库对比Syscall调用 前言 系统调用 系统调用可被看成是一个内核与用户空间程序交互

程序员自我修养阅读笔记——系统调用与API-爱代码爱编程

1 系统调用 1.1 系统调用简介   由操作系统实现提供的所有系统调用所构成的集合即程序接口或应用编程接口(Application Programming Interface,API)。是应用程序同系统之间的接口。操作系统本身是硬件资源的管理者,系统调用就是用户通过系统访问硬件资源和一些核心功能的媒介。一般涉及磁盘访问、网络、进程管理、外设等等系统资

复习一下linux常用命令,孰能生巧~_linux查看某文件夹是否有config命令-爱代码爱编程

目录 一.文件目录类 1、查看文件类型 2、列出文件/目录的命令 3、操作文件命令 4、打包、压缩命令 5、输出文件、查看文件类  二、系统操作类 1、开关机 2、显示时间类 3、网络、进程类 4、系统服务 5、防火墙 6、其他类    因为热爱所以坚持,因为热爱所以等待。熬过漫长无戏可演的日子,终于换来了人生的春天,共勉!!