1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993, 2004 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
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA. */
23 #include <sys/types.h>
28 # define S_IRUSR S_IREAD
30 # define S_IRUSR 00400
36 # define S_IWUSR S_IWRITE
38 # define S_IWUSR 00200
44 # define S_IXUSR S_IEXEC
46 # define S_IXUSR 00100
50 #ifdef STAT_MACROS_BROKEN
61 #endif /* STAT_MACROS_BROKEN. */
63 #if !defined(S_ISBLK) && defined(S_IFBLK)
64 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
66 #if !defined(S_ISCHR) && defined(S_IFCHR)
67 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
69 #if !defined(S_ISDIR) && defined(S_IFDIR)
70 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
72 #if !defined(S_ISREG) && defined(S_IFREG)
73 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
75 #if !defined(S_ISFIFO) && defined(S_IFIFO)
76 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
78 #if !defined(S_ISLNK) && defined(S_IFLNK)
79 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
81 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
82 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
84 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
85 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
86 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
88 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
89 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
92 /* Return a character indicating the type of file described by
95 'b' for block special files
96 'c' for character special files
97 'm' for multiplexor files
98 'l' for symbolic links
101 '-' for regular files
102 '?' for any other file type. */
140 /* Look at read, write, and execute bits in BITS and set
141 flags in CHARS accordingly. */
144 rwx (unsigned short bits, char *chars)
146 chars[0] = (bits & S_IRUSR) ? 'r' : '-';
147 chars[1] = (bits & S_IWUSR) ? 'w' : '-';
148 chars[2] = (bits & S_IXUSR) ? 'x' : '-';
151 /* Set the 's' and 't' flags in file attributes string CHARS,
152 according to the file mode BITS. */
155 setst (unsigned short bits, char *chars)
161 /* Set-uid, but not executable by owner. */
171 /* Set-gid, but not executable by group. */
181 /* Sticky, but not executable by others. */
189 /* Like filemodestring (see below), but only the relevant part of the
190 `struct stat' is given as an argument. */
193 mode_string (unsigned short mode, char *str)
195 str[0] = ftypelet ((long) mode);
196 rwx ((mode & 0700) << 0, &str[1]);
197 rwx ((mode & 0070) << 3, &str[4]);
198 rwx ((mode & 0007) << 6, &str[7]);
202 /* filemodestring - fill in string STR with an ls-style ASCII
203 representation of the st_mode field of file stats block STATP.
204 10 characters are stored in STR; no terminating null is added.
205 The characters stored in STR are:
207 0 File type. 'd' for directory, 'c' for character
208 special, 'b' for block special, 'm' for multiplex,
209 'l' for symbolic link, 's' for socket, 'p' for fifo,
210 '-' for regular, '?' for any other file type
212 1 'r' if the owner may read, '-' otherwise.
214 2 'w' if the owner may write, '-' otherwise.
216 3 'x' if the owner may execute, 's' if the file is
217 set-user-id, '-' otherwise.
218 'S' if the file is set-user-id, but the execute
221 4 'r' if group members may read, '-' otherwise.
223 5 'w' if group members may write, '-' otherwise.
225 6 'x' if group members may execute, 's' if the file is
226 set-group-id, '-' otherwise.
227 'S' if it is set-group-id but not executable.
229 7 'r' if any user may read, '-' otherwise.
231 8 'w' if any user may write, '-' otherwise.
233 9 'x' if any user may execute, 't' if the file is "sticky"
234 (will be retained in swap space after execution), '-'
236 'T' if the file is sticky but not executable. */
239 filemodestring (struct stat *statp, char *str)
241 mode_string (statp->st_mode, str);