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