开发者

Regarding checking for file or directory

开发者 https://www.devze.com 2023-01-22 16:34 出处:网络
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

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. Can any one pleeas help me. I am using QNX Neurtion RTOS

#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.htm开发者_C百科l

Following information may be helpful. lstat for file is failing with errno set to 2. I am not sure why, can any one know this.


Just a guess; since you're not checking for an error after your lstat call, the eStat buffer could be containing the result of the last successful call. Try checking if lstat returns -1.

readdir() on Linux is fundamentally different, so I can't fully test on my system. See the sample programs at link text and link text. Modifying the lstat sample code, this seems to work for me:


#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

int main( int argc, char **argv )
  {
    int ecode = 0;
    int n;
    struct stat sbuf;

    for( n = 1; n < argc; ++n ) {
      if( lstat( argv[n], &sbuf ) == -1 ) {
        perror( argv[n] );
        ecode++;

      } else if( S_ISDIR( sbuf.st_mode ) ) {
        printf( "%s is a dir\n", argv[n] );

      } else {
        printf( "%s is not a dir\n", argv[n] );
      }
    }
}

I don't know if that helps any. Note that the readdir() sample code uses opendir() as schot suggested. But I can't explain why your readdir() seems to work regardless.


My compiler says: "warning: 'dir' is used uninitialized in this function" You may want to add dir = opendir(root); after you initialize root. And don't forget to add some error checking.

I doubt this causes your problem, jcomeau_ictx is probably right. If lstat returns -1 it sets errno to a value that signifies the type of error. Look at its man page and the man page for strerror


Even though this question was asked long time ago, and I found it because this quesion. but the answers here didn't really solve the problem, so I decided to post the answer which I wrote on another post, such that if anyone had the same problem and used google to find this page, there is a clear answer.

The real reason of S_ISDIR not working as expected is dp->d_name contains only the name of the file, you need to pass the full path of the file to lstat().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号