xref: /netbsd-src/lib/libc/string/strmode.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: strmode.c,v 1.11 1999/09/20 04:39:47 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.11 1999/09/20 04:39:47 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 
59 	 /* print type */
60 	switch (mode & S_IFMT) {
61 	case S_IFDIR:			/* directory */
62 		*p++ = 'd';
63 		break;
64 	case S_IFCHR:			/* character special */
65 		*p++ = 'c';
66 		break;
67 	case S_IFBLK:			/* block special */
68 		*p++ = 'b';
69 		break;
70 	case S_IFREG:			/* regular */
71 #ifdef S_ARCH2
72 		if ((mode & S_ARCH2) != 0) {
73 			*p++ = 'A';
74 		} else if ((mode & S_ARCH1) != 0) {
75 			*p++ = 'a';
76 		} else {
77 #endif
78 			*p++ = '-';
79 #ifdef S_ARCH2
80 		}
81 #endif
82 		break;
83 	case S_IFLNK:			/* symbolic link */
84 		*p++ = 'l';
85 		break;
86 	case S_IFSOCK:			/* socket */
87 		*p++ = 's';
88 		break;
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 	default:			/* unknown */
100 		*p++ = '?';
101 		break;
102 	}
103 	/* usr */
104 	if (mode & S_IRUSR)
105 		*p++ = 'r';
106 	else
107 		*p++ = '-';
108 	if (mode & S_IWUSR)
109 		*p++ = 'w';
110 	else
111 		*p++ = '-';
112 	switch (mode & (S_IXUSR | S_ISUID)) {
113 	case 0:
114 		*p++ = '-';
115 		break;
116 	case S_IXUSR:
117 		*p++ = 'x';
118 		break;
119 	case S_ISUID:
120 		*p++ = 'S';
121 		break;
122 	case S_IXUSR | S_ISUID:
123 		*p++ = 's';
124 		break;
125 	}
126 	/* group */
127 	if (mode & S_IRGRP)
128 		*p++ = 'r';
129 	else
130 		*p++ = '-';
131 	if (mode & S_IWGRP)
132 		*p++ = 'w';
133 	else
134 		*p++ = '-';
135 	switch (mode & (S_IXGRP | S_ISGID)) {
136 	case 0:
137 		*p++ = '-';
138 		break;
139 	case S_IXGRP:
140 		*p++ = 'x';
141 		break;
142 	case S_ISGID:
143 		*p++ = 'S';
144 		break;
145 	case S_IXGRP | S_ISGID:
146 		*p++ = 's';
147 		break;
148 	}
149 	/* other */
150 	if (mode & S_IROTH)
151 		*p++ = 'r';
152 	else
153 		*p++ = '-';
154 	if (mode & S_IWOTH)
155 		*p++ = 'w';
156 	else
157 		*p++ = '-';
158 	switch (mode & (S_IXOTH | S_ISVTX)) {
159 	case 0:
160 		*p++ = '-';
161 		break;
162 	case S_IXOTH:
163 		*p++ = 'x';
164 		break;
165 	case S_ISVTX:
166 		*p++ = 'T';
167 		break;
168 	case S_IXOTH | S_ISVTX:
169 		*p++ = 't';
170 		break;
171 	}
172 	*p++ = ' ';		/* will be a '+' if ACL's implemented */
173 	*p = '\0';
174 }
175