LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2000, 02:49 AM   #1
brain
LQ Newbie
 
Registered: Dec 2000
Posts: 1

Rep: Reputation: 0

I'm trying to determine if a file is a directory or not. d_type is 0 for all files I tested it with. Any help is greatly appreciated. I'm running Linux 2.2.17 and I'm using an ext2 fs.

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main()
{
DIR *dp;
struct dirent *ptrdirstrct;

dp=opendir(".");
if (!dp) {
printf("Couldn't open working directory\n");
exit(0);
}

ptrdirstrct=readdir(dp);
do {
printf("%-30.28s",ptrdirstrct->d_name);

/* This never happens. Why? */
if (ptrdirstrct->d_type==DT_DIR)
printf("DIRECTORY,d_type=%d\n",ptrdirstrct->d_type);
else {
printf("NOT A DIRECTORY, d_type=%d\n",ptrdirstrct->d_type);
ptrdirstrct=readdir(dp);
}
} while (ptrdirstrct!=NULL);

return 0;
}
 
Old 04-30-2001, 06:32 PM   #2
hack6500
LQ Newbie
 
Registered: Apr 2001
Posts: 1

Rep: Reputation: 0
ok, this might be a little old, ntl ...

from READDIR(3)
According to POSIX, the dirent structure contains a field char d_name[] of unspecified size ...
Use of other fields will harm the portability of your programs.

iac, why not just try to open each entry using opendir().

DIR *dp, *tstdp;
...
tstdp=opendir(ptrdirstrct->d_name);
if(tstdp)
printf("DIRECTORY\n");
else
printf("NOT A DIRECTORY\n");


mike
 
Old 04-30-2001, 08:39 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I've always used the S_ISDIR() macro. See the example below:
Code:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>

int main (int argc, char * argv[])
{
    struct stat stDirInfo;
    struct dirent * stFiles;
    DIR * stDirIn;
    char szFullName[MAXPATHLEN];
    char szDirectory[MAXPATHLEN];
    struct stat stFileInfo;

    if ( argc == 2 )
        strncpy( szDirectory, argv[1], MAXPATHLEN - 1 );
    else
        exit(-1);

    if (lstat( szDirectory, &stDirInfo) < 0)
    {
        perror (szDirectory);
        return;
    }
    if (!S_ISDIR(stDirInfo.st_mode))
        return;
    if ((stDirIn = opendir( szDirectory)) == NULL)
    {
        perror( szDirectory );
        return;
    }
    while (( stFiles = readdir(stDirIn)) != NULL)
    {
        sprintf(szFullName, "%s/%s", szDirectory, stFiles -> d_name );

        if (lstat(szFullName, &stFileInfo) < 0)
           perror ( szFullName );

        /* is the file a directory? */
        if (S_ISDIR(stFileInfo.st_mode))
        {
            printf( "Directory: %s\n", szFullName );
        }
        else
        {
            printf( "Filename: %s\n", szFullName );
        }

    }  // end while
    closedir(stDirIn);
    return;
}  // end main
 
Old 05-23-2001, 03:43 PM   #4
fr30n
LQ Newbie
 
Registered: May 2001
Posts: 1

Rep: Reputation: 0
those two fixes may work great... that is until you come across a symbolic link.

if you want to make use of d_type you have to break POSIX
compliance and #define __USE_BSD somewhere in your program.
consult the dirent.h file in /usr/include for the values
associated with d_type.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is d_type in struct dirent ? fssengg Programming 1 03-16-2005 02:39 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration