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