实验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-
...