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