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