Linux系统编程25 目录和用户操作 - 分析目录,读取目录内容的两种方法,glob函数解析

本文介绍如何利用glob()函数查找与解析特定模式的文件,例如在/etc/目录下寻找所有以a开头的.conf文件,并展示如何使用opendir与readdir函数遍历目录流,获取目录中所有条目的名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验1: 使用glob()查找解析目标目录, etc/ 下有多少 以a开头的 .conf文件
实验2: 把目标目录当做目录流 来解析


NAME
glob, globfree - find pathnames matching a pattern, free memory from glob()
glob 解析模式或者通配符

SYNOPSIS
#include <glob.h>

/*
	const char *pattern 目标通配文件名
	flags 特殊要求,位图
	int (*errfunc) (const char *epath, int eerrno) 出错的路径和原因
	glob_t *pglob 存放通配的结果
*/
   int glob(const char *pattern, int flags, 
   			int (*errfunc) (const char *epath, int eerrno),
            glob_t *pglob);
   void globfree(glob_t *pglob);

       typedef struct {
           size_t   gl_pathc;    /* Count of paths matched so far  */
           char   **gl_pathv;    /* List of matched pathnames.  */
           size_t   gl_offs;     /* Slots to reserve in gl_pathv.  */
       } glob_t;

实验1: 使用glob()查找解析目标目录, etc/ 下有多少 以a开头的 .conf文件

mhr@ubuntu:~/work/linux/wenjianxitong/20$ 
mhr@ubuntu:~/work/linux/wenjianxitong/20$ ls /etc/a*.conf
/etc/adduser.conf  /etc/apg.conf  /etc/appstream.conf
mhr@ubuntu:~/work/linux/wenjianxitong/20$ 

.

#include <stdio.h>
#include <stdlib.h>
#include <glob.h>

#define PAT "/etc/a*.conf"
//#define PAT "/etc/*"  //  etc 下的所有文件,但不包括隐藏文件
//#define PAT "/etc/.*"  //  etc 下的隐藏文件

#if 0
static int errfunc_(const char *errpath,int eerrno)
{
	puts(errpath);
	fprintf(stderr,"ERROR MSG:%s\n", strerror(eerrno));
	return 0;
}
#endif

int main()
{
	glob_t globres;
	int i,err;

	err = glob(PAT,0,NULL,&globres);
	if(err)
	{
		printf("Error code = %d\n",err);
		exit(1);
	}

	for(i = 0; i < globres.gl_pathc; i++)
		puts(globres.gl_pathv[i]);

	globfree(&globres);
	exit(0);

}

mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ gcc glob.c 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ ./a.out
/etc/adduser.conf
/etc/apg.conf
/etc/appstream.conf
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ ls -l /etc/a*.conf
-rw-r--r-- 1 root root 3028 Apr 20  2016 /etc/adduser.conf
-rw-r--r-- 1 root root  112 Jan 10  2014 /etc/apg.conf
-rw-r--r-- 1 root root  389 Apr 18  2016 /etc/appstream.conf
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ 

NAME
opendir, fdopendir - open a directory

SYNOPSIS
#include <sys/types.h>
#include <dirent.h>

   DIR *opendir(const char *name);
   DIR *fdopendir(int fd);

DESCRIPTION
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 direc‐tory.

The fdopendir() function is like opendir(), but returns a directory stream for the directory referred to by the open file descriptor fd. After a successful call to fdopendir(), fd is used internally by the implementation, and should not otherwise be used by the application.

RETURN VALUE
The opendir() and fdopendir() functions return a pointer to the directory stream. On error, NULL is returned, and errno is set appropriately.


CLOSEDIR(3) Linux Programmer’s Manual CLOSEDIR(3)

NAME
closedir - close a directory

SYNOPSIS
#include <sys/types.h>

   #include <dirent.h>

   int closedir(DIR *dirp);

READDIR(3) Linux Programmer’s Manual READDIR(3)

NAME
readdir, readdir_r - read a directory

SYNOPSIS
#include <dirent.h>

   struct dirent *readdir(DIR *dirp);
   int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);


      struct dirent {
           ino_t          d_ino;       /* inode number */
           off_t          d_off;       /* not an offset; see NOTES */
           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]; /* filename */
       };

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. If an error occurs, NULL is returned and errno is set appropriately.


实验2: 把目标目录当做目录流 来解析

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>

#define PAT "/etc"

int main()
{
	DIR *dp;
	struct dirent *cur;
	
	dp = opendir(PAT);
	if(dp == NULL)
	{
		perror("opendir()");
		exit(1);
	}

	while((cur = readdir(dp)) != NULL)
		puts(cur->d_name);
	
	closedir(dp);
	exit(0);
}





mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ gcc readdir.c
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/25$ ./a.out 
.
..
printcap
lightdm
sysctl.conf
perl
subuid
nsswitch.conf
machine-id
brltty
acpi
papersize
cupshelpers
brlapi.key
polkit-1
debian_version
libaudit.conf
rc6.d
pm
ghostscript
subgid-
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux老A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值