access.c 904 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <stdlib.h>
  8. #include "sys9.h"
  9. #include "dir.h"
  10. int
  11. access(const char *name, int mode)
  12. {
  13. int fd, n;
  14. Dir *db;
  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. if(mode == 0){
  28. db = _dirstat(name);
  29. if(db == nil){
  30. _syserrno();
  31. return -1;
  32. }
  33. free(db);
  34. return 0;
  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. }