Ask the Expert

_Fstat returns 0100666 not executable

The C library, which is part of Microsoft Visual Studio 6.0, contains functions _stat() and _fstat() that are documented in MSDN. They appear to be stand-ins for stat() and fstat() respectively. The latter are apparently not documented in MSDN. I found that I can compile the same code either using the '_' prefix or not on these two functions and the corresponding variable struct stat buf. The issue is that when invoked against an executable file such as c:perlbinperl.exe, my machine (Win2k), _stat() returns the st_mode field (oct) = 0100777 (executable) while _fstat returns 0100666 (not executable)! Do you consider this a bug or do you have a rational explanation for it?

Requires Free Membership to View

I would not consider it a bug. This is an excellent question and I had to spelunk through quite a big of C Run Time Source Code to see what was going on. (You all always install the C Run Time Source Code when you install the Visual C++, do you not? If you are not, you need to stop and do it immediately!) The issue is that stat can take a file name, while fstat can only take a handle. If you look at the code for stat, you will see that it eventually calls a function called __tdtoxmode inside STAT.C. In __tdtoxmode, the check that determines if a file is an executable file is:

if (p = _tcsrchr(name, _T('.'))) {
            if ( !_tcsicmp(p, _T(".exe")) ||
                 !_tcsicmp(p, _T(".cmd")) ||
                 !_tcsicmp(p, _T(".bat")) ||
                 !_tcsicmp(p, _T(".com")) )
                uxmode |= _S_IEXEC;

By having the filename, stat can check it. The fstat function, on the other hand, only has the handle and there is no documented way of taking a handle value and turning it into the filename associated with the handle. The closest you can get is with GetFileType, but that just returns the type of pipe.

This was first published in July 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: