is_readable.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "is_readable.h"
  9. #ifdef _WIN32
  10. # include <cstdio>
  11. IGL_INLINE bool igl::is_readable(const char* filename)
  12. {
  13. FILE * f = fopen(filename,"r");
  14. if(f == NULL)
  15. {
  16. return false;
  17. }
  18. fclose(f);
  19. return true;
  20. }
  21. #else
  22. # include <sys/stat.h>
  23. # include <unistd.h>
  24. # include <iostream>
  25. IGL_INLINE bool igl::is_readable(const char* filename)
  26. {
  27. // Check if file already exists
  28. struct stat status;
  29. if(stat(filename,&status)!=0)
  30. {
  31. return false;
  32. }
  33. // Get current users uid and gid
  34. uid_t this_uid = getuid();
  35. gid_t this_gid = getgid();
  36. // Dealing with owner
  37. if( this_uid == status.st_uid )
  38. {
  39. return S_IRUSR & status.st_mode;
  40. }
  41. // Dealing with group member
  42. if( this_gid == status.st_gid )
  43. {
  44. return S_IRGRP & status.st_mode;
  45. }
  46. // Dealing with other
  47. return S_IROTH & status.st_mode;
  48. }
  49. #endif