Q

_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?

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

Dig deeper on Enterprise Infrastructure Management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close