* ppc-opc.c (extract_sprg): Correct operand range check.
[binutils.git] / binutils / filemode.c
blob8b29defc5287a4af5087d2d8b6348e73c951acd5
1 /* filemode.c -- make a string describing file modes
2 Copyright 1985, 1990, 1991, 1994, 1995, 1997, 1999, 2002, 2003, 2005,
3 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
18 02110-1301, USA. */
20 #include "sysdep.h"
21 #include "bfd.h"
22 #include "bucomm.h"
24 static char ftypelet (unsigned long);
25 static void setst (unsigned long, char *);
27 /* filemodestring - fill in string STR with an ls-style ASCII
28 representation of the st_mode field of file stats block STATP.
29 10 characters are stored in STR; no terminating null is added.
30 The characters stored in STR are:
32 0 File type. 'd' for directory, 'c' for character
33 special, 'b' for block special, 'm' for multiplex,
34 'l' for symbolic link, 's' for socket, 'p' for fifo,
35 '-' for any other file type
37 1 'r' if the owner may read, '-' otherwise.
39 2 'w' if the owner may write, '-' otherwise.
41 3 'x' if the owner may execute, 's' if the file is
42 set-user-id, '-' otherwise.
43 'S' if the file is set-user-id, but the execute
44 bit isn't set.
46 4 'r' if group members may read, '-' otherwise.
48 5 'w' if group members may write, '-' otherwise.
50 6 'x' if group members may execute, 's' if the file is
51 set-group-id, '-' otherwise.
52 'S' if it is set-group-id but not executable.
54 7 'r' if any user may read, '-' otherwise.
56 8 'w' if any user may write, '-' otherwise.
58 9 'x' if any user may execute, 't' if the file is "sticky"
59 (will be retained in swap space after execution), '-'
60 otherwise.
61 'T' if the file is sticky but not executable. */
63 /* Get definitions for the file permission bits. */
65 #ifndef S_IRWXU
66 #define S_IRWXU 0700
67 #endif
68 #ifndef S_IRUSR
69 #define S_IRUSR 0400
70 #endif
71 #ifndef S_IWUSR
72 #define S_IWUSR 0200
73 #endif
74 #ifndef S_IXUSR
75 #define S_IXUSR 0100
76 #endif
78 #ifndef S_IRWXG
79 #define S_IRWXG 0070
80 #endif
81 #ifndef S_IRGRP
82 #define S_IRGRP 0040
83 #endif
84 #ifndef S_IWGRP
85 #define S_IWGRP 0020
86 #endif
87 #ifndef S_IXGRP
88 #define S_IXGRP 0010
89 #endif
91 #ifndef S_IRWXO
92 #define S_IRWXO 0007
93 #endif
94 #ifndef S_IROTH
95 #define S_IROTH 0004
96 #endif
97 #ifndef S_IWOTH
98 #define S_IWOTH 0002
99 #endif
100 #ifndef S_IXOTH
101 #define S_IXOTH 0001
102 #endif
104 /* Like filemodestring, but only the relevant part of the `struct stat'
105 is given as an argument. */
107 void
108 mode_string (unsigned long mode, char *str)
110 str[0] = ftypelet ((unsigned long) mode);
111 str[1] = (mode & S_IRUSR) != 0 ? 'r' : '-';
112 str[2] = (mode & S_IWUSR) != 0 ? 'w' : '-';
113 str[3] = (mode & S_IXUSR) != 0 ? 'x' : '-';
114 str[4] = (mode & S_IRGRP) != 0 ? 'r' : '-';
115 str[5] = (mode & S_IWGRP) != 0 ? 'w' : '-';
116 str[6] = (mode & S_IXGRP) != 0 ? 'x' : '-';
117 str[7] = (mode & S_IROTH) != 0 ? 'r' : '-';
118 str[8] = (mode & S_IWOTH) != 0 ? 'w' : '-';
119 str[9] = (mode & S_IXOTH) != 0 ? 'x' : '-';
120 setst ((unsigned long) mode, str);
123 /* Return a character indicating the type of file described by
124 file mode BITS:
125 'd' for directories
126 'b' for block special files
127 'c' for character special files
128 'm' for multiplexer files
129 'l' for symbolic links
130 's' for sockets
131 'p' for fifos
132 '-' for any other file type. */
134 #ifndef S_ISDIR
135 #ifdef S_IFDIR
136 #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
137 #else /* ! defined (S_IFDIR) */
138 #define S_ISDIR(i) (((i) & 0170000) == 040000)
139 #endif /* ! defined (S_IFDIR) */
140 #endif /* ! defined (S_ISDIR) */
142 #ifndef S_ISBLK
143 #ifdef S_IFBLK
144 #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
145 #else /* ! defined (S_IFBLK) */
146 #define S_ISBLK(i) 0
147 #endif /* ! defined (S_IFBLK) */
148 #endif /* ! defined (S_ISBLK) */
150 #ifndef S_ISCHR
151 #ifdef S_IFCHR
152 #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
153 #else /* ! defined (S_IFCHR) */
154 #define S_ISCHR(i) 0
155 #endif /* ! defined (S_IFCHR) */
156 #endif /* ! defined (S_ISCHR) */
158 #ifndef S_ISFIFO
159 #ifdef S_IFIFO
160 #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
161 #else /* ! defined (S_IFIFO) */
162 #define S_ISFIFO(i) 0
163 #endif /* ! defined (S_IFIFO) */
164 #endif /* ! defined (S_ISFIFO) */
166 #ifndef S_ISSOCK
167 #ifdef S_IFSOCK
168 #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
169 #else /* ! defined (S_IFSOCK) */
170 #define S_ISSOCK(i) 0
171 #endif /* ! defined (S_IFSOCK) */
172 #endif /* ! defined (S_ISSOCK) */
174 #ifndef S_ISLNK
175 #ifdef S_IFLNK
176 #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
177 #else /* ! defined (S_IFLNK) */
178 #define S_ISLNK(i) 0
179 #endif /* ! defined (S_IFLNK) */
180 #endif /* ! defined (S_ISLNK) */
182 static char
183 ftypelet (unsigned long bits)
185 if (S_ISDIR (bits))
186 return 'd';
187 if (S_ISLNK (bits))
188 return 'l';
189 if (S_ISBLK (bits))
190 return 'b';
191 if (S_ISCHR (bits))
192 return 'c';
193 if (S_ISSOCK (bits))
194 return 's';
195 if (S_ISFIFO (bits))
196 return 'p';
198 #ifdef S_IFMT
199 #ifdef S_IFMPC
200 if ((bits & S_IFMT) == S_IFMPC
201 || (bits & S_IFMT) == S_IFMPB)
202 return 'm';
203 #endif
204 #ifdef S_IFNWK
205 if ((bits & S_IFMT) == S_IFNWK)
206 return 'n';
207 #endif
208 #endif
210 return '-';
213 /* Set the 's' and 't' flags in file attributes string CHARS,
214 according to the file mode BITS. */
216 static void
217 setst (unsigned long bits ATTRIBUTE_UNUSED, char *chars ATTRIBUTE_UNUSED)
219 #ifdef S_ISUID
220 if (bits & S_ISUID)
222 if (chars[3] != 'x')
223 /* Set-uid, but not executable by owner. */
224 chars[3] = 'S';
225 else
226 chars[3] = 's';
228 #endif
229 #ifdef S_ISGID
230 if (bits & S_ISGID)
232 if (chars[6] != 'x')
233 /* Set-gid, but not executable by group. */
234 chars[6] = 'S';
235 else
236 chars[6] = 's';
238 #endif
239 #ifdef S_ISVTX
240 if (bits & S_ISVTX)
242 if (chars[9] != 'x')
243 /* Sticky, but not executable by others. */
244 chars[9] = 'T';
245 else
246 chars[9] = 't';
248 #endif