xref: /netbsd-src/lib/libc/string/strmode.c (revision 075022b349321510ad14036d70908681ed02f117)
1 /*	$NetBSD: strmode.c,v 1.14 2003/07/25 03:21:18 atatat 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.14 2003/07/25 03:21:18 atatat Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44 
45 #include "namespace.h"
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 
49 #include <assert.h>
50 #include <unistd.h>
51 
52 void
53 strmode(mode, p)
54 	mode_t mode;
55 	char *p;
56 {
57 
58 	_DIAGASSERT(p != NULL);
59 
60 	 /* print type */
61 	switch (mode & S_IFMT) {
62 	case S_IFDIR:			/* directory */
63 		*p++ = 'd';
64 		break;
65 	case S_IFCHR:			/* character special */
66 		*p++ = 'c';
67 		break;
68 	case S_IFBLK:			/* block special */
69 		*p++ = 'b';
70 		break;
71 	case S_IFREG:			/* regular */
72 #ifdef S_ARCH2
73 		if ((mode & S_ARCH2) != 0) {
74 			*p++ = 'A';
75 		} else if ((mode & S_ARCH1) != 0) {
76 			*p++ = 'a';
77 		} else {
78 #endif
79 			*p++ = '-';
80 #ifdef S_ARCH2
81 		}
82 #endif
83 		break;
84 	case S_IFLNK:			/* symbolic link */
85 		*p++ = 'l';
86 		break;
87 	case S_IFSOCK:			/* socket */
88 		*p++ = 's';
89 		break;
90 #ifdef S_IFIFO
91 	case S_IFIFO:			/* fifo */
92 		*p++ = 'p';
93 		break;
94 #endif
95 #ifdef S_IFWHT
96 	case S_IFWHT:			/* whiteout */
97 		*p++ = 'w';
98 		break;
99 #endif
100 #ifdef S_IFDOOR
101 	case S_IFDOOR:			/* door */
102 		*p++ = 'D';
103 		break;
104 #endif
105 	default:			/* unknown */
106 		*p++ = '?';
107 		break;
108 	}
109 	/* usr */
110 	if (mode & S_IRUSR)
111 		*p++ = 'r';
112 	else
113 		*p++ = '-';
114 	if (mode & S_IWUSR)
115 		*p++ = 'w';
116 	else
117 		*p++ = '-';
118 	switch (mode & (S_IXUSR | S_ISUID)) {
119 	case 0:
120 		*p++ = '-';
121 		break;
122 	case S_IXUSR:
123 		*p++ = 'x';
124 		break;
125 	case S_ISUID:
126 		*p++ = 'S';
127 		break;
128 	case S_IXUSR | S_ISUID:
129 		*p++ = 's';
130 		break;
131 	}
132 	/* group */
133 	if (mode & S_IRGRP)
134 		*p++ = 'r';
135 	else
136 		*p++ = '-';
137 	if (mode & S_IWGRP)
138 		*p++ = 'w';
139 	else
140 		*p++ = '-';
141 	switch (mode & (S_IXGRP | S_ISGID)) {
142 	case 0:
143 		*p++ = '-';
144 		break;
145 	case S_IXGRP:
146 		*p++ = 'x';
147 		break;
148 	case S_ISGID:
149 		*p++ = 'S';
150 		break;
151 	case S_IXGRP | S_ISGID:
152 		*p++ = 's';
153 		break;
154 	}
155 	/* other */
156 	if (mode & S_IROTH)
157 		*p++ = 'r';
158 	else
159 		*p++ = '-';
160 	if (mode & S_IWOTH)
161 		*p++ = 'w';
162 	else
163 		*p++ = '-';
164 	switch (mode & (S_IXOTH | S_ISVTX)) {
165 	case 0:
166 		*p++ = '-';
167 		break;
168 	case S_IXOTH:
169 		*p++ = 'x';
170 		break;
171 	case S_ISVTX:
172 		*p++ = 'T';
173 		break;
174 	case S_IXOTH | S_ISVTX:
175 		*p++ = 't';
176 		break;
177 	}
178 	*p++ = ' ';		/* will be a '+' if ACL's implemented */
179 	*p = '\0';
180 }
181