0
|
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 XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.30. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include "lisp.h"
|
|
25
|
|
26 #include "sysfile.h"
|
|
27
|
|
28 static void mode_string (unsigned short mode, char *str);
|
|
29 static char ftypelet (mode_t bits);
|
|
30 static void rwx (unsigned short bits, char *chars);
|
|
31 static void setst (unsigned short bits, char *chars);
|
|
32
|
1467
|
33 /* declared in "sysfile.h"
|
|
34
|
|
35 filemodestring - fill in string STR with an ls-style ASCII
|
0
|
36 representation of the st_mode field of file stats block STATP.
|
|
37 10 characters are stored in STR; no terminating null is added.
|
|
38 The characters stored in STR are:
|
|
39
|
|
40 0 File type. 'd' for directory, 'c' for character
|
|
41 special, 'b' for block special, 'm' for multiplex,
|
|
42 'l' for symbolic link, 's' for socket, 'p' for fifo,
|
|
43 '-' for regular, '?' for any other file type
|
|
44
|
|
45 1 'r' if the owner may read, '-' otherwise.
|
|
46
|
|
47 2 'w' if the owner may write, '-' otherwise.
|
|
48
|
|
49 3 'x' if the owner may execute, 's' if the file is
|
|
50 set-user-id, '-' otherwise.
|
|
51 'S' if the file is set-user-id, but the execute
|
|
52 bit isn't set.
|
|
53
|
|
54 4 'r' if group members may read, '-' otherwise.
|
|
55
|
|
56 5 'w' if group members may write, '-' otherwise.
|
|
57
|
|
58 6 'x' if group members may execute, 's' if the file is
|
|
59 set-group-id, '-' otherwise.
|
|
60 'S' if it is set-group-id but not executable.
|
|
61
|
|
62 7 'r' if any user may read, '-' otherwise.
|
|
63
|
|
64 8 'w' if any user may write, '-' otherwise.
|
|
65
|
|
66 9 'x' if any user may execute, 't' if the file is "sticky"
|
|
67 (will be retained in swap space after execution), '-'
|
|
68 otherwise.
|
|
69 'T' if the file is sticky but not executable. */
|
|
70
|
|
71 void
|
|
72 filemodestring (struct stat *statp, char *str)
|
|
73 {
|
|
74 mode_string (statp->st_mode, str);
|
|
75 }
|
|
76
|
|
77 /* Like filemodestring, but only the relevant part of the `struct stat'
|
|
78 is given as an argument. */
|
|
79
|
|
80 static void
|
|
81 mode_string (unsigned short mode, char *str)
|
|
82 {
|
|
83 str[0] = ftypelet (mode);
|
404
|
84 rwx ((unsigned short) ((mode & 0700) << 0), &str[1]);
|
|
85 rwx ((unsigned short) ((mode & 0070) << 3), &str[4]);
|
|
86 rwx ((unsigned short) ((mode & 0007) << 6), &str[7]);
|
0
|
87 setst (mode, str);
|
|
88 }
|
|
89
|
|
90 /* Return a character indicating the type of file described by
|
|
91 file mode BITS:
|
|
92 'd' for directories
|
|
93 'b' for block special files
|
|
94 'c' for character special files
|
|
95 'm' for multiplexor files
|
|
96 'l' for symbolic links
|
|
97 's' for sockets
|
|
98 'p' for fifos
|
|
99 '-' for regular files
|
|
100 '?' for any other file type. */
|
|
101
|
|
102 static char
|
|
103 ftypelet (mode_t bits)
|
|
104 {
|
|
105 #ifdef S_ISBLK
|
|
106 if (S_ISBLK (bits))
|
|
107 return 'b';
|
|
108 #endif
|
|
109 if (S_ISCHR (bits))
|
|
110 return 'c';
|
|
111 if (S_ISDIR (bits))
|
|
112 return 'd';
|
|
113 if (S_ISREG (bits))
|
|
114 return '-';
|
|
115 #ifdef S_ISFIFO
|
|
116 if (S_ISFIFO (bits))
|
|
117 return 'p';
|
|
118 #endif
|
|
119 #ifdef S_ISLNK
|
|
120 if (S_ISLNK (bits))
|
|
121 return 'l';
|
|
122 #endif
|
|
123 #ifdef S_ISSOCK
|
|
124 if (S_ISSOCK (bits))
|
|
125 return 's';
|
|
126 #endif
|
|
127 #ifdef S_ISMPC
|
|
128 if (S_ISMPC (bits))
|
|
129 return 'm';
|
|
130 #endif
|
|
131 #ifdef S_ISNWK
|
|
132 if (S_ISNWK (bits))
|
|
133 return 'n';
|
|
134 #endif
|
|
135 return '?';
|
|
136 }
|
|
137
|
|
138 /* Look at read, write, and execute bits in BITS and set
|
|
139 flags in CHARS accordingly. */
|
|
140
|
|
141 static void
|
|
142 rwx (unsigned short bits, char *chars)
|
|
143 {
|
|
144 chars[0] = (bits & S_IRUSR) ? 'r' : '-';
|
|
145 chars[1] = (bits & S_IWUSR) ? 'w' : '-';
|
|
146 chars[2] = (bits & S_IXUSR) ? 'x' : '-';
|
|
147 }
|
|
148
|
|
149 /* Set the 's' and 't' flags in file attributes string CHARS,
|
|
150 according to the file mode BITS. */
|
|
151
|
|
152 static void
|
2286
|
153 setst (
|
|
154 #if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
|
|
155 unsigned short bits, char *chars
|
|
156 #else
|
|
157 unsigned short UNUSED (bits), char *UNUSED (chars)
|
|
158 #endif
|
|
159 )
|
0
|
160 {
|
|
161 #ifdef S_ISUID
|
|
162 if (bits & S_ISUID)
|
|
163 {
|
|
164 if (chars[3] != 'x')
|
|
165 /* Set-uid, but not executable by owner. */
|
|
166 chars[3] = 'S';
|
|
167 else
|
|
168 chars[3] = 's';
|
|
169 }
|
|
170 #endif
|
|
171 #ifdef S_ISGID
|
|
172 if (bits & S_ISGID)
|
|
173 {
|
|
174 if (chars[6] != 'x')
|
|
175 /* Set-gid, but not executable by group. */
|
|
176 chars[6] = 'S';
|
|
177 else
|
|
178 chars[6] = 's';
|
|
179 }
|
|
180 #endif
|
|
181 #ifdef S_ISVTX
|
|
182 if (bits & S_ISVTX)
|
|
183 {
|
|
184 if (chars[9] != 'x')
|
|
185 /* Sticky, but not executable by others. */
|
|
186 chars[9] = 'T';
|
|
187 else
|
|
188 chars[9] = 't';
|
|
189 }
|
|
190 #endif
|
|
191 }
|