opendir

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MK

    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 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);
    }

  • Walter Roberson

    #2
    Re: opendir

    In article <keGdnamcvsOLPu 7VnZ2dnUVZ_qrin Z2d@pghconnect. com>,
    MK <halfcountplus@ intergate.comwr ote:
    >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

    Comment

    • viza

      #3
      Re: opendir

      On Tue, 08 Jul 2008 12:25:42 -0500, MK wrote:
      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:
      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:
      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.progr ammer. 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

      Comment

      • Richard Heathfield

        #4
        Re: opendir

        viza said:
        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

        Comment

        • Gordon Burditt

          #5
          Re: opendir

          >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.

          >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:
          >
          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[].
          ....
          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[].

          Comment

          • viza

            #6
            Re: opendir

            On Tue, 08 Jul 2008 18:51:29 +0000, Richard Heathfield wrote:
            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

            Comment

            • vippstar@gmail.com

              #7
              Re: opendir

              On Jul 8, 8:25 pm, MK <halfcountp...@ intergate.comwr ote:
              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:
              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.
              #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.progr ammer to get more
              help. POSIX is very relevant there.
              <snip rest of the code>

              Comment

              Working...