2331
|
1 /* filemode.c -- make a string describing file modes |
|
2 Copyright (C) 1985, 1990, 1993 Free Software Foundation, Inc. |
|
3 |
|
4 This program is free software; you can redistribute it and/or modify |
|
5 it under the terms of the GNU General Public License as published by |
|
6 the Free Software Foundation; either version 2, or (at your option) |
|
7 any later version. |
|
8 |
|
9 This program is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 GNU General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU General Public License |
|
15 along with this program; if not, write to the Free Software |
5307
|
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
17 02110-1301 USA. */ |
2331
|
18 |
|
19 #ifdef HAVE_CONFIG_H |
|
20 #include <config.h> |
|
21 #endif |
|
22 |
2443
|
23 #ifdef HAVE_SYS_TYPES_H |
2331
|
24 #include <sys/types.h> |
2443
|
25 #endif |
2331
|
26 #include <sys/stat.h> |
|
27 |
|
28 #if !S_IRUSR |
|
29 # if S_IREAD |
|
30 # define S_IRUSR S_IREAD |
|
31 # else |
|
32 # define S_IRUSR 00400 |
|
33 # endif |
|
34 #endif |
|
35 |
|
36 #if !S_IWUSR |
|
37 # if S_IWRITE |
|
38 # define S_IWUSR S_IWRITE |
|
39 # else |
|
40 # define S_IWUSR 00200 |
|
41 # endif |
|
42 #endif |
|
43 |
|
44 #if !S_IXUSR |
|
45 # if S_IEXEC |
|
46 # define S_IXUSR S_IEXEC |
|
47 # else |
|
48 # define S_IXUSR 00100 |
|
49 # endif |
|
50 #endif |
|
51 |
|
52 #ifdef STAT_MACROS_BROKEN |
|
53 #undef S_ISBLK |
|
54 #undef S_ISCHR |
|
55 #undef S_ISDIR |
|
56 #undef S_ISFIFO |
|
57 #undef S_ISLNK |
|
58 #undef S_ISMPB |
|
59 #undef S_ISMPC |
|
60 #undef S_ISNWK |
|
61 #undef S_ISREG |
|
62 #undef S_ISSOCK |
|
63 #endif /* STAT_MACROS_BROKEN. */ |
|
64 |
|
65 #if !defined(S_ISBLK) && defined(S_IFBLK) |
|
66 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) |
|
67 #endif |
|
68 #if !defined(S_ISCHR) && defined(S_IFCHR) |
|
69 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) |
|
70 #endif |
|
71 #if !defined(S_ISDIR) && defined(S_IFDIR) |
|
72 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
|
73 #endif |
|
74 #if !defined(S_ISREG) && defined(S_IFREG) |
|
75 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
|
76 #endif |
|
77 #if !defined(S_ISFIFO) && defined(S_IFIFO) |
|
78 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) |
|
79 #endif |
|
80 #if !defined(S_ISLNK) && defined(S_IFLNK) |
|
81 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) |
|
82 #endif |
|
83 #if !defined(S_ISSOCK) && defined(S_IFSOCK) |
|
84 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) |
|
85 #endif |
|
86 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */ |
|
87 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB) |
|
88 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC) |
|
89 #endif |
|
90 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */ |
|
91 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK) |
|
92 #endif |
|
93 |
|
94 void mode_string (); |
|
95 static char ftypelet (); |
|
96 static void rwx (); |
|
97 static void setst (); |
|
98 |
|
99 /* filemodestring - fill in string STR with an ls-style ASCII |
|
100 representation of the st_mode field of file stats block STATP. |
|
101 10 characters are stored in STR; no terminating null is added. |
|
102 The characters stored in STR are: |
|
103 |
|
104 0 File type. 'd' for directory, 'c' for character |
|
105 special, 'b' for block special, 'm' for multiplex, |
|
106 'l' for symbolic link, 's' for socket, 'p' for fifo, |
|
107 '-' for regular, '?' for any other file type |
|
108 |
|
109 1 'r' if the owner may read, '-' otherwise. |
|
110 |
|
111 2 'w' if the owner may write, '-' otherwise. |
|
112 |
|
113 3 'x' if the owner may execute, 's' if the file is |
|
114 set-user-id, '-' otherwise. |
|
115 'S' if the file is set-user-id, but the execute |
|
116 bit isn't set. |
|
117 |
|
118 4 'r' if group members may read, '-' otherwise. |
|
119 |
|
120 5 'w' if group members may write, '-' otherwise. |
|
121 |
|
122 6 'x' if group members may execute, 's' if the file is |
|
123 set-group-id, '-' otherwise. |
|
124 'S' if it is set-group-id but not executable. |
|
125 |
|
126 7 'r' if any user may read, '-' otherwise. |
|
127 |
|
128 8 'w' if any user may write, '-' otherwise. |
|
129 |
|
130 9 'x' if any user may execute, 't' if the file is "sticky" |
|
131 (will be retained in swap space after execution), '-' |
|
132 otherwise. |
|
133 'T' if the file is sticky but not executable. */ |
|
134 |
|
135 void |
|
136 filemodestring (statp, str) |
|
137 struct stat *statp; |
|
138 char *str; |
|
139 { |
|
140 mode_string (statp->st_mode, str); |
|
141 } |
|
142 |
|
143 /* Like filemodestring, but only the relevant part of the `struct stat' |
|
144 is given as an argument. */ |
|
145 |
|
146 void |
|
147 mode_string (mode, str) |
|
148 unsigned short mode; |
|
149 char *str; |
|
150 { |
|
151 str[0] = ftypelet ((long) mode); |
|
152 rwx ((mode & 0700) << 0, &str[1]); |
|
153 rwx ((mode & 0070) << 3, &str[4]); |
|
154 rwx ((mode & 0007) << 6, &str[7]); |
|
155 setst (mode, str); |
|
156 } |
|
157 |
|
158 /* Return a character indicating the type of file described by |
|
159 file mode BITS: |
|
160 'd' for directories |
|
161 'b' for block special files |
|
162 'c' for character special files |
|
163 'm' for multiplexor files |
|
164 'l' for symbolic links |
|
165 's' for sockets |
|
166 'p' for fifos |
|
167 '-' for regular files |
|
168 '?' for any other file type. */ |
|
169 |
|
170 static char |
|
171 ftypelet (bits) |
|
172 long bits; |
|
173 { |
|
174 #ifdef S_ISBLK |
|
175 if (S_ISBLK (bits)) |
|
176 return 'b'; |
|
177 #endif |
|
178 if (S_ISCHR (bits)) |
|
179 return 'c'; |
|
180 if (S_ISDIR (bits)) |
|
181 return 'd'; |
|
182 if (S_ISREG (bits)) |
|
183 return '-'; |
|
184 #ifdef S_ISFIFO |
|
185 if (S_ISFIFO (bits)) |
|
186 return 'p'; |
|
187 #endif |
|
188 #ifdef S_ISLNK |
|
189 if (S_ISLNK (bits)) |
|
190 return 'l'; |
|
191 #endif |
|
192 #ifdef S_ISSOCK |
|
193 if (S_ISSOCK (bits)) |
|
194 return 's'; |
|
195 #endif |
|
196 #ifdef S_ISMPC |
|
197 if (S_ISMPC (bits)) |
|
198 return 'm'; |
|
199 #endif |
|
200 #ifdef S_ISNWK |
|
201 if (S_ISNWK (bits)) |
|
202 return 'n'; |
|
203 #endif |
|
204 return '?'; |
|
205 } |
|
206 |
|
207 /* Look at read, write, and execute bits in BITS and set |
|
208 flags in CHARS accordingly. */ |
|
209 |
|
210 static void |
|
211 rwx (bits, chars) |
|
212 unsigned short bits; |
|
213 char *chars; |
|
214 { |
|
215 chars[0] = (bits & S_IRUSR) ? 'r' : '-'; |
|
216 chars[1] = (bits & S_IWUSR) ? 'w' : '-'; |
|
217 chars[2] = (bits & S_IXUSR) ? 'x' : '-'; |
|
218 } |
|
219 |
|
220 /* Set the 's' and 't' flags in file attributes string CHARS, |
|
221 according to the file mode BITS. */ |
|
222 |
|
223 static void |
|
224 setst (bits, chars) |
|
225 unsigned short bits; |
|
226 char *chars; |
|
227 { |
|
228 #ifdef S_ISUID |
|
229 if (bits & S_ISUID) |
|
230 { |
|
231 if (chars[3] != 'x') |
|
232 /* Set-uid, but not executable by owner. */ |
|
233 chars[3] = 'S'; |
|
234 else |
|
235 chars[3] = 's'; |
|
236 } |
|
237 #endif |
|
238 #ifdef S_ISGID |
|
239 if (bits & S_ISGID) |
|
240 { |
|
241 if (chars[6] != 'x') |
|
242 /* Set-gid, but not executable by group. */ |
|
243 chars[6] = 'S'; |
|
244 else |
|
245 chars[6] = 's'; |
|
246 } |
|
247 #endif |
|
248 #ifdef S_ISVTX |
|
249 if (bits & S_ISVTX) |
|
250 { |
|
251 if (chars[9] != 'x') |
|
252 /* Sticky, but not executable by others. */ |
|
253 chars[9] = 'T'; |
|
254 else |
|
255 chars[9] = 't'; |
|
256 } |
|
257 #endif |
|
258 } |