is_readable.cpp 825 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. IGL_INLINE bool igl::is_readable(const char* filename)
  18. {
  19. // Check if file already exists
  20. struct stat status;
  21. if(stat(filename,&status)!=0)
  22. {
  23. return false;
  24. }
  25. // Get current users uid and gid
  26. uid_t this_uid = getuid();
  27. gid_t this_gid = getgid();
  28. // Dealing with owner
  29. if( this_uid == status.st_uid )
  30. {
  31. return S_IRUSR & status.st_mode;
  32. }
  33. // Dealing with group member
  34. if( this_gid == status.st_gid )
  35. {
  36. return S_IRGRP & status.st_mode;
  37. }
  38. // Dealing with other
  39. return S_IROTH & status.st_mode;
  40. }
  41. #endif