source: trunk/essentials/sys-apps/findutils/gnulib/lib/filemode.c

Last change on this file was 3170, checked in by bird, 18 years ago

findutils 4.3.2

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