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