readdir() and S_ISDIR(stat.st_mode) question (OT ?)

Discussion in 'C Programming' started by electric sheep, Mar 4, 2004.

  1. I'm not sure if this is POSIX or not ... and indeed if POSIX
    is OT here or not, but if the subject made sense to you ....

    I have a very simple program here, but it seems to be returning
    a "true" value to the query S_ISDIR() even when the directory
    entry is not a directory.

    #include <stdio.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <errno.h>

    int main(int argc, char *argv[]) {
    DIR *dir;
    struct dirent *entry;
    struct stat eStat;
    char *root;
    int i;

    root = argv[1];

    while((entry = readdir(dir)) != NULL) {
    lstat(entry->d_name, &eStat);
    if(S_ISDIR(eStat.st_mode))
    printf("found directory %s\n", entry->d_name);
    else
    printf("not a dir\n");
    }
    return 0;
    }


    sample output:
    found directory .
    found directory ..
    found directory NCURSES-Programming-HOWTO-html.tar.gz
    found directory ncurses_programs
    found directory ncurses.html
    electric sheep, Mar 4, 2004
    #1
    1. Advertising

  2. On Thu, 04 Mar 2004 05:08:53 GMT,
    electric sheep <> wrote:
    > I'm not sure if this is POSIX or not ... and indeed if POSIX
    > is OT here or not, but if the subject made sense to you ....


    Yes, it is a POSIX question, and it is offtopic here.

    comp.unix.programmer is the place where they discuss POSIX.

    Martien
    --
    |
    Martien Verbruggen | Think of the average person. Approximately
    Trading Post Australia | half of the people out there are dumber.
    |
    Martien Verbruggen, Mar 4, 2004
    #2
    1. Advertising

  3. electric sheep

    Villy Kruse Guest

    On 04 Mar 2004 05:28:37 GMT,
    Martien Verbruggen <> wrote:


    > On Thu, 04 Mar 2004 05:08:53 GMT,
    > electric sheep <> wrote:
    >> I'm not sure if this is POSIX or not ... and indeed if POSIX
    >> is OT here or not, but if the subject made sense to you ....

    >
    > Yes, it is a POSIX question, and it is offtopic here.
    >
    > comp.unix.programmer is the place where they discuss POSIX.
    >



    Regardless of that, it is still a good idea to check possible errors
    also from non-ANSI-C functions.


    Villy
    Villy Kruse, Mar 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    3
    Views:
    9,695
    Pete Becker
    Jun 2, 2005
  2. John

    stat.st_mode deciphering

    John, Jun 19, 2004, in forum: C Programming
    Replies:
    1
    Views:
    725
  3. Patrick Useldinger

    os.stat('<filename>')[stat.ST_INO] on Windows

    Patrick Useldinger, Feb 27, 2005, in forum: Python
    Replies:
    6
    Views:
    1,157
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 3, 2005
  4. Magesh
    Replies:
    3
    Views:
    431
    Gordon Burditt
    Oct 5, 2007
  5. Deniz Dogan

    Help with st_mode in sys/stat.h

    Deniz Dogan, May 11, 2008, in forum: C Programming
    Replies:
    3
    Views:
    850
Loading...

Share This Page