I don't use c much but i've done lots of perl, perl/Tk, and shellcode on
LINUX.
I'm trying to do things with directory trees and i occasionally get back
an errno from opendir "No such file or directory" while my program is
working its way thru a tree. This always happens with the same directory
(ie. it is not totally random) although there is nothing different or
unusual about the ownership or permissions (ie. there actually is such a
readable directory).
Here is a concise version of the problem (also on nopaste http://rafb.net/
p/GgsYML83.html), argv[1] should be a valid directory with a tree in it:
#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);
int retv;
char invalid[2], parentpath[256];
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(parentpa th, fullname); // since fullname is operated on
while (dcon = readdir (dir)) {
stat(fullname,& dstuff);
strcpy(invalid, ".");
retv = strcmp (dcon->d_name, invalid);
if (retv != 0) { strcat(invalid, ".");
retv = strcmp (dcon->d_name, invalid); }
if (dcon->d_type == 4 && retv != 0) {
strcat(fullname , "/");
strcat(fullname , dcon->d_name); nextdir(); }
}
closedir (dir);
strcpy(fullname ,parentpath); // resets the path path
}
int main (int argc, char *argv[]) {
int retv, bytes;
char invalid[2];
errno = 0;
DIR *dir = opendir (argv[1]);
if (dir == NULL ) {
printf ("For %s ERROR: %s\n", fullname, strerror(errno) );
exit(1);}
struct dirent *dcon;
struct stat dstuff;
strcpy(parentpa th, fullname);
printf ("Top Level '%s':\n", argv[1]);
while (dcon = readdir (dir)) {
strcpy(fullname , argv[1]);
strcat(fullname , "/");
strcat(fullname , dcon->d_name);
stat(fullname,& dstuff);
strcpy(invalid, ".");
retv = strcmp (dcon->d_name, invalid);
if (retv != 0) { strcat(invalid, ".");
retv = strcmp (dcon->d_name, invalid); }
if (dcon->d_type == 4 && retv != 0) { nextdir(); }
}
closedir (dir);
}
LINUX.
I'm trying to do things with directory trees and i occasionally get back
an errno from opendir "No such file or directory" while my program is
working its way thru a tree. This always happens with the same directory
(ie. it is not totally random) although there is nothing different or
unusual about the ownership or permissions (ie. there actually is such a
readable directory).
Here is a concise version of the problem (also on nopaste http://rafb.net/
p/GgsYML83.html), argv[1] should be a valid directory with a tree in it:
#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);
int retv;
char invalid[2], parentpath[256];
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(parentpa th, fullname); // since fullname is operated on
while (dcon = readdir (dir)) {
stat(fullname,& dstuff);
strcpy(invalid, ".");
retv = strcmp (dcon->d_name, invalid);
if (retv != 0) { strcat(invalid, ".");
retv = strcmp (dcon->d_name, invalid); }
if (dcon->d_type == 4 && retv != 0) {
strcat(fullname , "/");
strcat(fullname , dcon->d_name); nextdir(); }
}
closedir (dir);
strcpy(fullname ,parentpath); // resets the path path
}
int main (int argc, char *argv[]) {
int retv, bytes;
char invalid[2];
errno = 0;
DIR *dir = opendir (argv[1]);
if (dir == NULL ) {
printf ("For %s ERROR: %s\n", fullname, strerror(errno) );
exit(1);}
struct dirent *dcon;
struct stat dstuff;
strcpy(parentpa th, fullname);
printf ("Top Level '%s':\n", argv[1]);
while (dcon = readdir (dir)) {
strcpy(fullname , argv[1]);
strcat(fullname , "/");
strcat(fullname , dcon->d_name);
stat(fullname,& dstuff);
strcpy(invalid, ".");
retv = strcmp (dcon->d_name, invalid);
if (retv != 0) { strcat(invalid, ".");
retv = strcmp (dcon->d_name, invalid); }
if (dcon->d_type == 4 && retv != 0) { nextdir(); }
}
closedir (dir);
}
Comment