1 #include "lib.h" 2 #include <string.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 #include <unistd.h> 6 #include <errno.h> 7 #include "sys9.h" 8 #include "dir.h" 9 10 int 11 access(const char *name, int mode) 12 { 13 int fd, n; 14 char db[DIRLEN]; 15 struct stat st; 16 static char omode[] = { 17 0, 18 3, 19 1, 20 2, 21 0, 22 2, 23 2, 24 2 25 }; 26 char tname[1024]; 27 28 if(mode == 0){ 29 if(_STAT(name, db) >= 0) 30 return 0; 31 else { 32 _syserrno(); 33 return -1; 34 } 35 } 36 fd = open(name, omode[mode&7]); 37 if(fd >= 0){ 38 close(fd); 39 return 0; 40 } 41 else if(stat(name, &st)==0 && S_ISDIR(st.st_mode)){ 42 if(mode & (R_OK|X_OK)){ 43 fd = open(name, O_RDONLY); 44 if(fd < 0) 45 return -1; 46 close(fd); 47 } 48 if(mode & W_OK){ 49 strncpy(tname, name, sizeof(tname)-9); 50 strcat(tname, "/_AcChAcK"); 51 fd = creat(tname, 0666); 52 if(fd < 0) 53 return -1; 54 close(fd); 55 _REMOVE(tname); 56 } 57 return 0; 58 } 59 return -1; 60 } 61