(ftypelet): Add comments for Cray DMF support.
[pspp] / lib / filemode.c
1 /* filemode.c -- make a string describing file modes
2    Copyright (C) 1985, 1990, 1993, 1998 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17 \f
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #if !S_IRUSR
26 # if S_IREAD
27 #  define S_IRUSR S_IREAD
28 # else
29 #  define S_IRUSR 00400
30 # endif
31 #endif
32
33 #if !S_IWUSR
34 # if S_IWRITE
35 #  define S_IWUSR S_IWRITE
36 # else
37 #  define S_IWUSR 00200
38 # endif
39 #endif
40
41 #if !S_IXUSR
42 # if S_IEXEC
43 #  define S_IXUSR S_IEXEC
44 # else
45 #  define S_IXUSR 00100
46 # endif
47 #endif
48
49 #ifdef STAT_MACROS_BROKEN
50 # undef S_ISBLK
51 # undef S_ISCHR
52 # undef S_ISDIR
53 # undef S_ISFIFO
54 # undef S_ISLNK
55 # undef S_ISMPB
56 # undef S_ISMPC
57 # undef S_ISNWK
58 # undef S_ISREG
59 # undef S_ISSOCK
60 #endif /* STAT_MACROS_BROKEN.  */
61
62 #if !defined(S_ISBLK) && defined(S_IFBLK)
63 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
64 #endif
65 #if !defined(S_ISCHR) && defined(S_IFCHR)
66 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
67 #endif
68 #if !defined(S_ISDIR) && defined(S_IFDIR)
69 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
70 #endif
71 #if !defined(S_ISREG) && defined(S_IFREG)
72 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
73 #endif
74 #if !defined(S_ISFIFO) && defined(S_IFIFO)
75 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
76 #endif
77 #if !defined(S_ISLNK) && defined(S_IFLNK)
78 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
79 #endif
80 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
81 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
82 #endif
83 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
84 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
85 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
86 #endif
87 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
88 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
89 #endif
90
91 void mode_string ();
92 static char ftypelet ();
93 static void rwx ();
94 static void setst ();
95
96 /* filemodestring - fill in string STR with an ls-style ASCII
97    representation of the st_mode field of file stats block STATP.
98    10 characters are stored in STR; no terminating null is added.
99    The characters stored in STR are:
100
101    0    File type.  'd' for directory, 'c' for character
102         special, 'b' for block special, 'm' for multiplex,
103         'l' for symbolic link, 's' for socket, 'p' for fifo,
104         '-' for regular, '?' for any other file type
105
106    1    'r' if the owner may read, '-' otherwise.
107
108    2    'w' if the owner may write, '-' otherwise.
109
110    3    'x' if the owner may execute, 's' if the file is
111         set-user-id, '-' otherwise.
112         'S' if the file is set-user-id, but the execute
113         bit isn't set.
114
115    4    'r' if group members may read, '-' otherwise.
116
117    5    'w' if group members may write, '-' otherwise.
118
119    6    'x' if group members may execute, 's' if the file is
120         set-group-id, '-' otherwise.
121         'S' if it is set-group-id but not executable.
122
123    7    'r' if any user may read, '-' otherwise.
124
125    8    'w' if any user may write, '-' otherwise.
126
127    9    'x' if any user may execute, 't' if the file is "sticky"
128         (will be retained in swap space after execution), '-'
129         otherwise.
130         'T' if the file is sticky but not executable.  */
131
132 void
133 filemodestring (statp, str)
134      struct stat *statp;
135      char *str;
136 {
137   mode_string (statp->st_mode, str);
138 }
139
140 /* Like filemodestring, but only the relevant part of the `struct stat'
141    is given as an argument.  */
142
143 void
144 mode_string (mode, str)
145      unsigned short mode;
146      char *str;
147 {
148   str[0] = ftypelet ((long) mode);
149   rwx ((mode & 0700) << 0, &str[1]);
150   rwx ((mode & 0070) << 3, &str[4]);
151   rwx ((mode & 0007) << 6, &str[7]);
152   setst (mode, str);
153 }
154
155 /* Return a character indicating the type of file described by
156    file mode BITS:
157    'd' for directories
158    'b' for block special files
159    'c' for character special files
160    'm' for multiplexor files
161    'M' for an off-line (regular) file
162    'l' for symbolic links
163    's' for sockets
164    'p' for fifos
165    '-' for regular files
166    '?' for any other file type.  */
167
168 static char
169 ftypelet (bits)
170      long bits;
171 {
172 #ifdef S_ISBLK
173   if (S_ISBLK (bits))
174     return 'b';
175 #endif
176   if (S_ISCHR (bits))
177     return 'c';
178   if (S_ISDIR (bits))
179     return 'd';
180   if (S_ISREG (bits))
181     return '-';
182 #ifdef S_ISFIFO
183   if (S_ISFIFO (bits))
184     return 'p';
185 #endif
186 #ifdef S_ISLNK
187   if (S_ISLNK (bits))
188     return 'l';
189 #endif
190 #ifdef S_ISSOCK
191   if (S_ISSOCK (bits))
192     return 's';
193 #endif
194 #ifdef S_ISMPC
195   if (S_ISMPC (bits))
196     return 'm';
197 #endif
198 #ifdef S_ISNWK
199   if (S_ISNWK (bits))
200     return 'n';
201 #endif
202
203   /* The following two tests are for Cray DMF (Data Migration
204      Facility), which is a HSM file system.  A migrated file has a
205      `st_dm_mode' that is different from the normal `st_mode', so any
206      tests for migrated files should use the former.  */
207
208 #ifdef S_ISOFD
209   if (S_ISOFD (bits))
210     /* off line, with data  */
211     return 'M';
212 #endif
213 #ifdef S_ISOFL
214   /* off line, with no data  */
215   if (S_ISOFL (bits))
216     return 'M';
217 #endif
218   return '?';
219 }
220
221 /* Look at read, write, and execute bits in BITS and set
222    flags in CHARS accordingly.  */
223
224 static void
225 rwx (bits, chars)
226      unsigned short bits;
227      char *chars;
228 {
229   chars[0] = (bits & S_IRUSR) ? 'r' : '-';
230   chars[1] = (bits & S_IWUSR) ? 'w' : '-';
231   chars[2] = (bits & S_IXUSR) ? 'x' : '-';
232 }
233
234 /* Set the 's' and 't' flags in file attributes string CHARS,
235    according to the file mode BITS.  */
236
237 static void
238 setst (bits, chars)
239      unsigned short bits;
240      char *chars;
241 {
242 #ifdef S_ISUID
243   if (bits & S_ISUID)
244     {
245       if (chars[3] != 'x')
246         /* Set-uid, but not executable by owner.  */
247         chars[3] = 'S';
248       else
249         chars[3] = 's';
250     }
251 #endif
252 #ifdef S_ISGID
253   if (bits & S_ISGID)
254     {
255       if (chars[6] != 'x')
256         /* Set-gid, but not executable by group.  */
257         chars[6] = 'S';
258       else
259         chars[6] = 's';
260     }
261 #endif
262 #ifdef S_ISVTX
263   if (bits & S_ISVTX)
264     {
265       if (chars[9] != 'x')
266         /* Sticky, but not executable by others.  */
267         chars[9] = 'T';
268       else
269         chars[9] = 't';
270     }
271 #endif
272 }