1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993, 1998 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
51 #ifdef STAT_MACROS_BROKEN
62 #endif /* STAT_MACROS_BROKEN. */
64 #if !defined(S_ISBLK) && defined(S_IFBLK)
65 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
67 #if !defined(S_ISCHR) && defined(S_IFCHR)
68 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
70 #if !defined(S_ISDIR) && defined(S_IFDIR)
71 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73 #if !defined(S_ISREG) && defined(S_IFREG)
74 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76 #if !defined(S_ISFIFO) && defined(S_IFIFO)
77 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
79 #if !defined(S_ISLNK) && defined(S_IFLNK)
80 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
82 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
83 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
85 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
86 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
87 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
89 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
90 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
93 /* Look at read, write, and execute bits in BITS and set
94 flags in CHARS accordingly. */
97 rwx (short unsigned int bits, char *chars)
99 chars[0] = (bits & S_IRUSR) ? 'r' : '-';
100 chars[1] = (bits & S_IWUSR) ? 'w' : '-';
101 chars[2] = (bits & S_IXUSR) ? 'x' : '-';
104 /* Set the 's' and 't' flags in file attributes string CHARS,
105 according to the file mode BITS. */
108 setst (short unsigned int bits, char *chars)
114 /* Set-uid, but not executable by owner. */
124 /* Set-gid, but not executable by group. */
134 /* Sticky, but not executable by others. */
142 /* Return a character indicating the type of file described by
145 'b' for block special files
146 'c' for character special files
147 'm' for multiplexor files
148 'M' for an off-line (regular) file
149 'l' for symbolic links
152 '-' for regular files
153 '?' for any other file type. */
156 ftypelet (long int bits)
189 /* The following two tests are for Cray DMF (Data Migration
190 Facility), which is a HSM file system. A migrated file has a
191 `st_dm_mode' that is different from the normal `st_mode', so any
192 tests for migrated files should use the former. */
196 /* off line, with data */
200 /* off line, with no data */
207 /* Like filemodestring, but only the relevant part of the `struct stat'
208 is given as an argument. */
211 mode_string (short unsigned int mode, char *str)
213 str[0] = ftypelet ((long) mode);
214 rwx ((mode & 0700) << 0, &str[1]);
215 rwx ((mode & 0070) << 3, &str[4]);
216 rwx ((mode & 0007) << 6, &str[7]);
220 /* filemodestring - fill in string STR with an ls-style ASCII
221 representation of the st_mode field of file stats block STATP.
222 10 characters are stored in STR; no terminating null is added.
223 The characters stored in STR are:
225 0 File type. 'd' for directory, 'c' for character
226 special, 'b' for block special, 'm' for multiplex,
227 'l' for symbolic link, 's' for socket, 'p' for fifo,
228 '-' for regular, '?' for any other file type
230 1 'r' if the owner may read, '-' otherwise.
232 2 'w' if the owner may write, '-' otherwise.
234 3 'x' if the owner may execute, 's' if the file is
235 set-user-id, '-' otherwise.
236 'S' if the file is set-user-id, but the execute
239 4 'r' if group members may read, '-' otherwise.
241 5 'w' if group members may write, '-' otherwise.
243 6 'x' if group members may execute, 's' if the file is
244 set-group-id, '-' otherwise.
245 'S' if it is set-group-id but not executable.
247 7 'r' if any user may read, '-' otherwise.
249 8 'w' if any user may write, '-' otherwise.
251 9 'x' if any user may execute, 't' if the file is "sticky"
252 (will be retained in swap space after execution), '-'
254 'T' if the file is sticky but not executable. */
257 filemodestring (struct stat *statp, char *str)
259 mode_string (statp->st_mode, str);