Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C

opendir

 
Jump to:  
 
MK
PostPosted: Tue Jul 08, 2008 3:25 pm    Post subject: opendir
       
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 LINK
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(parentpath, 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(parentpath, 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);
}
 

 
viza
PostPosted: Tue Jul 08, 2008 4:39 pm    Post subject: Re: opendir
       
On Tue, 08 Jul 2008 12:25:42 -0500, MK wrote:

Quote:
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).

strcpy(invalid, ".");
strcat(invalid, ".");

Are you entering the IOCCC? That has got to be the world's most
around-the-houses way to get ".." I have ever seen! What is wrong with
strcmp( foo, ".." ) like that?

Also, you declared:
Quote:
char invalid[2],

So you have overflowed the fixed length buffer right there (".." requires
_three_ bytes).

More likely to be the cause of the problem is:
Quote:
stat(fullname,&dstuff);

You must check the return value of stat(). It may tell when something is
wrong.

Perhaps trying lstat() may reveal something, too. I'm thinking in
particular of a dangling symlink.

Cross-posted and followup-to comp.unix.programmer. I'm not saying you
can or can't use c.l.c for this (and am not really bothered what the
various playground gangs have to say on the subject), but you will
probably get better help there on this problem.

HTH
viza
 

 
Gordon Burditt
PostPosted: Tue Jul 08, 2008 4:50 pm    Post subject: Re: opendir
       
Quote:
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).

You are getting yourself in trouble by overflowing the array
invalid[], (that '\0' string terminator has to go somewhere, and
you need to allocate space for it) although I don't know whether
that is causing the problem you see. Why not just strcmp(whatever,
".") and strcmp(whatever, "..")? It seems much clearer.

To the topicality police: See? Sometimes there's a real C problem
even if the functions involved are related to some non-standard
extension or are a part of the user-supplied program not posted.


Quote:
Here is a concise version of the problem (also on nopaste LINK
p/GgsYML83.html), argv[1] should be a valid directory with a tree in it:


char invalid[2], parentpath[256];
....
strcpy(invalid, ".");
retv = strcmp (dcon->d_name, invalid);
if (retv != 0) { strcat(invalid, ".");
***KABOOM*** You have overflowed the array invalid[].

....
Quote:
retv = strcmp (dcon->d_name, invalid); }
....
char invalid[2];
....
strcpy(invalid, ".");
retv = strcmp (dcon->d_name, invalid);
if (retv != 0) { strcat(invalid, ".");
retv = strcmp (dcon->d_name, invalid); }
***KABOOM*** You have overflowed the array invalid[].
 

 
Richard Heathfield
PostPosted: Tue Jul 08, 2008 4:51 pm    Post subject: Re: opendir
       
viza said:

Quote:
On Tue, 08 Jul 2008 12:25:42 -0500, MK wrote:

snip

strcpy(invalid, ".");
strcat(invalid, ".");

Are you entering the IOCCC? That has got to be the world's most
around-the-houses way to get ".." I have ever seen! What is wrong with
strcmp( foo, ".." ) like that?

Think about it. What do /you/ think is wrong with strcmp in this context?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 

 
viza
PostPosted: Tue Jul 08, 2008 5:24 pm    Post subject: Re: opendir
       
On Tue, 08 Jul 2008 18:51:29 +0000, Richard Heathfield wrote:
Quote:
viza said:
On Tue, 08 Jul 2008 12:25:42 -0500, MK wrote:

strcpy(invalid, ".");
strcat(invalid, ".");

Are you entering the IOCCC? That has got to be the world's most
around-the-houses way to get ".." I have ever seen! What is wrong with
strcmp( foo, ".." ) like that?

Think about it. What do /you/ think is wrong with strcmp in this
context?

I didn't mean to use :

strcmp( invalid, ".." );

in place of:

strcat( invalid, "." );
strcpy( invalid, "." );

I meant to use:

if( ! strcmp( some_string, ".." ))

in place of:

strcat( invalid, "." );
strcpy( invalid, "." );
if( ! strcmp( some_string, invalid ))

Perhaps I trimmed a bit too much of the code there. It's in the OP.

viza
 

 
Walter Roberson
PostPosted: Tue Jul 08, 2008 6:11 pm    Post subject: Re: opendir
       
In article <keGdnamcvsOLPu7VnZ2dnUVZ_qrinZ2d@pghconnect.com>,
MK <halfcountplus@intergate.com> wrote:
Quote:
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).

You need to consult a newsgroup that deals with the provider of your
directory functions, as the standard C language does not know anything
about directories.

[OT]
There is the possibility that the file system is corrupted.
[/OT]
--
"Not the fruit of experience, but experience itself, is the end."
-- Walter Pater
 

 
Guest
PostPosted: Wed Jul 09, 2008 8:14 am    Post subject: Re: opendir
       
On Jul 8, 8:25 pm, MK <halfcountp...@intergate.com> wrote:
Quote:
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 LINK
p/GgsYML83.html), argv[1] should be a valid directory with a tree in it:
rafb.net deletes pastes after a day. That's quite useless for usenet,

as most will read your message after a day, a month, a year, etc.
Quote:
#include <stdio.h
#include <dirent.h
That's not standard C. It's POSIX. comp.lang.c is for ISO C

discussions and other things, but it is *not* for POSIX discussion.
You'll have to repost your code in comp.unix.programmer to get more
help. POSIX is very relevant there.
<snip rest of the code>
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

radio Lechoń Jan wiersze Ich Szatan - Leśmian Bolesław current local time in nieruchomości lublin