checking file type from C program

This is a discussion on checking file type from C program within the Linux Programming forums, part of the Platform Specific Boards category; Hi. How come when I use readdir() to read each file in a directory the d_type field is always NULL? ...

  1. #1
    Registered User geekoftheweek's Avatar
    Join Date
    Mar 2003
    Location
    maine
    Posts
    8

    checking file type from C program

    Hi.

    How come when I use readdir() to read each file in a directory the d_type field is
    always NULL? Isn't it supposed to be different for different file types?

    Also is there a better way then using stat() to find out each file type (directory, regular file, etc.) from a C program?

    Thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    d_type is a char, so it can't be NULL. It can be DT_UNKNOWN, DT_REG, DT_DIR, and a few others. Still, apparently it always returns DT_UNKNOWN for you. What filesystem are you reading?

    Oh, and if the d_type field consistently fails you, there is no way but to call stat().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    15
    On my system, I have this d_type under the compile time flag __USD_BSD, just check dirent.h in /usr/include.
    To get the the file type I have been using stat().
    To get the file system statfs() can be used.

    Regards,
    Amit Sahrawat

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by geekoftheweek View Post
    Hi.

    How come when I use readdir() to read each file in a directory the d_type field is
    always NULL? Isn't it supposed to be different for different file types?
    No. It's not supposed to be anything in particular at all. The following language comes from the Linux readdir(3) manpage (it applies elsewhere):

    According to POSIX, the dirent structure contains a field char d_name[]
    of unspecified size, with at most NAME_MAX characters preceding the
    terminating null character. Use of other fields will harm the porta-
    bility of your programs.
    POSIX 1003.1-2001 also documents the field
    ino_t d_ino as an XSI extension.
    In other words, you can look at d_name and maybe d_ino. Trying to look at ANY OTHER FIELD of the dirent structure leads to unportable, possibly undefined behavior.

    In UNIX there is no such thing as a "file type." A file is a file. Extensions don't matter, the exact "type" of file is not saved anywhere at all within the file system. To figure out what "type" of file you have, you have to open it, look at it, and figure it out yourself. The value of d_type is probably meaningless and certainly has nothing to do with the idea of "type" that you are looking for here. To determine if a file is regular or a directory, you have to call stat().
    Last edited by brewbuck; 11-26-2007 at 10:57 AM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    By type, I think he means file, directory, symlink, device, named pipe, unix domain socket, etc. Those types do exist in Unix.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by CornedBee View Post
    By type, I think he means file, directory, symlink, device, named pipe, unix domain socket, etc. Those types do exist in Unix.
    They do, but they will not be reliably returned in the d_type field of a dirent entry. You simply have to use stat().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21