|
发表于 2009-8-4 11:37:54
|
显示全部楼层
在opendir之后,chdir一下,就可以了:
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
int main(void)
{
DIR *dp;
struct dirent *ep;
struct stat st;
char dirp[50];
printf("please input a dir name:\n");
scanf("%s", dirp);
printf("filename:\ttype:\tpermission\taccesstime\tlastmodtime\tsize\t\n");
dp = opendir(dirp);
chdir(dirp);
if (dp != NULL) {
while ((ep = readdir(dp)) != NULL) {
if (strcmp(".", ep->d_name) ==0 ||
strcmp("..", ep->d_name) ==0 ) {
continue;
}
printf("%s\n", ep->d_name);
if (stat(ep->d_name, &st) >= 0) {
// printf("%s\t",ep->d_name);
if ((st.st_mode&S_IFMT)==S_IFDIR)
printf("directory\t");
else if ((st.st_mode&S_IFMT)==S_IFBLK)
printf("block file\t");
else if ((st.st_mode&S_IFMT)==S_IFCHR)
printf("character file\t");
else if ((st.st_mode&S_IFMT)==S_IFREG)
printf("ordinary file\t");
else if ((st.st_mode&S_IFMT)==S_IFIFO)
printf("pipefile\t");
else
printf("other\t");
printf("%o\t", st.st_mode&0xfff);
printf("%15s\t", ctime(&st.st_atime));
printf("%15s\t", ctime(&st.st_mtime));
printf("%ld\n", st.st_size);
} else {
printf("stat failed\n");
}
}
} else {
puts("could not open the dir \n");
}
closedir(dp);
return 0;
} |
|