is_readable.cpp 847 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "is_readable.h"
  2. #ifdef _WIN32
  3. # include <cstdio>
  4. IGL_INLINE bool igl::is_readable(const char* filename)
  5. {
  6. FILE * f = fopen(filename,"r");
  7. if(f == NULL)
  8. {
  9. return false;
  10. }
  11. fclose(f);
  12. return true;
  13. }
  14. #else
  15. # include <sys/stat.h>
  16. # include <unistd.h>
  17. # include <iostream>
  18. IGL_INLINE bool igl::is_readable(const char* filename)
  19. {
  20. // Check if file already exists
  21. struct stat status;
  22. if(stat(filename,&status)!=0)
  23. {
  24. return false;
  25. }
  26. // Get current users uid and gid
  27. uid_t this_uid = getuid();
  28. gid_t this_gid = getgid();
  29. // Dealing with owner
  30. if( this_uid == status.st_uid )
  31. {
  32. return S_IRUSR & status.st_mode;
  33. }
  34. // Dealing with group member
  35. if( this_gid == status.st_gid )
  36. {
  37. return S_IRGRP & status.st_mode;
  38. }
  39. // Dealing with other
  40. return S_IROTH & status.st_mode;
  41. }
  42. #endif