Where is __dirstream_t really defined?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Where is __dirstream_t really defined?

Miro Kropáček
Hi,

I'm quite new to MinGW so excuse me if this is a known thing. I tried to look it up on Google but with no luck. I'd like to use some members of the DIR structure in my code (e.g. file handle). It's defined as:

typedef struct __dirstream_t DIR;

in dirent.h but it seems that definition of __dirstream_t is missing. Do I need to install some additional package(s). Or what could be wrong here? I followed the guide for both MinGW and MSYS.

Thanks,
Miro

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Eli Zaretskii
> Date: Fri, 6 Feb 2015 14:53:24 +0100
> From: Miro Kropáček <[hidden email]>
>
> I'm quite new to MinGW so excuse me if this is a known thing. I tried to look
> it up on Google but with no luck. I'd like to use some members of the DIR
> structure in my code (e.g. file handle). It's defined as:
>
> typedef struct __dirstream_t DIR;
>
> in dirent.h but it seems that definition of __dirstream_t is missing. Do I need
> to install some additional package(s). Or what could be wrong here? I followed
> the guide for both MinGW and MSYS.

The definition of DIR is intentionally opaque, because applications
are not supposed to fiddle with it.  Its complete definition is in the
MinGW sources, not in any header file.

Can you tell why you need to access the members of DIR?


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Miro Kropáček
The definition of DIR is intentionally opaque, because applications
Ah, now I see what it's meant by that 'opaque', OK.

Can you tell why you need to access the members of DIR?
Well, it's not my project, it's someone else's. It's used like this:

#define DIRHANDLE dirp->dd_handle

  DIR *dirp;

  if ((dirp = opendir(dirname)) == NULL)
    return(-1);

  if (fstat(DIRHANDLE, &stb) < 0)
    return(-1);

Not saying it's a great practice, though. So I'd like to make it compilable somehow, it works for other Unix flavors, incl. Cygwin.


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Eli Zaretskii
> Date: Fri, 6 Feb 2015 19:08:39 +0100
> From: Miro Kropáček <[hidden email]>
>
>     Can you tell why you need to access the members of DIR?
>    
>
> Well, it's not my project, it's someone else's. It's used like this:
>
> #define DIRHANDLE dirp->dd_handle
>
> DIR *dirp;
>
> if ((dirp = opendir(dirname)) == NULL)
> return(-1);
>
> if (fstat(DIRHANDLE, &stb) < 0)
> return(-1);
>
> Not saying it's a great practice, though. So I'd like to make it compilable
> somehow, it works for other Unix flavors, incl. Cygwin.

That's unportable code.  Just stat the directory instead.


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Keith Marshall
In reply to this post by Miro Kropáček
On 06/02/15 18:08, Miro Kropáček wrote:

> Well, it's not my project, it's someone else's. It's used like this:
>
> #define DIRHANDLE dirp->dd_handle
>
>   DIR *dirp;
>
>   if ((dirp = opendir(dirname)) == NULL)
>     return(-1);
>
>   if (fstat(DIRHANDLE, &stb) < 0)
>     return(-1);
>
> Not saying it's a great practice, though. So I'd like to make it compilable
> somehow, it works for other Unix flavors, incl. Cygwin.

Really?  You amaze me.  Can you name any such Unix flavours?  (FWIW,
GNU/Linux certainly isn't among them).

I see two fatally flawed assumptions in the above code snippet:

1) It assumes that the opaque entity referenced by dirp represents a
   struct or a union, which includes a dd_handle member; there is no
   way in which you can be certain of that.

2) Even if (1) were true, it assumes that this dd_handle member is a
   descriptor reference to a file stream; on Windows, it is impossible
   to process a directory as a regular file stream, (notwithstanding
   the hoops cygwin may jump through, to make it appear otherwise).

Like Eli says, your code snippet is not portable; in fact, it is just
plain broken.

--
Regards,
Keith.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Miro Kropáček

On Sat, Feb 7, 2015 at 7:13 AM, Keith Marshall <[hidden email]> wrote:
> Not saying it's a great practice, though. So I'd like to make it compilable
> somehow, it works for other Unix flavors, incl. Cygwin.

Really?  You amaze me.  Can you name any such Unix flavours?  (FWIW,
GNU/Linux certainly isn't among them).
http://sourceforge.net/p/aranym/code/ci/master/tree/src/gui-sdl/file.cpp#l63 .. works on all Linuxes, FreeBSD, Solaris and Cygwin for good 10+ years. Via #ifdefs, but works.

Like Eli says, your code snippet is not portable; in fact, it is just
plain broken.
Guys, there's no need to show off, I'm not the author, I don't advocate for that code, I was just unaware of its nonportability, that's all. I'll try to patch it in a more portable way and I'm thankful you showed me the right way.


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Keith Marshall
On 07/02/15 10:05, Miro Kropáček wrote:
> http://sourceforge.net/p/aranym/code/ci/master/tree/src/gui-sdl/file.cpp#l63
> .. works on all Linuxes, FreeBSD, Solaris and Cygwin for good 10+ years.

The fragment you showed most definitely does NOT compile on Linux; with
the addition of a few necessary includes, and minimal infrastructure, on
LinuxMint Debian this entirely equivalent fragment fails:

  $ cat foo.c
  #include <dirent.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <unistd.h>

  #define DIRHANDLE dirp->dd_handle

  int main()
  {
    struct stat stb;
    DIR *dirp = opendir( "foodir" );
    if( (dirp == NULL) || (fstat( DIRHANDLE, &stb ) < 0) )
      return -1;
    return 0;
  }

  $ gcc -c -Wall foo.c
  foo.c: In function ‘main’:
  foo.c:6:23: error: dereferencing pointer to incomplete type
   #define DIRHANDLE dirp->dd_handle
                         ^
  foo.c:12:33: note: in expansion of macro ‘DIRHANDLE’
     if( (dirp == NULL) || (fstat( DIRHANDLE, &stb ) < 0) )

> Via #ifdefs, but works.

No doubt because the #ifdefs prevent the compiler from ever seeing the
broken code fragment; IOW, that code specifically, does NOT work.

> Guys, there's no need to show off...

No one is showing off; merely stating the fact that the code you've
shown is broken.  In pure Windows code, compiled by MinGW -- because
MSVC doesn't give you opendir() anyway -- the directory stream returned
by opendir() is NOT a file stream.  You cannot manipulate it using any
function which requires a file descriptor argument.  All you can be sure
of is that you can retrieve struct dirent references from it, by calling
readdir() to process it sequentially.  Other than that, the only
functions which can legitimately manipulate [*] it are telldir(),
seekdir(), rewinddir(), and closedir() -- anything else is sure to
result in failure.

[*] Strictly, this same set of functions are the only ones which should
be used on ANY platform, for this purpose; using anything else breaks
portability, and should be considered a programming error.

--
Regards,
Keith.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Miro Kropáček

On Sat, Feb 7, 2015 at 12:22 PM, Keith Marshall <[hidden email]> wrote:
#define DIRHANDLE dirp->dd_handle
Perhaps you overlooked this line? http://sourceforge.net/p/aranym/code/ci/master/tree/src/gui-sdl/file.cpp#l22 So it's using the 'fd' member variable. I assure you it does compile :)

All right, I'll see what can I do about that code, thank you.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Greg Jung
I've been modifying a routine that compiles in mingw, linux, and (lately not tested tho) MS visual studio.
For everything but glob(), mingw maps pretty good but there is no fnmatch, so win32/PatternMatch is used.

 The directory is opened as follows (c++):

   DString root = dirN;
    AppendIfNeeded( root, "/");

    DIR* dir = opendir( dirN.c_str());
 
    if( dir == NULL) return;

 then to scan the directory

   struct stat64    statStruct;
#ifdef _WIN32

    WCHAR tchrpat[MAX_PATH+1];
    WCHAR tchrentry[MAX_PATH+1];
    MultiByteToWideChar(CP_UTF8, 0,
                  (LPCSTR)pat.c_str(), -1,
                             tchrpat, MAX_PATH+1);
#endif

    for(;;)
      {
struct dirent* entry = readdir( dir);
if( entry == NULL)
 break;
DString entryStr( entry->d_name);
if( entryStr != "." && entryStr != "..")
 {
   DString testFile = root + entryStr;

   int actStat = lstat64( testFile.c_str(), &statStruct);

   if( S_ISDIR(statStruct.st_mode) == 0)

     { // only test non-dirs

this is the same, _WIN32 or no.  But for matching files you have to do the unicode dance:
#ifdef _WIN32
                MultiByteToWideChar(CP_UTF8, 0,
                                (LPCSTR)entryStr.c_str(), -1,
                                tchrentry, MAX_PATH+1);
                int match = 1 - PathMatchSpecW(tchrentry, tchrpat);
#else
   int match = fnmatch( pat.c_str(), entryStr.c_str(), fnFlags);
#endif

another drop-in replacement that increases compatibiliity:

#ifdef _WIN32
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH) 
// ref:http://sourceforge.net/p/mingw/patches/256/ Keith Marshall 2005-12-02
#endif

The header dance at the top is another story:

#ifndef _MSC_VER  // everything but MS visual studio
# include <unistd.h> 
#endif


#ifndef _WIN32
# include <fnmatch.h>
# include <glob.h> // glob in MinGW does not working..... why?
#else
# include <shlwapi.h>
#       include <windows.h>
#    if !defined(S_IFLNK)
# define S_IFLNK 0xA000
# define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK)
#    endif
#endif

#ifndef _MSC_VER
# include <dirent.h>

#else
# include <io.h>

# define access _access

# define R_OK    4       /* Test for read permission.  */
# define W_OK    2       /* Test for write permission.  */
//# define   X_OK    1       /* execute permission - unsupported in windows*/
# define F_OK    0       /* Test for existence.  */

# define PATH_MAX 255
# include <direct.h>


# if !defined(S_ISDIR)

# define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
# define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR)
# define S_ISREG(mode)    __S_ISTYPE((mode), S_IFREG)

#endif


#endif
#ifdef _MSC_VER

/*
    Implementation of POSIX directory browsing functions and types for Win32.

    Author:  Kevlin Henney ([hidden email], [hidden email])
    History: Created March 1997. Updated June 2003 and July 2012.
    Rights:  See end of file.
*/
  << Code for MSC_VER case opendir, readdir, etc. >>
#endif
/*
    Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved.

    Permission to use, copy, modify, and distribute this software and its
    documentation for any purpose is hereby granted without fee, provided
    that this copyright and permissions notice appear in all copies and
    derivatives.
    This software is supplied "as is" without express or implied warranty.
    But that said, if there are any problems please get in touch.
*/

in my case, I use fstat64, and stat64 but cygwin doesn't:

#ifdef __CYGWIN__
#define stat64 stat
#define lstat64 lstat
// for religious reasons, CYGWIN doesn't do lstat64
#endif
#ifdef _WIN32
//

#    define lstat64(x,y) stat64(x,y)

#endif



On Sat, Feb 7, 2015 at 4:14 AM, Miro Kropáček <[hidden email]> wrote:

On Sat, Feb 7, 2015 at 12:22 PM, Keith Marshall <[hidden email]> wrote:
#define DIRHANDLE dirp->dd_handle
Perhaps you overlooked this line? http://sourceforge.net/p/aranym/code/ci/master/tree/src/gui-sdl/file.cpp#l22 So it's using the 'fd' member variable. I assure you it does compile :)

All right, I'll see what can I do about that code, thank you.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Keith Marshall
On 07/02/15 20:04, Greg Jung wrote:
> For everything but glob(), mingw maps pretty good but there is no fnmatch,
> so win32/PatternMatch is used.

As of mingwrt-3.21, MinGW provides glob(), (and globfree()); they WJFFM.

--
Regards,
Keith.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Where is __dirstream_t really defined?

Greg Jung

Looking forward to its appearance in the compiler builds; I have the reccomended headers in /mingw/include
but the compiler goes for /mingw32/i686-blablabla/include and every time I've messed with that, it hasn't
worked out.  I tried copying just fnmatch.h, glob.h but then glob will need consistent <_mingw.h> and <stddef.h>.

On Sat, Feb 7, 2015 at 1:17 PM, Keith Marshall <[hidden email]> wrote:
On 07/02/15 20:04, Greg Jung wrote:
> For everything but glob(), mingw maps pretty good but there is no fnmatch,
> so win32/PatternMatch is used.

As of mingwrt-3.21, MinGW provides glob(), (and globfree()); they WJFFM.

--
Regards,
Keith.



------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe