filemode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* filemode.c -- make a string describing file modes
  2. Copyright (C) 1985, 1990, 1993, 1998-2000, 2004, 2006, 2009-2011 Free
  3. Software Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "filemode.h"
  16. /* The following is for Cray DMF (Data Migration Facility), which is a
  17. HSM file system. A migrated file has a `st_dm_mode' that is
  18. different from the normal `st_mode', so any tests for migrated
  19. files should use the former. */
  20. #if HAVE_ST_DM_MODE
  21. # define IS_MIGRATED_FILE(statp) \
  22. (S_ISOFD (statp->st_dm_mode) || S_ISOFL (statp->st_dm_mode))
  23. #else
  24. # define IS_MIGRATED_FILE(statp) 0
  25. #endif
  26. #if ! HAVE_DECL_STRMODE
  27. /* Return a character indicating the type of file described by
  28. file mode BITS:
  29. '-' regular file
  30. 'b' block special file
  31. 'c' character special file
  32. 'C' high performance ("contiguous data") file
  33. 'd' directory
  34. 'D' door
  35. 'l' symbolic link
  36. 'm' multiplexed file (7th edition Unix; obsolete)
  37. 'n' network special file (HP-UX)
  38. 'p' fifo (named pipe)
  39. 'P' port
  40. 's' socket
  41. 'w' whiteout (4.4BSD)
  42. '?' some other file type */
  43. static char
  44. ftypelet (mode_t bits)
  45. {
  46. /* These are the most common, so test for them first. */
  47. if (S_ISREG (bits))
  48. return '-';
  49. if (S_ISDIR (bits))
  50. return 'd';
  51. /* Other letters standardized by POSIX 1003.1-2004. */
  52. if (S_ISBLK (bits))
  53. return 'b';
  54. if (S_ISCHR (bits))
  55. return 'c';
  56. if (S_ISLNK (bits))
  57. return 'l';
  58. if (S_ISFIFO (bits))
  59. return 'p';
  60. /* Other file types (though not letters) standardized by POSIX. */
  61. if (S_ISSOCK (bits))
  62. return 's';
  63. /* Nonstandard file types. */
  64. if (S_ISCTG (bits))
  65. return 'C';
  66. if (S_ISDOOR (bits))
  67. return 'D';
  68. if (S_ISMPB (bits) || S_ISMPC (bits))
  69. return 'm';
  70. if (S_ISNWK (bits))
  71. return 'n';
  72. if (S_ISPORT (bits))
  73. return 'P';
  74. if (S_ISWHT (bits))
  75. return 'w';
  76. return '?';
  77. }
  78. /* Like filemodestring, but rely only on MODE. */
  79. void
  80. strmode (mode_t mode, char *str)
  81. {
  82. str[0] = ftypelet (mode);
  83. str[1] = mode & S_IRUSR ? 'r' : '-';
  84. str[2] = mode & S_IWUSR ? 'w' : '-';
  85. str[3] = (mode & S_ISUID
  86. ? (mode & S_IXUSR ? 's' : 'S')
  87. : (mode & S_IXUSR ? 'x' : '-'));
  88. str[4] = mode & S_IRGRP ? 'r' : '-';
  89. str[5] = mode & S_IWGRP ? 'w' : '-';
  90. str[6] = (mode & S_ISGID
  91. ? (mode & S_IXGRP ? 's' : 'S')
  92. : (mode & S_IXGRP ? 'x' : '-'));
  93. str[7] = mode & S_IROTH ? 'r' : '-';
  94. str[8] = mode & S_IWOTH ? 'w' : '-';
  95. str[9] = (mode & S_ISVTX
  96. ? (mode & S_IXOTH ? 't' : 'T')
  97. : (mode & S_IXOTH ? 'x' : '-'));
  98. str[10] = ' ';
  99. str[11] = '\0';
  100. }
  101. #endif /* ! HAVE_DECL_STRMODE */
  102. /* filemodestring - fill in string STR with an ls-style ASCII
  103. representation of the st_mode field of file stats block STATP.
  104. 12 characters are stored in STR.
  105. The characters stored in STR are:
  106. 0 File type, as in ftypelet above, except that other letters are used
  107. for files whose type cannot be determined solely from st_mode:
  108. 'F' semaphore
  109. 'M' migrated file (Cray DMF)
  110. 'Q' message queue
  111. 'S' shared memory object
  112. 'T' typed memory object
  113. 1 'r' if the owner may read, '-' otherwise.
  114. 2 'w' if the owner may write, '-' otherwise.
  115. 3 'x' if the owner may execute, 's' if the file is
  116. set-user-id, '-' otherwise.
  117. 'S' if the file is set-user-id, but the execute
  118. bit isn't set.
  119. 4 'r' if group members may read, '-' otherwise.
  120. 5 'w' if group members may write, '-' otherwise.
  121. 6 'x' if group members may execute, 's' if the file is
  122. set-group-id, '-' otherwise.
  123. 'S' if it is set-group-id but not executable.
  124. 7 'r' if any user may read, '-' otherwise.
  125. 8 'w' if any user may write, '-' otherwise.
  126. 9 'x' if any user may execute, 't' if the file is "sticky"
  127. (will be retained in swap space after execution), '-'
  128. otherwise.
  129. 'T' if the file is sticky but not executable.
  130. 10 ' ' for compatibility with 4.4BSD strmode,
  131. since this interface does not support ACLs.
  132. 11 '\0'. */
  133. void
  134. filemodestring (struct stat const *statp, char *str)
  135. {
  136. strmode (statp->st_mode, str);
  137. if (S_TYPEISSEM (statp))
  138. str[0] = 'F';
  139. else if (IS_MIGRATED_FILE (statp))
  140. str[0] = 'M';
  141. else if (S_TYPEISMQ (statp))
  142. str[0] = 'Q';
  143. else if (S_TYPEISSHM (statp))
  144. str[0] = 'S';
  145. else if (S_TYPEISTMO (statp))
  146. str[0] = 'T';
  147. }