1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993 Free Software Foundation, Inc.
4 This file is part of SXEmacs
6 SXEmacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Synched up with: FSF 19.30. */
27 static void mode_string(unsigned short mode, char *str);
28 static char ftypelet(mode_t bits);
29 static void rwx(unsigned short bits, char *chars);
30 static void setst(unsigned short bits, char *chars);
32 /* filemodestring - fill in string STR with an ls-style ASCII
33 representation of the st_mode field of file stats block STATP.
34 10 characters are stored in STR; no terminating null is added.
35 The characters stored in STR are:
37 0 File type. 'd' for directory, 'c' for character
38 special, 'b' for block special, 'm' for multiplex,
39 'l' for symbolic link, 's' for socket, 'p' for fifo,
40 '-' for regular, '?' for any other file type
42 1 'r' if the owner may read, '-' otherwise.
44 2 'w' if the owner may write, '-' otherwise.
46 3 'x' if the owner may execute, 's' if the file is
47 set-user-id, '-' otherwise.
48 'S' if the file is set-user-id, but the execute
51 4 'r' if group members may read, '-' otherwise.
53 5 'w' if group members may write, '-' otherwise.
55 6 'x' if group members may execute, 's' if the file is
56 set-group-id, '-' otherwise.
57 'S' if it is set-group-id but not executable.
59 7 'r' if any user may read, '-' otherwise.
61 8 'w' if any user may write, '-' otherwise.
63 9 'x' if any user may execute, 't' if the file is "sticky"
64 (will be retained in swap space after execution), '-'
66 'T' if the file is sticky but not executable. */
68 void filemodestring(struct stat *statp, char *str)
70 mode_string(statp->st_mode, str);
73 /* Like filemodestring, but only the relevant part of the `struct stat'
74 is given as an argument. */
76 static void mode_string(unsigned short mode, char *str)
78 str[0] = ftypelet(mode);
79 rwx((unsigned short)((mode & 0700) << 0), &str[1]);
80 rwx((unsigned short)((mode & 0070) << 3), &str[4]);
81 rwx((unsigned short)((mode & 0007) << 6), &str[7]);
85 /* Return a character indicating the type of file described by
88 'b' for block special files
89 'c' for character special files
90 'm' for multiplexor files
91 'l' for symbolic links
95 '?' for any other file type. */
97 static char ftypelet(mode_t bits)
132 /* Look at read, write, and execute bits in BITS and set
133 flags in CHARS accordingly. */
135 static void rwx(unsigned short bits, char *chars)
137 chars[0] = (bits & S_IRUSR) ? 'r' : '-';
138 chars[1] = (bits & S_IWUSR) ? 'w' : '-';
139 chars[2] = (bits & S_IXUSR) ? 'x' : '-';
142 /* Set the 's' and 't' flags in file attributes string CHARS,
143 according to the file mode BITS. */
145 static void setst(unsigned short bits, char *chars)
148 if (bits & S_ISUID) {
150 /* Set-uid, but not executable by owner. */
157 if (bits & S_ISGID) {
159 /* Set-gid, but not executable by group. */
166 if (bits & S_ISVTX) {
168 /* Sticky, but not executable by others. */