c - Excevp into array to be used later -
i using execvp find files. code using:
char *argv1[] = {"find", "-name", "*.jpg", null}; execvp("find",argv1);
i wondering there way store/hold results execvp array later user? there way can desired file instead of whole path?
this readdir() functoin.
static void list_dir (const char * dir_name) { char *arrayfiles[100]; dir * d; /* open directory specified "dir_name". */ int = 0; d = opendir (dir_name); /* check 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 "d". */ entry = readdir (d); if (! entry) { /* there no more entries in directory, break out of 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 root directories ("." , "..")*/ 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 long.\n"); exit (exit_failure); } /* recursively call "list_dir" new path. */ list_dir (path); } } } /* after going through entries, close directory. */ if (closedir (d)) { fprintf (stderr, "could not close '%s': %s\n", dir_name, strerror (errno)); exit (exit_failure); } }
Comments
Post a Comment