access.c 421 B

123456789101112131415161718192021222324252627282930313233
  1. #include <u.h>
  2. #include <libc.h>
  3. int
  4. access(char *name, int mode)
  5. {
  6. int fd;
  7. Dir *db;
  8. static char omode[] = {
  9. 0,
  10. OEXEC,
  11. OWRITE,
  12. ORDWR,
  13. OREAD,
  14. OEXEC, /* only approximate */
  15. ORDWR,
  16. ORDWR /* only approximate */
  17. };
  18. if(mode == AEXIST){
  19. db = dirstat(name);
  20. free(db);
  21. if(db != nil)
  22. return 0;
  23. return -1;
  24. }
  25. fd = open(name, omode[mode&7]);
  26. if(fd >= 0){
  27. close(fd);
  28. return 0;
  29. }
  30. return -1;
  31. }