xref: /netbsd-src/lib/libc/string/strmode.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: strmode.c,v 1.6 1997/10/16 23:09:09 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * 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	5.3 (Berkeley) 5/18/90";
40 #else
41 __RCSID("$NetBSD: strmode.c,v 1.6 1997/10/16 23:09:09 christos Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 
49 void
50 strmode(mode, p)
51 	register mode_t mode;
52 	register char *p;
53 {
54 	 /* print type */
55 	switch (mode & S_IFMT) {
56 	case S_IFDIR:			/* directory */
57 		*p++ = 'd';
58 		break;
59 	case S_IFCHR:			/* character special */
60 		*p++ = 'c';
61 		break;
62 	case S_IFBLK:			/* block special */
63 		*p++ = 'b';
64 		break;
65 	case S_IFREG:			/* regular */
66 		*p++ = '-';
67 		break;
68 	case S_IFLNK:			/* symbolic link */
69 		*p++ = 'l';
70 		break;
71 	case S_IFSOCK:			/* socket */
72 		*p++ = 's';
73 		break;
74 #ifdef S_IFIFO
75 	case S_IFIFO:			/* fifo */
76 		*p++ = 'p';
77 		break;
78 #endif
79 #ifdef S_IFWHT
80 	case S_IFWHT:			/* whiteout */
81 		*p++ = 'w';
82 		break;
83 #endif
84 	default:			/* unknown */
85 		*p++ = '?';
86 		break;
87 	}
88 	/* usr */
89 	if (mode & S_IRUSR)
90 		*p++ = 'r';
91 	else
92 		*p++ = '-';
93 	if (mode & S_IWUSR)
94 		*p++ = 'w';
95 	else
96 		*p++ = '-';
97 	switch (mode & (S_IXUSR | S_ISUID)) {
98 	case 0:
99 		*p++ = '-';
100 		break;
101 	case S_IXUSR:
102 		*p++ = 'x';
103 		break;
104 	case S_ISUID:
105 		*p++ = 'S';
106 		break;
107 	case S_IXUSR | S_ISUID:
108 		*p++ = 's';
109 		break;
110 	}
111 	/* group */
112 	if (mode & S_IRGRP)
113 		*p++ = 'r';
114 	else
115 		*p++ = '-';
116 	if (mode & S_IWGRP)
117 		*p++ = 'w';
118 	else
119 		*p++ = '-';
120 	switch (mode & (S_IXGRP | S_ISGID)) {
121 	case 0:
122 		*p++ = '-';
123 		break;
124 	case S_IXGRP:
125 		*p++ = 'x';
126 		break;
127 	case S_ISGID:
128 		*p++ = 'S';
129 		break;
130 	case S_IXGRP | S_ISGID:
131 		*p++ = 's';
132 		break;
133 	}
134 	/* other */
135 	if (mode & S_IROTH)
136 		*p++ = 'r';
137 	else
138 		*p++ = '-';
139 	if (mode & S_IWOTH)
140 		*p++ = 'w';
141 	else
142 		*p++ = '-';
143 	switch (mode & (S_IXOTH | S_ISVTX)) {
144 	case 0:
145 		*p++ = '-';
146 		break;
147 	case S_IXOTH:
148 		*p++ = 'x';
149 		break;
150 	case S_ISVTX:
151 		*p++ = 'T';
152 		break;
153 	case S_IXOTH | S_ISVTX:
154 		*p++ = 't';
155 		break;
156 	}
157 	*p++ = ' ';		/* will be a '+' if ACL's implemented */
158 	*p = '\0';
159 }
160