testing whether a file is a directory under mingw

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

testing whether a file is a directory under mingw

C. Meli
Hello all,
what is the preferred mingw way to test whether a file is a directory ?

Best regards and thanks in advance
CM

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

lrn-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30.10.2011 19:18, C. Meli wrote:
> Hello all, what is the preferred mingw way to test whether a file
> is a directory ?
>
> Best regards and thanks in advance CM
>
Well, one way would be to do:

DWORD error, attributes = 0;
SetLastError (0);
attributes = GetFileAttributesA (filename);
error = GetLastError ();
if (attributes == INVALID_FILE_ATTRIBUTES || error != NO_ERROR)
{
  /* handle the error */
}
else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
{
  /* It's a directory */
}
else
{
  /* It is not a directory */
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOrW4FAAoJEOs4Jb6SI2CwM8gH/2mdIIm82AjL42OkjhsbrgIE
9uN+m5Arj1JVhM7D72SMSNwex1nct7mYXCyUiOQkVSxtA1Qzk9YVKuIt0ZpsV+X2
5ZJRpd2KlZNChL0pEcjSV7zZubFMjaK+bLsUvC0gk5LDJUq8jPEzLq4IPW+dSf7r
hg9RCSkJaHLfikVUPGFH4DUAq4khk21rStqoGVlximr1W9RwsiUiC2VbH3WCU65e
EH82Rb/rNuaxbnvC0GchkFm8E9ic7srZ22q0h6ep2sWRzCLOmHyaIR2yD7VA0uDw
fgNHPi9vp7C1BwCWxz/CsMWIjkfiTruFeNdRH5Yepnv4B5rI0k2Uaqgx2Y/yh8M=
=OSEX
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

Earnie Boyd
LRN wrote:
> On 30.10.2011 19:18, C. Meli wrote:
>> Hello all, what is the preferred mingw way to test whether a
>> file is a directory ?

This isn't a question best phrased for an exact response.

>
>
> Well, one way would be to do:
>

Certainly, if you want to do it in a C/C++ program.  But if you want
to do it in an MSYS shell it would be a different construct.  What
does the OP really mean?

--
Earnie
-- http://www.for-my-kids.com

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

lrn-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 31.10.2011 2:49, Earnie wrote:
> LRN wrote:
>> On 30.10.2011 19:18, C. Meli wrote:
>>> Hello all, what is the preferred mingw way to test whether a
>>> file is a directory ?
>
> This isn't a question best phrased for an exact response.
>
True
>>
>>
>> Well, one way would be to do:
>>
>
> Certainly, if you want to do it in a C/C++ program.  But if you
> want to do it in an MSYS shell it would be a different construct.
True, it would be
$ ls -ld filename
and then parse the resulting string
> What does the OP really mean?
>
Does it matter? Now he's got answers for both possible questions.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOribxAAoJEOs4Jb6SI2Cw2U4H/R0NCMqaouLI4zrGQzHKFj/9
rkqI/o5RqAZrZ3/sfmv15Pfm25uMGmkk2wfazWl+8XMd3Tvs3lul9jBIrAfMec+9
xZk0ZoufhEVEvx8JEWGgI8smu7ENmU5RnpxgMoP2VldGVQ9hgH39rnp2EifsObNa
qYPB1LpQwFBGdU7/9H/MzOlU1gSJ5i4tYsbNvI2MTOJv2c7Ho60hIeI7bHfTu6Kj
jR9hr7k70Rkmr3nFqPV4DRLnRxGizmT8jmBRiviMFLXLGzyZDKM5QFTBINeS+Pqu
nE0XfU11kVXBJg05B5fKfa12ffAfrMlzGWTCVjdMCJ5hYvqEA0HmHDeVVjh1A/g=
=Xlsj
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

Tor Lillqvist
In reply to this post by lrn-2
> attributes = GetFileAttributesA (filename);

Note that this works only if "filename" is the file name expressed in
the "system codepage" encoding, though. File names in Windows are in
Unicode, and it is perfectly possible, and in some locales might even
be quite common, to have files and directories with names that can't
be expressed in the system codepage. (Example: Hebrew file names in an
Arabic localisation of Windows, Greek file names in a German
localisation of Windows.)

So in the remote chance that you plan to use code like this for some
functionality that somebody might find useful to prevent from working
properly on purpose, be prepared: Creating files with arbitrary
Unicode names isn't hard.

To work in general, you should handle file names in Unicode (typically
either UTF-16, which is the encoding that Windows itself and the
Microsoft C/C++ library use for their wide-character API, like
GetFileAttributesW() or _wfopen(), or UTF-8, which is what much open
source software uses, and then convert to/from UTF-16 for Windows and
C library wide-character API calls).

--tml

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

Keith Marshall
In reply to this post by lrn-2
On 31/10/11 04:41, LRN wrote:
>> Certainly, if you want to do it in a C/C++ program.  But if you
>> want to do it in an MSYS shell it would be a different construct.
>
> True, it would be
> $ ls -ld filename
> and then parse the resulting string

Huh?  If you're insanely masochistic, perhaps you would adopt some such
awkward and potentially error prone strategy.  The smart way is:

  if test -d name; then
    # processing for name representing a directory
  elif test -f name; then
    # processing for name representing a regular file
  else
    # handle the case where name represents neither
  fi

--
Regards,
Keith.

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

Keith Marshall
In reply to this post by lrn-2
On 30/10/11 15:32, LRN wrote:

> Well, one way would be to do:
>
> DWORD error, attributes = 0;
> SetLastError (0);
> attributes = GetFileAttributesA (filename);
> error = GetLastError ();
> if (attributes == INVALID_FILE_ATTRIBUTES || error != NO_ERROR)
> {
>   /* handle the error */
> }
> else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
> {
>   /* It's a directory */
> }
> else
> {
>   /* It is not a directory */
> }

Sure, if you don't care that this is all MS-Windows specific, so is a
potential vendor lock-in trap.

A more generic (portable) approach would call stat(), and test for the
S_IFDIR and S_IFREG attributes, (be sure *not* to define NO_OLDNAMES or
_NO_OLDNAMES), in the st_mode field of the returned structure.

Note that this may also be susceptible to Tor Lillqvist's code page
caveat; wrapping MS-Windows UTF-16LE specific API calls in conditional
#ifdef _WIN32 blocks, while uglier, may be ultimately more robust.

--
Regards,
Keith.

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

Earnie Boyd
In reply to this post by lrn-2
LRN wrote:

> On 31.10.2011 2:49, Earnie wrote:
>> Certainly, if you want to do it in a C/C++ program.  But if you
>> want to do it in an MSYS shell it would be a different
>> construct.
> True, it would be $ ls -ld filename and then parse the resulting
> string
>> What does the OP really mean?
>
> Does it matter? Now he's got answers for both possible questions.
>

There's still more possibilities.

if [ -d somepath ]; then
  ...
fi

But there could be even more depending on the scenario.

--
Earnie
-- http://www.for-my-kids.com

------------------------------------------------------------------------------
Get your Android app more play: Bring it to the BlackBerry PlayBook
in minutes. BlackBerry App World™ now supports Android™ Apps
for the BlackBerry® PlayBook™. Discover just how easy and simple
it is! http://p.sf.net/sfu/android-dev2dev
_______________________________________________
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: testing whether a file is a directory under mingw

C. Meli
In reply to this post by C. Meli
On 30 October 2011 23:49,  <[hidden email]> wrote:

>
> Message: 8
> Date: Sun, 30 Oct 2011 19:32:23 +0400
> From: LRN <[hidden email]>
> Subject: Re: [Mingw-users] testing whether a file is a directory under
>        mingw
> To: [hidden email]
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=UTF-8
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 30.10.2011 19:18, C. Meli wrote:
>> Hello all, what is the preferred mingw way to test whether a file
>> is a directory ?
>>
>> Best regards and thanks in advance CM
>>
> Well, one way would be to do:
>
> DWORD error, attributes = 0;
> SetLastError (0);
> attributes = GetFileAttributesA (filename);
> error = GetLastError ();
> if (attributes == INVALID_FILE_ATTRIBUTES || error != NO_ERROR)
> {
>  /* handle the error */
> }
> else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
> {
>  /* It's a directory */
> }
> else
> {
>  /* It is not a directory */
> }


Thank you very much, that was what I was looking for (how to do it in C++).
Sorry for the brevity of my post, but I was a bit in a rush.. :)
I tried using stat but that function didn't work under mingw. I am
using the TDM build (64 bit version).

I did find an alternative - calling opendir on filename +"\\" allows
me to determine this too successfully.

Best regards and thanks
CM

>>
>>
>> Well, one way would be to do:
>>
>
> Certainly, if you want to do it in a C/C++ program.  But if you want
> to do it in an MSYS shell it would be a different construct.  What
> does the OP really mean?
>
> --
> Earnie
> -- http://www.for-my-kids.com
>
>
>
> ------------------------------
>
> ------------------------------------------------------------------------------
> Get your Android app more play: Bring it to the BlackBerry PlayBook
> in minutes. BlackBerry App World&#153; now supports Android&#153; Apps
> for the BlackBerry&reg; PlayBook&#153;. Discover just how easy and simple
> it is! http://p.sf.net/sfu/android-dev2dev
>
>
> ------------------------------
>
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>
>
> End of MinGW-users Digest, Vol 65, Issue 24
> *******************************************
>

------------------------------------------------------------------------------
RSA&reg; Conference 2012
Save &#36;700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
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: testing whether a file is a directory under Mingw

C. Meli
In reply to this post by C. Meli
On 31 October 2011 23:49,  <[hidden email]> wrote:

> ------------------------------
>
> Message: 7
> Date: Mon, 31 Oct 2011 09:59:24 +0000
> From: Keith Marshall <[hidden email]>
> Subject: Re: [Mingw-users] testing whether a file is a directory under
>        mingw
> To: [hidden email]
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On 30/10/11 15:32, LRN wrote:
>> Well, one way would be to do:
>>
>> DWORD error, attributes = 0;
>> SetLastError (0);
>> attributes = GetFileAttributesA (filename);
>> error = GetLastError ();
>> if (attributes == INVALID_FILE_ATTRIBUTES || error != NO_ERROR)
>> {
>>   /* handle the error */
>> }
>> else if (attributes & FILE_ATTRIBUTE_DIRECTORY)
>> {
>>   /* It's a directory */
>> }
>> else
>> {
>>   /* It is not a directory */
>> }
>
> Sure, if you don't care that this is all MS-Windows specific, so is a
> potential vendor lock-in trap.
>
> A more generic (portable) approach would call stat(), and test for the
> S_IFDIR and S_IFREG attributes, (be sure *not* to define NO_OLDNAMES or
> _NO_OLDNAMES), in the st_mode field of the returned structure.
>
> Note that this may also be susceptible to Tor Lillqvist's code page
> caveat; wrapping MS-Windows UTF-16LE specific API calls in conditional
> #ifdef _WIN32 blocks, while uglier, may be ultimately more robust.

I did try calling stat(), but my calls to stat() all returned an
error. Caveat - I'm running the tdm 64-bit build which may be
why it did not work.
And in my code I prefer to a portable approach as you said except when
it doesn't work :(
In this case I have #ifdefs in place as a result..

Regards
CM

------------------------------------------------------------------------------
RSA&reg; Conference 2012
Save &#36;700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
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: testing whether a file is a directory under Mingw

Erwin Waterlander

> On 31 October 2011 23:49,  <[hidden email]>
>>
>> Sure, if you don't care that this is all MS-Windows specific, so is a
>> potential vendor lock-in trap.
>>
>> A more generic (portable) approach would call stat(), and test for the
>> S_IFDIR and S_IFREG attributes, (be sure *not* to define NO_OLDNAMES or
>> _NO_OLDNAMES), in the st_mode field of the returned structure.
>>
>> Note that this may also be susceptible to Tor Lillqvist's code page
>> caveat; wrapping MS-Windows UTF-16LE specific API calls in conditional
>> #ifdef _WIN32 blocks, while uglier, may be ultimately more robust.
>
> I did try calling stat(), but my calls to stat() all returned an
> error. Caveat - I'm running the tdm 64-bit build which may be
> why it did not work.
> And in my code I prefer to a portable approach as you said except when
> it doesn't work :(
> In this case I have #ifdefs in place as a result..
>
> Regards
> CM
>


You can copy my portable wcd_isdir() function. Works on
win32/win64/Unix/dos32/dos16/OS2/cygwin. With several ifdefs...

See wcddir.c in wcd 5.2.0-beta12
(http://waterlan.home.xs4all.nl/#WCD_BETA_ANCHOR)

regards,

--
Erwin Waterlander
http://waterlan.home.xs4all.nl/


------------------------------------------------------------------------------
RSA&#174; Conference 2012
Save $700 by Nov 18
Register now&#33;
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
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