- Code: Select all
while(argc > 1) {
fname = argv[1];
if(!lstat(fname, &statbuf)) {
if(S_ISDIR(statbuf.st_mode)) {
/*printf("\n%s:\n", fname);*/
if ((dir = opendir(fname)) == NULL) {
perror("Failed to open directory");
}
printf("\n%s:\n", fname);
while((file_info = readdir(dir)) != NULL) {
fname = file_info->d_name;
if (!lstat(fname, &statbuf)) { /* <-- This is the line that errors! */
if((DASH_A == (opt & DASH_A)) &&
(DASH_L != (opt & DASH_L))) {
if(fname[0] != '.') {
printf(" %s\n", fname);
} else {
printf(" %s\n", fname);
}
} else if(DASH_L == (opt & DASH_L)) {
if (DASH_A == (opt & DASH_A)) {
file_list(file_info->d_name, statbuf, opt);
} else {
if(fname[0] != '.') {
file_list(file_info->d_name, statbuf, opt);
}
}
} else {
if(fname[0] != '.') {
printf(" %s\n", fname);
}
}
} else {
perror(fname);
}
}
closedir(dir);
} else {
printf("%s\n", fname);
}
} else {
perror(fname);
}
++argv;
--argc;
}
The commented line is the line in question. When I run this it returns fine for the first two directories in the target dir ("." and "..") but once it hits a real direcrtory or file it returns the error for each file or dir it steps through.
Is there any reason why this would be happening? I cannot see it..
The full program can be found at the following links (in case that might help you help me
http://www.perlskripts.com/downloads/newls.c <-- main C file
http://www.perlskripts.com/downloads/myls.h <-- .h file


