* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / filemode.c
1 /* filemode.c -- make a string describing file modes
2
3    Copyright (C) 1985, 1990, 1993, 1998-2000, 2004, 2006 Free Software
4    Foundation, Inc.
5
6    This program 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 2, or (at your option)
9    any later version.
10
11    This program 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, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #include "filemode.h"
23
24 #include "stat-macros.h"
25
26 /* The following is for Cray DMF (Data Migration Facility), which is a
27    HSM file system.  A migrated file has a `st_dm_mode' that is
28    different from the normal `st_mode', so any tests for migrated
29    files should use the former.  */
30 #if HAVE_ST_DM_MODE
31 # define IS_MIGRATED_FILE(statp) \
32     (S_ISOFD (statp->st_dm_mode) || S_ISOFL (statp->st_dm_mode))
33 #else
34 # define IS_MIGRATED_FILE(statp) 0
35 #endif
36
37 #if ! HAVE_DECL_STRMODE
38
39 /* Return a character indicating the type of file described by
40    file mode BITS:
41    '-' regular file
42    'b' block special file
43    'c' character special file
44    'C' high performance ("contiguous data") file
45    'd' directory
46    'D' door
47    'l' symbolic link
48    'm' multiplexed file (7th edition Unix; obsolete)
49    'n' network special file (HP-UX)
50    'p' fifo (named pipe)
51    'P' port
52    's' socket
53    'w' whiteout (4.4BSD)
54    '?' some other file type  */
55
56 static char
57 ftypelet (mode_t bits)
58 {
59   /* These are the most common, so test for them first.  */
60   if (S_ISREG (bits))
61     return '-';
62   if (S_ISDIR (bits))
63     return 'd';
64
65   /* Other letters standardized by POSIX 1003.1-2004.  */
66   if (S_ISBLK (bits))
67     return 'b';
68   if (S_ISCHR (bits))
69     return 'c';
70   if (S_ISLNK (bits))
71     return 'l';
72   if (S_ISFIFO (bits))
73     return 'p';
74
75   /* Other file types (though not letters) standardized by POSIX.  */
76   if (S_ISSOCK (bits))
77     return 's';
78
79   /* Nonstandard file types.  */
80   if (S_ISCTG (bits))
81     return 'C';
82   if (S_ISDOOR (bits))
83     return 'D';
84   if (S_ISMPB (bits) || S_ISMPC (bits))
85     return 'm';
86   if (S_ISNWK (bits))
87     return 'n';
88   if (S_ISPORT (bits))
89     return 'P';
90   if (S_ISWHT (bits))
91     return 'w';
92
93   return '?';
94 }
95
96 /* Like filemodestring, but rely only on MODE.  */
97
98 void
99 strmode (mode_t mode, char *str)
100 {
101   str[0] = ftypelet (mode);
102   str[1] = mode & S_IRUSR ? 'r' : '-';
103   str[2] = mode & S_IWUSR ? 'w' : '-';
104   str[3] = (mode & S_ISUID
105             ? (mode & S_IXUSR ? 's' : 'S')
106             : (mode & S_IXUSR ? 'x' : '-'));
107   str[4] = mode & S_IRGRP ? 'r' : '-';
108   str[5] = mode & S_IWGRP ? 'w' : '-';
109   str[6] = (mode & S_ISGID
110             ? (mode & S_IXGRP ? 's' : 'S')
111             : (mode & S_IXGRP ? 'x' : '-'));
112   str[7] = mode & S_IROTH ? 'r' : '-';
113   str[8] = mode & S_IWOTH ? 'w' : '-';
114   str[9] = (mode & S_ISVTX
115             ? (mode & S_IXOTH ? 't' : 'T')
116             : (mode & S_IXOTH ? 'x' : '-'));
117   str[10] = ' ';
118   str[11] = '\0';
119 }
120
121 #endif /* ! HAVE_DECL_STRMODE */
122
123 /* filemodestring - fill in string STR with an ls-style ASCII
124    representation of the st_mode field of file stats block STATP.
125    12 characters are stored in STR.
126    The characters stored in STR are:
127
128    0    File type, as in ftypelet above, except that other letters are used
129         for files whose type cannot be determined solely from st_mode:
130
131             'F' semaphore
132             'M' migrated file (Cray DMF)
133             'Q' message queue
134             'S' shared memory object
135             'T' typed memory object
136
137    1    'r' if the owner may read, '-' otherwise.
138
139    2    'w' if the owner may write, '-' otherwise.
140
141    3    'x' if the owner may execute, 's' if the file is
142         set-user-id, '-' otherwise.
143         'S' if the file is set-user-id, but the execute
144         bit isn't set.
145
146    4    'r' if group members may read, '-' otherwise.
147
148    5    'w' if group members may write, '-' otherwise.
149
150    6    'x' if group members may execute, 's' if the file is
151         set-group-id, '-' otherwise.
152         'S' if it is set-group-id but not executable.
153
154    7    'r' if any user may read, '-' otherwise.
155
156    8    'w' if any user may write, '-' otherwise.
157
158    9    'x' if any user may execute, 't' if the file is "sticky"
159         (will be retained in swap space after execution), '-'
160         otherwise.
161         'T' if the file is sticky but not executable.
162
163    10   ' ' for compatibility with 4.4BSD strmode,
164         since this interface does not support ACLs.
165
166    11   '\0'.  */
167
168 void
169 filemodestring (struct stat const *statp, char *str)
170 {
171   strmode (statp->st_mode, str);
172
173   if (S_TYPEISSEM (statp))
174     str[0] = 'F';
175   else if (IS_MIGRATED_FILE (statp))
176     str[0] = 'M';
177   else if (S_TYPEISMQ (statp))
178     str[0] = 'Q';
179   else if (S_TYPEISSHM (statp))
180     str[0] = 'S';
181   else if (S_TYPEISTMO (statp))
182     str[0] = 'T';
183 }