What do you expect the gcc CRT to emulate?
Nothing. stat (via _stat64) is available from msvcrt.dll, as documented by Microsoft. For the return value, MS writes:
Each of these functions returns 0 if the file-status information is obtained. A return value of –1 indicates an error, in which case errno is set to ENOENT, indicating that the filename or path could not be found. A return value of EINVAL indicates an invalid parameter; errno is also set to EINVAL in this case.
So, let's have a look at errno:
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
int main( int argc, char **argv ) {
struct _stat buf1, buf2;
char *file = "\"\"";
printf("ENOENT=%d EINVAL=%d\n",ENOENT,EINVAL);
printf( "for %s; stat returned: %d\n", file, _stat( file, &buf1 )
+);
printf(
"gid: %d\natime:%I64x\nctime:%I64x\ndrive:%d\n"
"inode:%d\nmode:%x\nmtime:%I64x\nnlink:%d\nrdev:%d\n"
"size:%d\nuid:%d\nerrno:%d\n",
buf1.st_gid, buf1.st_atime, buf1.st_ctime, buf1.st_dev,
buf1.st_ino, buf1.st_mode, buf1.st_mtime, buf1.st_nlink, buf1.
+st_rdev,
buf1.st_size, buf1.st_uid, errno
);
printf( "\nFor %s; stat returned: %d\n", argv[1], _stat( argv[1],
+&buf2 ) );
printf(
"gid: %d\natime:%I64x\nctime:%I64x\ndrive:%d\n"
"inode:%d\nmode:%x\nmtime:%I64x\nnlink:%d\nrdev:%d\n"
"size:%d\nuid:%d\nerrno:%d\n",
buf2.st_gid, buf2.st_atime, buf2.st_ctime, buf2.st_dev,
buf2.st_ino, buf2.st_mode, buf2.st_mtime, buf2.st_nlink, buf2.
+st_rdev,
buf2.st_size, buf2.st_uid, errno
);
return 1;
}
Output:
X:\>979536.exe
ENOENT=2 EINVAL=22
for ""; stat returned: -1
gid: 0
atime:0
ctime:0
drive:0
inode:0
mode:0
mtime:0
nlink:0
rdev:0
size:0
uid:0
errno:2
For (null); stat returned: -1
gid: 0
atime:0
ctime:0
drive:0
inode:0
mode:0
mtime:0
nlink:0
rdev:0
size:0
uid:0
errno:22
X:\>979536.exe .
ENOENT=2 EINVAL=22
for ""; stat returned: -1
gid: 0
atime:0
ctime:0
drive:0
inode:0
mode:0
mtime:0
nlink:0
rdev:0
size:0
uid:0
errno:2
For .; stat returned: 0
gid: 0
atime:12ce97f0
ctime:12ce97f0
drive:23
inode:0
mode:41ff
mtime:12ce97f0
nlink:1
rdev:23
size:0
uid:0
errno:2
X:\>979536.exe 979536.exe
ENOENT=2 EINVAL=22
for ""; stat returned: -1
gid: 0
atime:0
ctime:0
drive:0
inode:0
mode:0
mtime:0
nlink:0
rdev:0
size:0
uid:0
errno:2
For 979536.exe; stat returned: 0
gid: 0
atime:4ff4bce0
ctime:4ff5ebed
drive:23
inode:0
mode:81ff
mtime:4ff5f368
nlink:1
rdev:23
size:121535
uid:0
errno:2
X:\>
I think this is what one should expect. The file "" does not exist, and NULL is an invalid parameter. For the current directory and the exe file, stat() returns 0, as expected, and errno is not touched at all, so it stays 2 from the previously failed stat().
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|