NAME
readdir, readdir_r - read directory
SYNOPSIS
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
Code Example.
/* just compile and run below c code, it will list all the process pid dir from /proc system. */
#include
#include
#include
#include
#include
#include
int main ()
{
DIR *fd_proc;
struct dirent *dirp;
int fd_stat, cur_pid, cnt;
char buf[1000], pdir[50], statfile[50], procname[50];
int pid;
sprintf (pdir, "/proc/");
if (!(fd_proc = opendir (pdir)))
{
printf( "Unable to open dir = %s, errno \n", pdir);
return -1;
}
while ((dirp = readdir (fd_proc)) != NULL)
{
printf(" %s, \n",dirp->d_name);
pid = atoi (dirp->d_name);
if (pid <= 0)
continue;
sprintf (statfile, "/proc/%s/stat", dirp->d_name);
if ((fd_stat = open (statfile, O_RDONLY)) < 0)
{
printf ( "Unable to open stat = %s, errno %d\n", dirp->d_name, errno);
closedir (fd_proc);
continue ;
}
}
printf("Hello World");
return 1;
}
DESCRIPTION
Do man readdir.
No comments:
Post a Comment