I just started working with c but have done a lot of perl and shellcode. I keep getting back an inappropriate "errno" from opendir ("No such file or directory") regarding directories which do exist and have no ownership or permission irregularities.
here's a concise version of the problem, also at nopaste (http://rafb.net/p/6HHgvD40.html). argv[1] should be the path to the top of a directory tree. the binary should then spit out an unsorted list of all the directories in the tree. it works sometimes -- the errno comes from line 15...
Code:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
char fullname[256], parentpath[256];
int nextdir () {
printf ("nextdir: %s\n", fullname);
errno = 0;
DIR *dir = opendir (fullname);
if (dir == NULL ) {
printf ("For %s ERROR: %s\n", fullname, strerror(errno));
exit(1);}
struct dirent *dcon;
struct stat dstuff;
strcpy(parentpath, fullname); // since fullname is operated on
while (dcon = readdir (dir)) {
if (stat(fullname,&dstuff) != 0)
printf("stat error for %s\n", dcon->d_name);
if ((dcon->d_type == 4) &&
(strcmp (dcon->d_name, ".")) != 0 &&
(strcmp (dcon->d_name, "..")) != 0) {
strcat(fullname, "/");
strcat(fullname, dcon->d_name); nextdir(); }
}
closedir (dir);
strcpy(fullname,parentpath); // resets the path
}
int main (int argc, char *argv[]) {
errno = 0;
DIR *dir = opendir (argv[1]);
if (dir == NULL ) {
printf ("For %s ERROR: %s\n", argv[1], strerror(errno));
exit(1);}
struct dirent *dcon;
struct stat dstuff;
printf ("Top Level '%s':\n", argv[1]);
while (dcon = readdir (dir)) {
strcpy(fullname, argv[1]);
strcat(fullname, "/");
strcat(fullname, dcon->d_name);
if (stat(fullname,&dstuff) != 0)
printf("stat error for %s\n", dcon->d_name);
if ((dcon->d_type == 4) &&
(strcmp (dcon->d_name, ".")) != 0 &&
(strcmp (dcon->d_name, "..")) != 0) {
nextdir(); }
}
closedir (dir);
}