strmode.c 3.4 KB

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