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