1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993, 2001-2011 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 3, 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
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 #include <sys/types.h>
25 # define S_IRUSR S_IREAD
27 # define S_IRUSR 00400
33 # define S_IWUSR S_IWRITE
35 # define S_IWUSR 00200
41 # define S_IXUSR S_IEXEC
43 # define S_IXUSR 00100
47 #ifdef STAT_MACROS_BROKEN
58 #endif /* STAT_MACROS_BROKEN. */
60 #if !defined(S_ISBLK) && defined(S_IFBLK)
61 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
63 #if !defined(S_ISCHR) && defined(S_IFCHR)
64 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
66 #if !defined(S_ISDIR) && defined(S_IFDIR)
67 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
69 #if !defined(S_ISREG) && defined(S_IFREG)
70 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
72 #if !defined(S_ISFIFO) && defined(S_IFIFO)
73 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
75 #if !defined(S_ISLNK) && defined(S_IFLNK)
76 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
78 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
79 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
81 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
82 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
83 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
85 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
86 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
89 void mode_string (short unsigned int mode
, char *str
);
90 static char ftypelet (long int bits
);
91 static void rwx (short unsigned int bits
, char *chars
);
92 static void setst (short unsigned int bits
, char *chars
);
94 /* filemodestring - fill in string STR with an ls-style ASCII
95 representation of the st_mode field of file stats block STATP.
96 10 characters are stored in STR; no terminating null is added.
97 The characters stored in STR are:
99 0 File type. 'd' for directory, 'c' for character
100 special, 'b' for block special, 'm' for multiplex,
101 'l' for symbolic link, 's' for socket, 'p' for fifo,
102 '-' for regular, '?' for any other file type
104 1 'r' if the owner may read, '-' otherwise.
106 2 'w' if the owner may write, '-' otherwise.
108 3 'x' if the owner may execute, 's' if the file is
109 set-user-id, '-' otherwise.
110 'S' if the file is set-user-id, but the execute
113 4 'r' if group members may read, '-' otherwise.
115 5 'w' if group members may write, '-' otherwise.
117 6 'x' if group members may execute, 's' if the file is
118 set-group-id, '-' otherwise.
119 'S' if it is set-group-id but not executable.
121 7 'r' if any user may read, '-' otherwise.
123 8 'w' if any user may write, '-' otherwise.
125 9 'x' if any user may execute, 't' if the file is "sticky"
126 (will be retained in swap space after execution), '-'
128 'T' if the file is sticky but not executable. */
131 filemodestring (struct stat
*statp
, char *str
)
133 mode_string (statp
->st_mode
, str
);
136 /* Like filemodestring, but only the relevant part of the `struct stat'
137 is given as an argument. */
140 mode_string (short unsigned int mode
, char *str
)
142 str
[0] = ftypelet ((long) mode
);
143 rwx ((mode
& 0700) << 0, &str
[1]);
144 rwx ((mode
& 0070) << 3, &str
[4]);
145 rwx ((mode
& 0007) << 6, &str
[7]);
149 /* Return a character indicating the type of file described by
152 'b' for block special files
153 'c' for character special files
154 'm' for multiplexor files
155 'l' for symbolic links
158 '-' for regular files
159 '?' for any other file type. */
162 ftypelet (long int bits
)
197 /* Look at read, write, and execute bits in BITS and set
198 flags in CHARS accordingly. */
201 rwx (short unsigned int bits
, char *chars
)
203 chars
[0] = (bits
& S_IRUSR
) ? 'r' : '-';
204 chars
[1] = (bits
& S_IWUSR
) ? 'w' : '-';
205 chars
[2] = (bits
& S_IXUSR
) ? 'x' : '-';
208 /* Set the 's' and 't' flags in file attributes string CHARS,
209 according to the file mode BITS. */
212 setst (short unsigned int bits
, char *chars
)
218 /* Set-uid, but not executable by owner. */
228 /* Set-gid, but not executable by group. */
238 /* Sticky, but not executable by others. */