遍历当前系统所有打开读文件

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
	struct dirent *dirp;
	DIR *fdir;
	fdir = opendir("/proc");
	while(dirp = readdir(fdir)){
		if(strcmp(dirp->d_name, ".")==0 || strcmp(dirp->d_name, "..") == 0)
			continue;
		if(dirp->d_type == DT_DIR) {
//			printf("%s\n", dirp->d_name );
//			getchar();
			char buf[256];
			sprintf(buf, "/proc/%s/fd", dirp->d_name);
			DIR *fdir2; 
			struct dirent *dirp2;
		       if((fdir2 = opendir(buf)) != NULL ){
		       		while(dirp2 = readdir(fdir2)){
					//printf("%s:%s\n", dirp->d_name, dirp2->d_name);
					char buf2[256];
					char tmp2[256];
					sprintf(tmp2, "%s/%s", buf, dirp2->d_name);
					readlink(tmp2, buf2, 256);
					printf("%s:\t%s\t%s\n", dirp->d_name, dirp2->d_name, buf2);
//					getchar();
				}	
		       }
			
		}
	}

	
	return 0;
}

 

posted @ 2012-08-26 15:21  庄庄庄  阅读(444)  评论(3编辑  收藏  举报