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