我正在使用execvp来查找某些文件。这是我正在使用的代码:
char *argv1[] = {"find", "-name", "*.jpg", NULL};
execvp("find",argv1);
我想知道有没有办法将execvp的结果存储/保存到以后用户的数组中? 还有一种方法我只能获得所需的文件而不是整个路径吗?
这是我的readdir()函数。
static void list_dir (const char * dir_name)
{
char *ArrayFiles[100];
DIR * d;
/* Open the directory specified by "dir_name". */
int i = 0;
d = opendir (dir_name);
/* Check it was opened. */
if (!d) {
fprintf (stderr, "Cannot open directory '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
while (1) {
struct dirent * entry;
const char * d_name;
/* "Readdir" gets subsequent entries from "d". */
entry = readdir (d);
if (! entry) {
/* There are no more entries in this directory, so break
out of the while loop. */
break;
}
d_name = entry->d_name;
if ((strstr(d_name, ".jpg")) || (strstr(d_name, ".JPG"))){
/* skip printing directories */
if (! (entry->d_type & DT_DIR)) {
char *filename = entry->d_name;
printf ("%s\n", d_name);
}
}
if (entry->d_type & DT_DIR) {
/* skip the root directories ("." and "..")*/
if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) {
int path_length;
char path[PATH_MAX];
path_length = snprintf (path, PATH_MAX,
"%s/%s", dir_name, d_name);
if (path_length >= PATH_MAX) {
fprintf (stderr, "Path length has got too long.\n");
exit (EXIT_FAILURE);
}
/* Recursively call "list_dir" with the new path. */
list_dir (path);
}
}
}
/* After going through all the entries, close the directory. */
if (closedir (d)) {
fprintf (stderr, "Could not close '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
}