logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

symbolize_mode.c (1470B)


  1. // Collection of Unix tools, comparable to coreutils
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #define _XOPEN_SOURCE 700 // S_ISVTX, S_IFMT, S_IFDIR, …
  6. #include "bitmasks.h"
  7. #include "mode.h"
  8. #include <sys/stat.h>
  9. void
  10. symbolize_mode(mode_t mode, char str[11])
  11. {
  12. switch(mode & S_IFMT)
  13. {
  14. case S_IFDIR:
  15. str[0] = 'd';
  16. break;
  17. case S_IFCHR:
  18. str[0] = 'c';
  19. break;
  20. case S_IFBLK:
  21. str[0] = 'b';
  22. break;
  23. case S_IFREG:
  24. str[0] = '-';
  25. break;
  26. case S_IFIFO:
  27. str[0] = 'f';
  28. break;
  29. case S_IFLNK:
  30. str[0] = 'l';
  31. break;
  32. case S_IFSOCK:
  33. str[0] = 's';
  34. break;
  35. default:
  36. str[0] = '?';
  37. break;
  38. }
  39. str[1] = FIELD_MATCH(mode, S_IRUSR) ? 'r' : '-';
  40. str[2] = FIELD_MATCH(mode, S_IWUSR) ? 'w' : '-';
  41. if(FIELD_MATCH(mode, S_ISUID))
  42. str[3] = FIELD_MATCH(mode, S_IXUSR) ? 's' : 'S';
  43. else
  44. str[3] = FIELD_MATCH(mode, S_IXUSR) ? 'x' : '-';
  45. str[4] = FIELD_MATCH(mode, S_IRGRP) ? 'r' : '-';
  46. str[5] = FIELD_MATCH(mode, S_IWGRP) ? 'w' : '-';
  47. if(FIELD_MATCH(mode, S_ISGID))
  48. str[6] = FIELD_MATCH(mode, S_IXGRP) ? 's' : 'S';
  49. else
  50. str[6] = FIELD_MATCH(mode, S_IXGRP) ? 'x' : '-';
  51. str[7] = FIELD_MATCH(mode, S_IROTH) ? 'r' : '-';
  52. str[8] = FIELD_MATCH(mode, S_IWOTH) ? 'w' : '-';
  53. if(FIELD_MATCH(mode, S_ISVTX))
  54. str[9] = FIELD_MATCH(mode, S_IXOTH) ? 't' : 'T';
  55. else
  56. str[9] = FIELD_MATCH(mode, S_IXOTH) ? 'x' : '-';
  57. str[10] = '\0';
  58. }