fork download
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4.  
  5. char const * sperm(__mode_t mode) {
  6. static char local_buff[16] = {0};
  7. int i = 0;
  8. // user permissions
  9. if ((mode & S_IRUSR) == S_IRUSR) local_buff[i] = 'r';
  10. else local_buff[i] = '-';
  11. i++;
  12. if ((mode & S_IWUSR) == S_IWUSR) local_buff[i] = 'w';
  13. else local_buff[i] = '-';
  14. i++;
  15. if ((mode & S_IXUSR) == S_IXUSR) local_buff[i] = 'x';
  16. else local_buff[i] = '-';
  17. i++;
  18. // group permissions
  19. if ((mode & S_IRGRP) == S_IRGRP) local_buff[i] = 'r';
  20. else local_buff[i] = '-';
  21. i++;
  22. if ((mode & S_IWGRP) == S_IWGRP) local_buff[i] = 'w';
  23. else local_buff[i] = '-';
  24. i++;
  25. if ((mode & S_IXGRP) == S_IXGRP) local_buff[i] = 'x';
  26. else local_buff[i] = '-';
  27. i++;
  28. // other permissions
  29. if ((mode & S_IROTH) == S_IROTH) local_buff[i] = 'r';
  30. else local_buff[i] = '-';
  31. i++;
  32. if ((mode & S_IWOTH) == S_IWOTH) local_buff[i] = 'w';
  33. else local_buff[i] = '-';
  34. i++;
  35. if ((mode & S_IXOTH) == S_IXOTH) local_buff[i] = 'x';
  36. else local_buff[i] = '-';
  37. i++;
  38. return local_buff;
  39. }
  40. int main(int argc, char ** argv)
  41. {
  42. std::cout<<sperm(S_IRUSR | S_IXUSR | S_IWGRP | S_IROTH)<<std::endl;
  43. std::cout<<sperm(S_IRUSR)<<std::endl;
  44. std::cout<<sperm(S_IRUSR | S_IRGRP | S_IWOTH | S_IROTH)<<std::endl;
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
r-x-w-r--
r--------
r--r--rw-