23,125
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
struct DIR *dp;
struct dirent *temp_dp;
char temp_name[NAME_MAX+1];
struct stat temp_stat;
int temp_fd;
dp=opendir("/dev/");
if(dp==NULL){
fprintf(stderr,"open /dev/ dir error");
exit(0);
}
strcpy(temp_name,"/dev/");
while((temp_dp=readdir(dp))!=NULL){
strncpy(temp_name+sizeof("/dev/")-1,
temp_dp->d_name,NAME_MAX);
if(strcmp(temp_name,"/dev/.")==0 || strcmp(temp_name,
"/dev/..")==0)
continue;
if((temp_fd=open(temp_name,O_RDONLY))<0){
fprintf(stderr,"open file %s error\n",temp_name);
exit(0);
}
if(stat(temp_name,&temp_stat)<0){
fprintf(stderr,"get file %s stat error\n",temp_name);
exit(0);
}
if(isastream(temp_fd)){
printf("%s is a stream device ",temp_name);
if(S_ISREG(temp_stat.st_mode)){
printf("and regular file");
}else if(S_ISDIR(temp_stat.st_mode)){
printf("and dir file");
}else if(S_ISCHR(temp_stat.st_mode)){
printf("and charact device file");
}else if(S_ISBLK(temp_stat.st_mode)){
printf("and block device file");
}else if(S_ISFIFO(temp_stat.st_mode)){
printf("and fifo file");
}
printf("\n");
}
close(temp_fd);
}
}