Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / filemode.c
1 /* filemode.c -- make a string describing file modes
2    Copyright (C) 1985, 1990, 1993 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs
5
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.
10
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.
15
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/>. */
18
19
20 /* Synched up with: FSF 19.30. */
21
22 #include <config.h>
23 #include "lisp.h"
24
25 #include "sysfile.h"
26
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);
31
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:
36
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
41
42    1    'r' if the owner may read, '-' otherwise.
43
44    2    'w' if the owner may write, '-' otherwise.
45
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
49         bit isn't set.
50
51    4    'r' if group members may read, '-' otherwise.
52
53    5    'w' if group members may write, '-' otherwise.
54
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.
58
59    7    'r' if any user may read, '-' otherwise.
60
61    8    'w' if any user may write, '-' otherwise.
62
63    9    'x' if any user may execute, 't' if the file is "sticky"
64         (will be retained in swap space after execution), '-'
65         otherwise.
66         'T' if the file is sticky but not executable. */
67
68 void filemodestring(struct stat *statp, char *str)
69 {
70         mode_string(statp->st_mode, str);
71 }
72
73 /* Like filemodestring, but only the relevant part of the `struct stat'
74    is given as an argument. */
75
76 static void mode_string(unsigned short mode, char *str)
77 {
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]);
82         setst(mode, str);
83 }
84
85 /* Return a character indicating the type of file described by
86    file mode BITS:
87    'd' for directories
88    'b' for block special files
89    'c' for character special files
90    'm' for multiplexor files
91    'l' for symbolic links
92    's' for sockets
93    'p' for fifos
94    '-' for regular files
95    '?' for any other file type. */
96
97 static char ftypelet(mode_t bits)
98 {
99 #ifdef S_ISBLK
100         if (S_ISBLK(bits))
101                 return 'b';
102 #endif
103         if (S_ISCHR(bits))
104                 return 'c';
105         if (S_ISDIR(bits))
106                 return 'd';
107         if (S_ISREG(bits))
108                 return '-';
109 #ifdef S_ISFIFO
110         if (S_ISFIFO(bits))
111                 return 'p';
112 #endif
113 #ifdef S_ISLNK
114         if (S_ISLNK(bits))
115                 return 'l';
116 #endif
117 #ifdef S_ISSOCK
118         if (S_ISSOCK(bits))
119                 return 's';
120 #endif
121 #ifdef S_ISMPC
122         if (S_ISMPC(bits))
123                 return 'm';
124 #endif
125 #ifdef S_ISNWK
126         if (S_ISNWK(bits))
127                 return 'n';
128 #endif
129         return '?';
130 }
131
132 /* Look at read, write, and execute bits in BITS and set
133    flags in CHARS accordingly. */
134
135 static void rwx(unsigned short bits, char *chars)
136 {
137         chars[0] = (bits & S_IRUSR) ? 'r' : '-';
138         chars[1] = (bits & S_IWUSR) ? 'w' : '-';
139         chars[2] = (bits & S_IXUSR) ? 'x' : '-';
140 }
141
142 /* Set the 's' and 't' flags in file attributes string CHARS,
143    according to the file mode BITS. */
144
145 static void setst(unsigned short bits, char *chars)
146 {
147 #ifdef S_ISUID
148         if (bits & S_ISUID) {
149                 if (chars[3] != 'x')
150                         /* Set-uid, but not executable by owner. */
151                         chars[3] = 'S';
152                 else
153                         chars[3] = 's';
154         }
155 #endif
156 #ifdef S_ISGID
157         if (bits & S_ISGID) {
158                 if (chars[6] != 'x')
159                         /* Set-gid, but not executable by group. */
160                         chars[6] = 'S';
161                 else
162                         chars[6] = 's';
163         }
164 #endif
165 #ifdef S_ISVTX
166         if (bits & S_ISVTX) {
167                 if (chars[9] != 'x')
168                         /* Sticky, but not executable by others. */
169                         chars[9] = 'T';
170                 else
171                         chars[9] = 't';
172         }
173 #endif
174 }