1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #if defined (emacs) || defined (CONFIG_BROKETS)
26 #include <sys/types.h>
30 #define S_IREAD S_IRUSR
31 #define S_IWRITE S_IWUSR
32 #define S_IEXEC S_IXUSR
35 #if 0 /* This is unreliable, since GCC 2.5 always has S_ISREG in its
36 fixed headers but it does not always have mode_t.
37 It seems safer not to try to use mode_t ever. */
38 #if !defined(S_ISREG) || defined(NO_MODE_T)
39 /* Doesn't have POSIX.1 stat stuff or doesn't have mode_t. */
40 #define mode_t unsigned short
44 #ifdef STAT_MACROS_BROKEN
75 #endif /* STAT_MACROS_BROKEN. */
77 #if !defined(S_ISBLK) && defined(S_IFBLK)
78 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
80 #if !defined(S_ISCHR) && defined(S_IFCHR)
81 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
83 #if !defined(S_ISDIR) && defined(S_IFDIR)
84 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
86 #if !defined(S_ISREG) && defined(S_IFREG)
87 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
89 #if !defined(S_ISFIFO) && defined(S_IFIFO)
90 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
92 #if !defined(S_ISLNK) && defined(S_IFLNK)
93 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
95 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
96 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
98 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
99 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
100 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
102 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
103 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
107 static char ftypelet ();
109 static void setst ();
111 /* filemodestring - fill in string STR with an ls-style ASCII
112 representation of the st_mode field of file stats block STATP.
113 10 characters are stored in STR; no terminating null is added.
114 The characters stored in STR are:
116 0 File type. 'd' for directory, 'c' for character
117 special, 'b' for block special, 'm' for multiplex,
118 'l' for symbolic link, 's' for socket, 'p' for fifo,
119 '-' for regular, '?' for any other file type
121 1 'r' if the owner may read, '-' otherwise.
123 2 'w' if the owner may write, '-' otherwise.
125 3 'x' if the owner may execute, 's' if the file is
126 set-user-id, '-' otherwise.
127 'S' if the file is set-user-id, but the execute
130 4 'r' if group members may read, '-' otherwise.
132 5 'w' if group members may write, '-' otherwise.
134 6 'x' if group members may execute, 's' if the file is
135 set-group-id, '-' otherwise.
136 'S' if it is set-group-id but not executable.
138 7 'r' if any user may read, '-' otherwise.
140 8 'w' if any user may write, '-' otherwise.
142 9 'x' if any user may execute, 't' if the file is "sticky"
143 (will be retained in swap space after execution), '-'
145 'T' if the file is sticky but not executable. */
148 filemodestring (statp, str)
152 mode_string (statp->st_mode, str);
155 /* Like filemodestring, but only the relevant part of the `struct stat'
156 is given as an argument. */
159 mode_string (mode, str)
163 str[0] = ftypelet ((long) mode);
164 rwx ((mode & 0700) << 0, &str[1]);
165 rwx ((mode & 0070) << 3, &str[4]);
166 rwx ((mode & 0007) << 6, &str[7]);
170 /* Return a character indicating the type of file described by
173 'b' for block special files
174 'c' for character special files
175 'm' for multiplexor files
176 'l' for symbolic links
179 '-' for regular files
180 '?' for any other file type. */
219 /* Look at read, write, and execute bits in BITS and set
220 flags in CHARS accordingly. */
227 chars[0] = (bits & S_IREAD) ? 'r' : '-';
228 chars[1] = (bits & S_IWRITE) ? 'w' : '-';
229 chars[2] = (bits & S_IEXEC) ? 'x' : '-';
232 /* Set the 's' and 't' flags in file attributes string CHARS,
233 according to the file mode BITS. */
244 /* Set-uid, but not executable by owner. */
254 /* Set-gid, but not executable by group. */
264 /* Sticky, but not executable by others. */