How to list all the files in a directory
I have a code below that list the PID in the directory for /proc. How do I modified it so that it would list
all files name in any directory such as /var/usr/.
I would like to have variable that control whether or not
it would list all files in all subdirectories as well.
==========================
Code:
find_processes_and_shut_it_down (char dvpnd_char[21])
{
DIR *d = opendir("/proc");
FILE *fp, *fopen();
char *end;
struct dirent *de;
pid_t pid;
char buf[100];
while ((de = readdir(d)) != NULL)
{
/* for this directory, check the name, we want only
numeric filenames (pid). */
pid = strtoul(de -> d_name, & end, 10);
if (*end != '\0')
continue; // skip this dir.
sprintf( buf, "/proc/%d/cmdline", pid);
fp = fopen(buf, "rt");
if (fp == NULL)
continue;
fgets(buf, sizeof(buf), fp);
fclose(fp);
/* compare command line to see if it is
the program you want. */
/* Perhaps drop the first argument */
/* and just compare the first token and
see if the name matches. */
/* printf ("buf = %s pid = %d\n", buf, pid); */
if (strcmp (buf, dvpnd_char) == 0)
{
/* the pid in question is in pid! kill this
pid (process id) */
printf ("\n");
printf ("\n");
printf ("string match. buf = %s pid = %d
dvpnd_char = %s\n", buf, pid, dvpnd_char);
kill (pid, SIGTERM);
}
}
closedir(d);
pclose(fp);
}