xref: /openbsd-src/lib/libc/string/strmode.c (revision 298116df5b000b61a69743d21c92035418df8900)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *rcsid = "$OpenBSD: strmode.c,v 1.6 2005/06/15 17:48:52 millert Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <string.h>
37 
38 /* XXX mode should be mode_t */
39 
40 void
41 strmode(int mode, char *p)
42 {
43 	 /* print type */
44 	switch (mode & S_IFMT) {
45 	case S_IFDIR:			/* directory */
46 		*p++ = 'd';
47 		break;
48 	case S_IFCHR:			/* character special */
49 		*p++ = 'c';
50 		break;
51 	case S_IFBLK:			/* block special */
52 		*p++ = 'b';
53 		break;
54 	case S_IFREG:			/* regular */
55 		*p++ = '-';
56 		break;
57 	case S_IFLNK:			/* symbolic link */
58 		*p++ = 'l';
59 		break;
60 	case S_IFSOCK:			/* socket */
61 		*p++ = 's';
62 		break;
63 #ifdef S_IFIFO
64 	case S_IFIFO:			/* fifo */
65 		*p++ = 'p';
66 		break;
67 #endif
68 	default:			/* unknown */
69 		*p++ = '?';
70 		break;
71 	}
72 	/* usr */
73 	if (mode & S_IRUSR)
74 		*p++ = 'r';
75 	else
76 		*p++ = '-';
77 	if (mode & S_IWUSR)
78 		*p++ = 'w';
79 	else
80 		*p++ = '-';
81 	switch (mode & (S_IXUSR | S_ISUID)) {
82 	case 0:
83 		*p++ = '-';
84 		break;
85 	case S_IXUSR:
86 		*p++ = 'x';
87 		break;
88 	case S_ISUID:
89 		*p++ = 'S';
90 		break;
91 	case S_IXUSR | S_ISUID:
92 		*p++ = 's';
93 		break;
94 	}
95 	/* group */
96 	if (mode & S_IRGRP)
97 		*p++ = 'r';
98 	else
99 		*p++ = '-';
100 	if (mode & S_IWGRP)
101 		*p++ = 'w';
102 	else
103 		*p++ = '-';
104 	switch (mode & (S_IXGRP | S_ISGID)) {
105 	case 0:
106 		*p++ = '-';
107 		break;
108 	case S_IXGRP:
109 		*p++ = 'x';
110 		break;
111 	case S_ISGID:
112 		*p++ = 'S';
113 		break;
114 	case S_IXGRP | S_ISGID:
115 		*p++ = 's';
116 		break;
117 	}
118 	/* other */
119 	if (mode & S_IROTH)
120 		*p++ = 'r';
121 	else
122 		*p++ = '-';
123 	if (mode & S_IWOTH)
124 		*p++ = 'w';
125 	else
126 		*p++ = '-';
127 	switch (mode & (S_IXOTH | S_ISVTX)) {
128 	case 0:
129 		*p++ = '-';
130 		break;
131 	case S_IXOTH:
132 		*p++ = 'x';
133 		break;
134 	case S_ISVTX:
135 		*p++ = 'T';
136 		break;
137 	case S_IXOTH | S_ISVTX:
138 		*p++ = 't';
139 		break;
140 	}
141 	*p++ = ' ';		/* will be a '+' if ACL's implemented */
142 	*p = '\0';
143 }
144