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