#include <stdio.h>
#include <dirent.h>
void type(unsigned char dType)
{
switch(dType){
case DT_BLK:
puts(" This is a block device. ");
break;
case DT_CHR:
puts(" This is a character device.");
break;
case DT_DIR:
puts(" This is a directory.");
break;
case DT_FIFO:
puts(" This is a named pipe (FIFO).");
break;
case DT_LNK:
puts(" This is a symbolic link.");
break;
case DT_REG:
puts(" This is a regular file.");
break;
case DT_SOCK:
puts(" This is a Unix domain socket.");
break;
case DT_UNKNOWN:
puts("The file type is unknown.");
break;
default:
break;
}
}
int main(int argc,char** argv)
{
DIR *dirptr = NULL;
struct dirent *entry;
if(argc<2)
{
printf("the command need a dirname\n");
return 1;
}
if(argc>2)
{
printf("the program can only deal with one dir at once\n");
return 1;
}
if((dirptr = opendir(argv[1])) == NULL)
{
printf("open dir error !\n");
return 1;
}
else
{
while (entry = readdir(dirptr))
{
//printf("%s\n", entry->d_name);
printf("%s\t\t", entry->d_name);
type(entry->d_type);
}
closedir(dirptr);
}
return 0;
}