1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993, 1998-2000 Free Software Foundation, Inc.
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)
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.
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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
29 # define S_IRUSR S_IREAD
31 # define S_IRUSR 00400
37 # define S_IWUSR S_IWRITE
39 # define S_IWUSR 00200
45 # define S_IXUSR S_IEXEC
47 # define S_IXUSR 00100
52 # define S_IRGRP (S_IRUSR >> 3)
55 # define S_IWGRP (S_IWUSR >> 3)
58 # define S_IXGRP (S_IXUSR >> 3)
61 # define S_IROTH (S_IRUSR >> 6)
64 # define S_IWOTH (S_IWUSR >> 6)
67 # define S_IXOTH (S_IXUSR >> 6)
70 #ifdef STAT_MACROS_BROKEN
81 #endif /* STAT_MACROS_BROKEN. */
83 #if !defined S_ISBLK && defined S_IFBLK
84 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
86 #if !defined S_ISCHR && defined S_IFCHR
87 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
89 #if !defined S_ISDIR && defined S_IFDIR
90 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
92 #if !defined S_ISREG && defined S_IFREG
93 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
95 #if !defined S_ISFIFO && defined S_IFIFO
96 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
98 #if !defined S_ISLNK && defined S_IFLNK
99 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
101 #if !defined S_ISSOCK && defined S_IFSOCK
102 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
104 #if !defined S_ISMPB && defined S_IFMPB /* V7 */
105 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
106 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
108 #if !defined S_ISNWK && defined S_IFNWK /* HP/UX */
109 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
111 #if !defined S_ISDOOR && defined S_IFDOOR /* Solaris 2.5 and up */
112 # define S_ISDOOR(m) (((m) & S_IFMT) == S_IFDOOR)
114 #if !defined S_ISCTG && defined S_IFCTG /* MassComp */
115 # define S_ISCTG(m) (((m) & S_IFMT) == S_IFCTG)
120 /* Set the 's' and 't' flags in file attributes string CHARS,
121 according to the file mode BITS. */
124 setst (mode_t bits
, char *chars
)
130 /* Set-uid, but not executable by owner. */
140 /* Set-gid, but not executable by group. */
150 /* Sticky, but not executable by others. */
158 /* Return a character indicating the type of file described by
162 'b' for block special files
163 'c' for character special files
164 'n' for network special files
165 'm' for multiplexor files
166 'M' for an off-line (regular) file
167 'l' for symbolic links
170 'C' for contigous data files
171 '-' for regular files
172 '?' for any other file type. */
175 ftypelet (mode_t bits
)
216 /* The following two tests are for Cray DMF (Data Migration
217 Facility), which is a HSM file system. A migrated file has a
218 `st_dm_mode' that is different from the normal `st_mode', so any
219 tests for migrated files should use the former. */
223 /* off line, with data */
227 /* off line, with no data */
234 /* Like filemodestring, but only the relevant part of the `struct stat'
235 is given as an argument. */
238 mode_string (mode_t mode
, char *str
)
240 str
[0] = ftypelet (mode
);
241 str
[1] = mode
& S_IRUSR
? 'r' : '-';
242 str
[2] = mode
& S_IWUSR
? 'w' : '-';
243 str
[3] = mode
& S_IXUSR
? 'x' : '-';
244 str
[4] = mode
& S_IRGRP
? 'r' : '-';
245 str
[5] = mode
& S_IWGRP
? 'w' : '-';
246 str
[6] = mode
& S_IXGRP
? 'x' : '-';
247 str
[7] = mode
& S_IROTH
? 'r' : '-';
248 str
[8] = mode
& S_IWOTH
? 'w' : '-';
249 str
[9] = mode
& S_IXOTH
? 'x' : '-';
253 /* filemodestring - fill in string STR with an ls-style ASCII
254 representation of the st_mode field of file stats block STATP.
255 10 characters are stored in STR; no terminating null is added.
256 The characters stored in STR are:
258 0 File type. 'd' for directory, 'c' for character
259 special, 'b' for block special, 'm' for multiplex,
260 'l' for symbolic link, 's' for socket, 'p' for fifo,
261 '-' for regular, '?' for any other file type
263 1 'r' if the owner may read, '-' otherwise.
265 2 'w' if the owner may write, '-' otherwise.
267 3 'x' if the owner may execute, 's' if the file is
268 set-user-id, '-' otherwise.
269 'S' if the file is set-user-id, but the execute
272 4 'r' if group members may read, '-' otherwise.
274 5 'w' if group members may write, '-' otherwise.
276 6 'x' if group members may execute, 's' if the file is
277 set-group-id, '-' otherwise.
278 'S' if it is set-group-id but not executable.
280 7 'r' if any user may read, '-' otherwise.
282 8 'w' if any user may write, '-' otherwise.
284 9 'x' if any user may execute, 't' if the file is "sticky"
285 (will be retained in swap space after execution), '-'
287 'T' if the file is sticky but not executable. */
290 filemodestring (struct stat
*statp
, char *str
)
292 mode_string (statp
->st_mode
, str
);