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