On Fri, 10 Aug 2007, S h i v wrote:

> Hi,
>
> A linux app that I want to build on SX using sunpro uses "struct
> dirent" that has non-standard gcc extensions (ex: d_type) that are not
> available in sunpro.

Not gcc extensions. Linux and/or BSD specifics.

The standard for dirent.h is quite clear - see dirent(3HEAD) manpage:

      The internal  format  of  directories  is  unspecified.  The
      <dirent.h> header defines the following type:

      DIR    A type representing a directory stream.

      The header also defines the structure dirent, which includes
      the following members:

        ino_t d_ino        /* file serial number */
        char  d_name[]     /* name of entry */

You cannot rely on the presence of any other members but these two, if you 
want to write portable code.

> I wrote a test program using stat and mask mode S_IFMT as defined
> sys/stat.h to get a substitute(snippet below), but the output does not
> recognize symlinks. Why so?

Because you're using stat() where you should be using lstat(). Again, see 
stat(2) manpage:

      The lstat() function  obtains  file  attributes  similar  to
      stat(),  except  when  the named file is a symbolic link; in
      that case lstat() returns information about the link,  while
      stat()  returns  information  about the file the link refer-
      ences.

I.e. if you want to see the symlink, aka S_IFLNK in st_mode, you need to 
call lstat() not stat(). The latter resolves the symlink.

Best regards,
FrankH.

>
> Code snippet
> -----------------------
> /* Loop through directory entries. */
> while ((dp = readdir(dir)) != NULL) {
>       /* Get file type information. */
>       if (stat(dp->d_name, &statbuf) == -1)
>               continue;
>       filetypeInformation = statbuf.st_mode & S_IFMT ;
>       printf("TYPE = %x  %20s    ",filetypeInformation, dp->d_name);
>       switch(filetypeInformation)
>       {
>               case S_IFREG : printf("Regular file. ");                        
> break;
>               case S_IFDIR : printf("Directory. ");                           
> break;
>               case S_IFLNK : printf("Symbolic link. ");                       
> break;
>               case default : printf("I do not care about 
> block/chr/fifo/socket. "); break;
>       }
>       printf("\n");
> }
>
> Output
> ------------------
> bash-3.00$ ./a.out
> TYPE = 4000                     .     Directory.
> TYPE = 4000                    ..     Directory.
> TYPE = 8000           hi.softlink     Regular file.
> TYPE = 8000                 a.out     Regular file.
> TYPE = 8000                    hi     Regular file.
> TYPE = 8000            filetype.c     Regular file.
> TYPE = 8000           hi.hardlink     Regular file.
> TYPE = 8000           filetype2.c     Regular file.
>
> hi.hardlink and hi.softlink are links to hi, but they are shown as Regular 
> file.
>
> regards
> Shiv
> _______________________________________________
> opensolaris-code mailing list
> [email protected]
> http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
>
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to