xref: /freebsd/lib/libc/string/strmode.c (revision 96950419f15510287080c557174e0d8409f06956)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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/types.h>
33 #include <sys/stat.h>
34 #include <string.h>
35 
36 void
37 strmode(mode_t mode, char *p)
38 {
39 	 /* print type */
40 	switch (mode & S_IFMT) {
41 	case S_IFDIR:			/* directory */
42 		*p++ = 'd';
43 		break;
44 	case S_IFCHR:			/* character special */
45 		*p++ = 'c';
46 		break;
47 	case S_IFBLK:			/* block special */
48 		*p++ = 'b';
49 		break;
50 	case S_IFREG:			/* regular */
51 		*p++ = '-';
52 		break;
53 	case S_IFLNK:			/* symbolic link */
54 		*p++ = 'l';
55 		break;
56 	case S_IFSOCK:			/* socket */
57 		*p++ = 's';
58 		break;
59 #ifdef S_IFIFO
60 	case S_IFIFO:			/* fifo */
61 		*p++ = 'p';
62 		break;
63 #endif
64 #ifdef S_IFWHT
65 	case S_IFWHT:			/* whiteout */
66 		*p++ = 'w';
67 		break;
68 #endif
69 	default:			/* unknown */
70 		*p++ = '?';
71 		break;
72 	}
73 	/* usr */
74 	if (mode & S_IRUSR)
75 		*p++ = 'r';
76 	else
77 		*p++ = '-';
78 	if (mode & S_IWUSR)
79 		*p++ = 'w';
80 	else
81 		*p++ = '-';
82 	switch (mode & (S_IXUSR | S_ISUID)) {
83 	case 0:
84 		*p++ = '-';
85 		break;
86 	case S_IXUSR:
87 		*p++ = 'x';
88 		break;
89 	case S_ISUID:
90 		*p++ = 'S';
91 		break;
92 	case S_IXUSR | S_ISUID:
93 		*p++ = 's';
94 		break;
95 	}
96 	/* group */
97 	if (mode & S_IRGRP)
98 		*p++ = 'r';
99 	else
100 		*p++ = '-';
101 	if (mode & S_IWGRP)
102 		*p++ = 'w';
103 	else
104 		*p++ = '-';
105 	switch (mode & (S_IXGRP | S_ISGID)) {
106 	case 0:
107 		*p++ = '-';
108 		break;
109 	case S_IXGRP:
110 		*p++ = 'x';
111 		break;
112 	case S_ISGID:
113 		*p++ = 'S';
114 		break;
115 	case S_IXGRP | S_ISGID:
116 		*p++ = 's';
117 		break;
118 	}
119 	/* other */
120 	if (mode & S_IROTH)
121 		*p++ = 'r';
122 	else
123 		*p++ = '-';
124 	if (mode & S_IWOTH)
125 		*p++ = 'w';
126 	else
127 		*p++ = '-';
128 	switch (mode & (S_IXOTH | S_ISVTX)) {
129 	case 0:
130 		*p++ = '-';
131 		break;
132 	case S_IXOTH:
133 		*p++ = 'x';
134 		break;
135 	case S_ISVTX:
136 		*p++ = 'T';
137 		break;
138 	case S_IXOTH | S_ISVTX:
139 		*p++ = 't';
140 		break;
141 	}
142 	*p++ = ' ';
143 	*p = '\0';
144 }
145