xref: /netbsd-src/lib/libc/string/strmode.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: strmode.c,v 1.16 2004/06/20 22:20:15 jmc 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.16 2004/06/20 22:20:15 jmc 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 #ifdef S_IFSOCK
84 	case S_IFSOCK:			/* socket */
85 		*p++ = 's';
86 		break;
87 #endif
88 #ifdef S_IFIFO
89 	case S_IFIFO:			/* fifo */
90 		*p++ = 'p';
91 		break;
92 #endif
93 #ifdef S_IFWHT
94 	case S_IFWHT:			/* whiteout */
95 		*p++ = 'w';
96 		break;
97 #endif
98 #ifdef S_IFDOOR
99 	case S_IFDOOR:			/* door */
100 		*p++ = 'D';
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