Re: [gtk-list] Re: General C question - file attributes



* Travis Loyd (lelandg@usa.net) [990815 15:35]:
> On Sun, Aug 15, 1999 at 09:25:55AM -0400, John C Atkeson wrote:
> >   
> >   I'm reading directories with opendir, readdir and closedir.   Um... how
> I was looking for these... glad I read your question.
> > do I detect file attributes, specifically how to distinguish files from
> > sub-directories? 
> In the man page for readdir I found this:
>               struct dirent {
>                       long            d_ino;  /* inode number */
>                       off_t           d_off;  /* offset to the next dirent */
>                       unsigned short  d_reclen;/* length of this record */
>                       unsigned char   d_type;         /* type of file */
>                       char            d_name[256];    /* filename */
>               };
> That is what is returned from readdir, this is just a guess but I would think
> that d_type is what you are looking for.
> 
> Travis Loyd <lelandg@usa.net>

It should be noted that on some systems, d_type will always return a
value of DT_UNKNOWN. In this case I would suggest the use of the stat
call.

>From libc docs,

[...]
   To examine the attributes of files, use the functions `stat',
`fstat' and `lstat'.  They return the attribute information in a
`struct stat' object.  All three functions are declared in the header
file `sys/stat.h'.

 - Function: int stat (const char *FILENAME, struct stat *BUF)
     The `stat' function returns information about the attributes of the
     file named by FILENAME in the structure pointed at by BUF.

     If FILENAME is the name of a symbolic link, the attributes you get
     describe the file that the link points to.  If the link points to a
     nonexistent file name, then `stat' fails, reporting a nonexistent
     file.

     The return value is `0' if the operation is successful, and `-1'
     on failure.  In addition to the usual file name errors (*note File
     Name Errors::., the following `errno' error conditions are defined
     for this function:
[...]

Tom.
-- 
            .-------------------------------------------------------.
    .^.     | Tom Gilbert, England | tom@tomgilbert.freeserve.co.uk |
    /V\     |----------------------| www.tomgilbert.freeserve.co.uk |
   // \\    | Sites I recommend:   `--------------------------------|
  /(   )\   | www.freshmeat.net www.enlightenment.org www.gnome.org |
   ^^-^^    `-------------------------------------------------------'



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]