strmode.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*-
  2. * Copyright (c) 1990 The Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #if defined(LIBC_SCCS) && !defined(lint)
  34. static char *rcsid = "$OpenBSD: strmode.c,v 1.3 1997/06/13 13:57:20 deraadt Exp $";
  35. #endif /* LIBC_SCCS and not lint */
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <string.h>
  39. #include <libtar/compat.h>
  40. void
  41. strmode(mode, p)
  42. register mode_t mode;
  43. register char *p;
  44. {
  45. /* print type */
  46. switch (mode & S_IFMT) {
  47. case S_IFDIR: /* directory */
  48. *p++ = 'd';
  49. break;
  50. case S_IFCHR: /* character special */
  51. *p++ = 'c';
  52. break;
  53. case S_IFBLK: /* block special */
  54. *p++ = 'b';
  55. break;
  56. case S_IFREG: /* regular */
  57. *p++ = '-';
  58. break;
  59. #ifdef S_IFLNK
  60. case S_IFLNK: /* symbolic link */
  61. *p++ = 'l';
  62. break;
  63. #endif
  64. #ifdef S_IFSOCK
  65. case S_IFSOCK: /* socket */
  66. *p++ = 's';
  67. break;
  68. #endif
  69. #ifdef S_IFIFO
  70. case S_IFIFO: /* fifo */
  71. *p++ = 'p';
  72. break;
  73. #endif
  74. #ifdef S_IFWHT
  75. case S_IFWHT: /* whiteout */
  76. *p++ = 'w';
  77. break;
  78. #endif
  79. default: /* unknown */
  80. *p++ = '?';
  81. break;
  82. }
  83. /* usr */
  84. #ifdef S_IRUSR
  85. if (mode & S_IRUSR)
  86. *p++ = 'r';
  87. else
  88. #endif
  89. *p++ = '-';
  90. #ifdef S_IWUSR
  91. if (mode & S_IWUSR)
  92. *p++ = 'w';
  93. else
  94. #endif
  95. *p++ = '-';
  96. #ifndef _MSC_VER
  97. #ifdef S_ISUID
  98. switch (mode & (S_IXUSR | S_ISUID)) {
  99. #else
  100. switch (mode & (S_IXUSR)) {
  101. #endif
  102. case 0:
  103. *p++ = '-';
  104. break;
  105. case S_IXUSR:
  106. *p++ = 'x';
  107. break;
  108. #ifdef S_ISUID
  109. case S_ISUID:
  110. *p++ = 'S';
  111. break;
  112. case S_IXUSR | S_ISUID:
  113. *p++ = 's';
  114. break;
  115. #endif
  116. }
  117. #endif
  118. /* group */
  119. #ifdef S_IRGRP
  120. if (mode & S_IRGRP)
  121. *p++ = 'r';
  122. else
  123. #endif
  124. *p++ = '-';
  125. #ifdef S_IWGRP
  126. if (mode & S_IWGRP)
  127. *p++ = 'w';
  128. else
  129. #endif
  130. *p++ = '-';
  131. #if defined(S_IXGRP) && defined(S_ISGID)
  132. switch (mode & (S_IXGRP | S_ISGID)) {
  133. case 0:
  134. *p++ = '-';
  135. break;
  136. case S_IXGRP:
  137. *p++ = 'x';
  138. break;
  139. case S_ISGID:
  140. *p++ = 'S';
  141. break;
  142. case S_IXGRP | S_ISGID:
  143. *p++ = 's';
  144. break;
  145. }
  146. #else
  147. *p++ = '-';
  148. #endif
  149. #ifndef WIN32
  150. /* other */
  151. if (mode & S_IROTH)
  152. *p++ = 'r';
  153. else
  154. *p++ = '-';
  155. if (mode & S_IWOTH)
  156. *p++ = 'w';
  157. else
  158. *p++ = '-';
  159. switch (mode & (S_IXOTH | S_ISVTX)) {
  160. case 0:
  161. *p++ = '-';
  162. break;
  163. case S_IXOTH:
  164. *p++ = 'x';
  165. break;
  166. case S_ISVTX:
  167. *p++ = 'T';
  168. break;
  169. case S_IXOTH | S_ISVTX:
  170. *p++ = 't';
  171. break;
  172. }
  173. #else
  174. *p++ = '-';
  175. *p++ = '-';
  176. *p++ = '-';
  177. #endif
  178. *p++ = ' '; /* will be a '+' if ACL's implemented */
  179. *p = '\0';
  180. }