1010#include <errno.h>
1111#include <fcntl.h>
1212#include <stdio.h>
13+ #include <string.h>
1314#include <sys/stat.h>
1415#include <unistd.h>
1516
@@ -31,18 +32,10 @@ int main() {
3132 char * files [] = {"readable" , "writeable" ,
3233 "allaccess" , "forbidden" , "nonexistent" , "" };
3334 for (int i = 0 ; i < sizeof files / sizeof files [0 ]; i ++ ) {
34- printf ("F_OK(%s): %d\n" , files [i ], access (files [i ], F_OK ));
35- printf ("errno: %d\n" , errno );
36- errno = 0 ;
37- printf ("R_OK(%s): %d\n" , files [i ], access (files [i ], R_OK ));
38- printf ("errno: %d\n" , errno );
39- errno = 0 ;
40- printf ("X_OK(%s): %d\n" , files [i ], access (files [i ], X_OK ));
41- printf ("errno: %d\n" , errno );
42- errno = 0 ;
43- printf ("W_OK(%s): %d\n" , files [i ], access (files [i ], W_OK ));
44- printf ("errno: %d\n" , errno );
45- errno = 0 ;
35+ printf ("F_OK('%s'): %s\n" , files [i ], access (files [i ], F_OK ) < 0 ? strerror (errno ) : "OK" );
36+ printf ("R_OK('%s'): %s\n" , files [i ], access (files [i ], R_OK ) < 0 ? strerror (errno ) : "OK" );
37+ printf ("X_OK('%s'): %s\n" , files [i ], access (files [i ], X_OK ) < 0 ? strerror (errno ) : "OK" );
38+ printf ("W_OK('%s'): %s\n" , files [i ], access (files [i ], W_OK ) < 0 ? strerror (errno ) : "OK" );
4639 printf ("\n" );
4740 }
4841
@@ -51,25 +44,20 @@ int main() {
5144 int rename_ret = rename ("filetorename" , "renamedfile" );
5245 assert (rename_ret == 0 );
5346
54- errno = 0 ;
55- printf ("F_OK(%s): %d\n" , "filetorename" , access ("filetorename" , F_OK ));
56- printf ("errno: %d\n" , errno );
57- errno = 0 ;
58- printf ("F_OK(%s): %d\n" , "renamedfile" , access ("renamedfile" , F_OK ));
59- printf ("errno: %d\n" , errno );
47+ printf ("F_OK('%s'): %d\n" , "filetorename" , access ("filetorename" , F_OK ));
48+ printf ("F_OK('%s'): %d\n" , "renamedfile" , access ("renamedfile" , F_OK ));
6049
6150 // Same againt with faccessat
62- errno = 0 ;
63- printf ("F_OK(%s): %d\n" , "filetorename" , faccessat (AT_FDCWD , "filetorename" , F_OK , 0 ));
64- printf ("errno: %d\n" , errno );
65- errno = 0 ;
66- printf ("F_OK(%s): %d\n" , "renamedfile" , faccessat (AT_FDCWD , "renamedfile" , F_OK , 0 ));
67- printf ("errno: %d\n" , errno );
68-
69- chmod ("fchmodtest" , 0666 );
51+ printf ("F_OK('%s'): %d\n" , "filetorename" , faccessat (AT_FDCWD , "filetorename" , F_OK , 0 ));
52+ printf ("F_OK('%s'): %d\n" , "renamedfile" , faccessat (AT_FDCWD , "renamedfile" , F_OK , 0 ));
53+
54+ chmod ("fchmodtest" , S_IRUGO | S_IWUGO );
7055 struct stat fileStats ;
7156 stat ("fchmodtest" , & fileStats );
72- assert ((fileStats .st_mode & 0777 ) == 0666 );
57+ int mode = fileStats .st_mode & 0777 ;
58+ // Allow S_IXUGO in addtion to S_IWUGO because on windows
59+ // we always report the execute bit.
60+ assert (mode == (S_IRUGO | S_IWUGO ) || mode == (S_IRUGO | S_IWUGO | S_IXUGO ));
7361
7462 EM_ASM (
7563 var fchmodstream = FS .open ("fchmodtest" , "r" );
@@ -92,7 +80,8 @@ int main() {
9280 assert ((symlinkStats .st_mode & 0777 ) == 0777 );
9381
9482 stat ("writeable" , & fileStats );
95- assert ((fileStats .st_mode & 0777 ) == 0222 );
83+ mode = fileStats .st_mode & 0777 ;
84+ assert (mode == S_IWUGO || mode == (S_IWUGO | S_IXUGO ));
9685#endif
9786
9887 EM_ASM (
0 commit comments