]> git.saurik.com Git - apple/libc.git/blob - string.subproj/strmode.c
Libc-186.tar.gz
[apple/libc.git] / string.subproj / strmode.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 */
54
55
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <string.h>
59
60 void
61 strmode(mode, p)
62 register mode_t mode;
63 register char *p;
64 {
65 /* print type */
66 switch (mode & S_IFMT) {
67 case S_IFDIR: /* directory */
68 *p++ = 'd';
69 break;
70 case S_IFCHR: /* character special */
71 *p++ = 'c';
72 break;
73 case S_IFBLK: /* block special */
74 *p++ = 'b';
75 break;
76 case S_IFREG: /* regular */
77 *p++ = '-';
78 break;
79 case S_IFLNK: /* symbolic link */
80 *p++ = 'l';
81 break;
82 case S_IFSOCK: /* socket */
83 *p++ = 's';
84 break;
85 #ifdef S_IFIFO
86 case S_IFIFO: /* fifo */
87 *p++ = 'p';
88 break;
89 #endif
90 #ifdef S_IFWHT
91 case S_IFWHT: /* whiteout */
92 *p++ = 'w';
93 break;
94 #endif
95 default: /* unknown */
96 *p++ = '?';
97 break;
98 }
99 /* usr */
100 if (mode & S_IRUSR)
101 *p++ = 'r';
102 else
103 *p++ = '-';
104 if (mode & S_IWUSR)
105 *p++ = 'w';
106 else
107 *p++ = '-';
108 switch (mode & (S_IXUSR | S_ISUID)) {
109 case 0:
110 *p++ = '-';
111 break;
112 case S_IXUSR:
113 *p++ = 'x';
114 break;
115 case S_ISUID:
116 *p++ = 'S';
117 break;
118 case S_IXUSR | S_ISUID:
119 *p++ = 's';
120 break;
121 }
122 /* group */
123 if (mode & S_IRGRP)
124 *p++ = 'r';
125 else
126 *p++ = '-';
127 if (mode & S_IWGRP)
128 *p++ = 'w';
129 else
130 *p++ = '-';
131 switch (mode & (S_IXGRP | S_ISGID)) {
132 case 0:
133 *p++ = '-';
134 break;
135 case S_IXGRP:
136 *p++ = 'x';
137 break;
138 case S_ISGID:
139 *p++ = 'S';
140 break;
141 case S_IXGRP | S_ISGID:
142 *p++ = 's';
143 break;
144 }
145 /* other */
146 if (mode & S_IROTH)
147 *p++ = 'r';
148 else
149 *p++ = '-';
150 if (mode & S_IWOTH)
151 *p++ = 'w';
152 else
153 *p++ = '-';
154 switch (mode & (S_IXOTH | S_ISVTX)) {
155 case 0:
156 *p++ = '-';
157 break;
158 case S_IXOTH:
159 *p++ = 'x';
160 break;
161 case S_ISVTX:
162 *p++ = 'T';
163 break;
164 case S_IXOTH | S_ISVTX:
165 *p++ = 't';
166 break;
167 }
168 *p++ = ' '; /* will be a '+' if ACL's implemented */
169 *p = '\0';
170 }