分析、读取目录
有两种方式
一、glob
二、opendir readdir closedir rewenddir fseek seekdir talldir
接下来只说glob()函数
文章目录
函数解析
#include <glob.h>
int glob(const char *pattern, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob);
void globfree(glob_t *pglob);
参数:
const char *pattern
: 通配符、模式
int flags
:
The argument flags is made up of the bitwise OR of zero or more the following symbolic constants, which modify the behavior of glob():
使用按位或的形式把0个或多个的符号信息或进flags下面
GLOB_ERR
Return upon a read error (because a directory does not have read permission, for example). By default, glob() attempts carry on despite errors, reading all of the directories that it can.
GLOB_MARK
Append a slash to each path which corresponds to a directory.
GLOB_NOSORT
Don't sort the returned pathnames. The only reason to do this is to save processing time. By default, the returned pathnames are sorted.
返回的路径不排序
GLOB_DOOFFS
Reserve pglob->gl_offs slots at the beginning of the list of strings in pglob->pathv. The reserved slots contain null pointers.
GLOB_NOCHECK
If no pattern matches, return the original pattern. By default, glob() returns GLOB_NOMATCH if there are no matches.
没有模式能匹配的,那么就把当前的pattern本身返回
GLOB_APPEND
Append the results of this call to the vector of results returned by a previous call to glob(). Do not set this flag on the first invocation of glob().
追加,往指定的结果追加
GLOB_NOESCAPE
Don't allow backslash ('\') to be used as an escape character. Normally, a backslash can be used to quote the following character, providing a mechanism to turn off the special meaning metacharacters.
int (*errfunc) (const char *epath, int eerrno):
函数,返回值int型,保存当前出去的路径和出去的原因。
在解析第一个参数pattern时,会出现各种各样的问题,比如没有权限解析或者路径不存在
作用:在这里放个函数的入口地址,如果出错,会把解析出错的路径和错误原因回填在这里函数里
glob_t *pglob
解析第一个参数pattern的结果放到glob_t 类型所指向的地址中
返回值
RETURN VALUE
On successful completion, glob() returns zero. Other possible returns are:
GLOB_NOSPACE
for running out of memory,
GLOB_ABORTED
for a read error, and
GLOB_NOMATCH
for no found matches.
typedef struct {
size_t gl_pathc; /* Count of paths matched so far */
计算能和pattern相匹配的名字有多少个,类似于int argc
char **gl_pathv; /* List of matched pathnames. */
和pattern相匹配的名字,类似于char **argv
size_t gl_offs; /* Slots to reserve in gl_pathv. */
} glob_t;
函数使用
/*************************************************************************
> File Name: glob.c
> Author: erfenjiao
> Mail: 630166475@qq.com
> Created Time: 2021年06月20日 星期日 21时47分58秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<glob.h>
#define PAT "/etc/.*"
// int errfunc(const char * errpath , int errno){
// puts(errpath);
// fprintf(stderr , "ERROR MSG :%s\n",strerror(errno));
// return 0;
// }被注释掉的内容为错误处理函数
int main(int argc , char ** argv){
glob_t globres;
//int err;
glob(PAT , 0 , NULL , &globres);
// if(err)
// {
// printf("Error code = %d\n",err);
// exit(1);
// }
for(int i = 0 ; i < globres.gl_pathc ; i++)
{
puts(globres.gl_pathv[i]);
}
globfree(&globres); //释放glob函数第四个参数所开拓的空间
exit(0);
}
结果
/etc/.
/etc/..
/etc/.pwd.lock