#include
#include
#include
#include
#include
int mm (DIR *dp) {
DIR *ap;
struct dirent *dirp;
while ((dirp = readdir(dp)) != NULL) {
if (strcmp(dirp->d_name , ".") && strcmp(dirp->d_name, "..")) {
printf("%s\n",dirp->d_name);
if ((ap = opendir(dirp->d_name)) != NULL)
if (!strcmp(ap,"DIR*"))
mm(ap);
}
}
closedir(dp);
return 0;
}
int main(int argc, char *argv[]) {
DIR *dp;
if (argc != 2)
printf("%s","usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
printf("%s", "kkdkkd");
else
mm(dp);
_exit(0);
}
-----------------------------------------
#include
#include
#include
#include
#include
#include
void printdir(char *dir, int depth){
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL){
fprintf(stderr,"cannot open directory: %s\n",dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
if(strcmp("..",entry->d_name) == 0||
strcmp(".",entry->d_name) == 0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
printdir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char *argv[]) {
printdir(argv[1],0);
}
阅读(1238) | 评论(0) | 转发(0) |