This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / binutils / filemode.c
1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 90, 91, 94, 95, 97, 1999 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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 02111-1307, USA. */
18 \f
19 #include "bfd.h"
20 #include "bucomm.h"
21
22 static char ftypelet PARAMS ((unsigned long));
23 static void setst PARAMS ((unsigned long, char *));
24
25 /* filemodestring - fill in string STR with an ls-style ASCII
26 representation of the st_mode field of file stats block STATP.
27 10 characters are stored in STR; no terminating null is added.
28 The characters stored in STR are:
29
30 0 File type. 'd' for directory, 'c' for character
31 special, 'b' for block special, 'm' for multiplex,
32 'l' for symbolic link, 's' for socket, 'p' for fifo,
33 '-' for any other file type
34
35 1 'r' if the owner may read, '-' otherwise.
36
37 2 'w' if the owner may write, '-' otherwise.
38
39 3 'x' if the owner may execute, 's' if the file is
40 set-user-id, '-' otherwise.
41 'S' if the file is set-user-id, but the execute
42 bit isn't set.
43
44 4 'r' if group members may read, '-' otherwise.
45
46 5 'w' if group members may write, '-' otherwise.
47
48 6 'x' if group members may execute, 's' if the file is
49 set-group-id, '-' otherwise.
50 'S' if it is set-group-id but not executable.
51
52 7 'r' if any user may read, '-' otherwise.
53
54 8 'w' if any user may write, '-' otherwise.
55
56 9 'x' if any user may execute, 't' if the file is "sticky"
57 (will be retained in swap space after execution), '-'
58 otherwise.
59 'T' if the file is sticky but not executable. */
60
61 #if 0
62
63 /* This is not used; only mode_string is used. */
64
65 void
66 filemodestring (statp, str)
67 struct stat *statp;
68 char *str;
69 {
70 mode_string ((unsigned long) statp->st_mode, str);
71 }
72
73 #endif
74
75 /* Get definitions for the file permission bits. */
76
77 #ifndef S_IRWXU
78 #define S_IRWXU 0700
79 #endif
80 #ifndef S_IRUSR
81 #define S_IRUSR 0400
82 #endif
83 #ifndef S_IWUSR
84 #define S_IWUSR 0200
85 #endif
86 #ifndef S_IXUSR
87 #define S_IXUSR 0100
88 #endif
89
90 #ifndef S_IRWXG
91 #define S_IRWXG 0070
92 #endif
93 #ifndef S_IRGRP
94 #define S_IRGRP 0040
95 #endif
96 #ifndef S_IWGRP
97 #define S_IWGRP 0020
98 #endif
99 #ifndef S_IXGRP
100 #define S_IXGRP 0010
101 #endif
102
103 #ifndef S_IRWXO
104 #define S_IRWXO 0007
105 #endif
106 #ifndef S_IROTH
107 #define S_IROTH 0004
108 #endif
109 #ifndef S_IWOTH
110 #define S_IWOTH 0002
111 #endif
112 #ifndef S_IXOTH
113 #define S_IXOTH 0001
114 #endif
115
116 /* Like filemodestring, but only the relevant part of the `struct stat'
117 is given as an argument. */
118
119 void
120 mode_string (mode, str)
121 unsigned long mode;
122 char *str;
123 {
124 str[0] = ftypelet ((unsigned long) mode);
125 str[1] = (mode & S_IRUSR) != 0 ? 'r' : '-';
126 str[2] = (mode & S_IWUSR) != 0 ? 'w' : '-';
127 str[3] = (mode & S_IXUSR) != 0 ? 'x' : '-';
128 str[4] = (mode & S_IRGRP) != 0 ? 'r' : '-';
129 str[5] = (mode & S_IWGRP) != 0 ? 'w' : '-';
130 str[6] = (mode & S_IXGRP) != 0 ? 'x' : '-';
131 str[7] = (mode & S_IROTH) != 0 ? 'r' : '-';
132 str[8] = (mode & S_IWOTH) != 0 ? 'w' : '-';
133 str[9] = (mode & S_IXOTH) != 0 ? 'x' : '-';
134 setst ((unsigned long) mode, str);
135 }
136
137 /* Return a character indicating the type of file described by
138 file mode BITS:
139 'd' for directories
140 'b' for block special files
141 'c' for character special files
142 'm' for multiplexor files
143 'l' for symbolic links
144 's' for sockets
145 'p' for fifos
146 '-' for any other file type. */
147
148 #ifndef S_ISDIR
149 #ifdef S_IFDIR
150 #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
151 #else /* ! defined (S_IFDIR) */
152 #define S_ISDIR(i) (((i) & 0170000) == 040000)
153 #endif /* ! defined (S_IFDIR) */
154 #endif /* ! defined (S_ISDIR) */
155
156 #ifndef S_ISBLK
157 #ifdef S_IFBLK
158 #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
159 #else /* ! defined (S_IFBLK) */
160 #define S_ISBLK(i) 0
161 #endif /* ! defined (S_IFBLK) */
162 #endif /* ! defined (S_ISBLK) */
163
164 #ifndef S_ISCHR
165 #ifdef S_IFCHR
166 #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
167 #else /* ! defined (S_IFCHR) */
168 #define S_ISCHR(i) 0
169 #endif /* ! defined (S_IFCHR) */
170 #endif /* ! defined (S_ISCHR) */
171
172 #ifndef S_ISFIFO
173 #ifdef S_IFIFO
174 #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
175 #else /* ! defined (S_IFIFO) */
176 #define S_ISFIFO(i) 0
177 #endif /* ! defined (S_IFIFO) */
178 #endif /* ! defined (S_ISFIFO) */
179
180 #ifndef S_ISSOCK
181 #ifdef S_IFSOCK
182 #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
183 #else /* ! defined (S_IFSOCK) */
184 #define S_ISSOCK(i) 0
185 #endif /* ! defined (S_IFSOCK) */
186 #endif /* ! defined (S_ISSOCK) */
187
188 #ifndef S_ISLNK
189 #ifdef S_IFLNK
190 #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
191 #else /* ! defined (S_IFLNK) */
192 #define S_ISLNK(i) 0
193 #endif /* ! defined (S_IFLNK) */
194 #endif /* ! defined (S_ISLNK) */
195
196 static char
197 ftypelet (bits)
198 unsigned long bits;
199 {
200 if (S_ISDIR (bits))
201 return 'd';
202 if (S_ISLNK (bits))
203 return 'l';
204 if (S_ISBLK (bits))
205 return 'b';
206 if (S_ISCHR (bits))
207 return 'c';
208 if (S_ISSOCK (bits))
209 return 's';
210 if (S_ISFIFO (bits))
211 return 'p';
212
213 #ifdef S_IFMT
214 #ifdef S_IFMPC
215 if ((bits & S_IFMT) == S_IFMPC
216 || (bits & S_IFMT) == S_IFMPB)
217 return 'm';
218 #endif
219 #ifdef S_IFNWK
220 if ((bits & S_IFMT) == S_IFNWK)
221 return 'n';
222 #endif
223 #endif
224
225 return '-';
226 }
227
228 /* Set the 's' and 't' flags in file attributes string CHARS,
229 according to the file mode BITS. */
230
231 static void
232 setst (bits, chars)
233 unsigned long bits ATTRIBUTE_UNUSED;
234 char *chars ATTRIBUTE_UNUSED;
235 {
236 #ifdef S_ISUID
237 if (bits & S_ISUID)
238 {
239 if (chars[3] != 'x')
240 /* Set-uid, but not executable by owner. */
241 chars[3] = 'S';
242 else
243 chars[3] = 's';
244 }
245 #endif
246 #ifdef S_ISGID
247 if (bits & S_ISGID)
248 {
249 if (chars[6] != 'x')
250 /* Set-gid, but not executable by group. */
251 chars[6] = 'S';
252 else
253 chars[6] = 's';
254 }
255 #endif
256 #ifdef S_ISVTX
257 if (bits & S_ISVTX)
258 {
259 if (chars[9] != 'x')
260 /* Sticky, but not executable by others. */
261 chars[9] = 'T';
262 else
263 chars[9] = 't';
264 }
265 #endif
266 }
This page took 0.035993 seconds and 5 git commands to generate.