source: trunk/third/findutils/lib/filemode.c @ 18890

Revision 18890, 6.7 KB checked in by zacheiss, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18889, which included commits to RCS files with non-trunk default branches.
Line 
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
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#if !S_IRGRP
52# define S_IRGRP (S_IRUSR >> 3)
53#endif
54#if !S_IWGRP
55# define S_IWGRP (S_IWUSR >> 3)
56#endif
57#if !S_IXGRP
58# define S_IXGRP (S_IXUSR >> 3)
59#endif
60#if !S_IROTH
61# define S_IROTH (S_IRUSR >> 6)
62#endif
63#if !S_IWOTH
64# define S_IWOTH (S_IWUSR >> 6)
65#endif
66#if !S_IXOTH
67# define S_IXOTH (S_IXUSR >> 6)
68#endif
69
70#ifdef STAT_MACROS_BROKEN
71# undef S_ISBLK
72# undef S_ISCHR
73# undef S_ISDIR
74# undef S_ISFIFO
75# undef S_ISLNK
76# undef S_ISMPB
77# undef S_ISMPC
78# undef S_ISNWK
79# undef S_ISREG
80# undef S_ISSOCK
81# undef S_ISDOOR
82#endif /* STAT_MACROS_BROKEN.  */
83
84#if !defined(S_ISBLK) && defined(S_IFBLK)
85# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
86#endif
87#if !defined(S_ISCHR) && defined(S_IFCHR)
88# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
89#endif
90#if !defined(S_ISDIR) && defined(S_IFDIR)
91# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
92#endif
93#if !defined(S_ISREG) && defined(S_IFREG)
94# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
95#endif
96#if !defined(S_ISFIFO) && defined(S_IFIFO)
97# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
98#endif
99#if !defined(S_ISLNK) && defined(S_IFLNK)
100# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
101#endif
102#if !defined(S_ISSOCK) && defined(S_IFSOCK)
103# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
104#endif
105#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
106# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
107# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
108#endif
109#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
110# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
111#endif
112#if !defined(S_ISDOOR) && defined(S_IFDOOR) /* Solaris 2.5 and up */
113# define S_ISDOOR(m) (((m) & S_IFMT) == S_IFDOOR)
114#endif
115
116/* Set the 's' and 't' flags in file attributes string CHARS,
117   according to the file mode BITS.  */
118
119static void
120setst (mode_t bits, char *chars)
121{
122#ifdef S_ISUID
123  if (bits & S_ISUID)
124    {
125      if (chars[3] != 'x')
126        /* Set-uid, but not executable by owner.  */
127        chars[3] = 'S';
128      else
129        chars[3] = 's';
130    }
131#endif
132#ifdef S_ISGID
133  if (bits & S_ISGID)
134    {
135      if (chars[6] != 'x')
136        /* Set-gid, but not executable by group.  */
137        chars[6] = 'S';
138      else
139        chars[6] = 's';
140    }
141#endif
142#ifdef S_ISVTX
143  if (bits & S_ISVTX)
144    {
145      if (chars[9] != 'x')
146        /* Sticky, but not executable by others.  */
147        chars[9] = 'T';
148      else
149        chars[9] = 't';
150    }
151#endif
152}
153
154/* Return a character indicating the type of file described by
155   file mode BITS:
156   'd' for directories
157   'D' for doors
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
168static char
169ftypelet (mode_t bits)
170{
171#ifdef S_ISBLK
172  if (S_ISBLK (bits))
173    return 'b';
174#endif
175  if (S_ISCHR (bits))
176    return 'c';
177  if (S_ISDIR (bits))
178    return 'd';
179  if (S_ISREG (bits))
180    return '-';
181#ifdef S_ISFIFO
182  if (S_ISFIFO (bits))
183    return 'p';
184#endif
185#ifdef S_ISLNK
186  if (S_ISLNK (bits))
187    return 'l';
188#endif
189#ifdef S_ISSOCK
190  if (S_ISSOCK (bits))
191    return 's';
192#endif
193#ifdef S_ISMPC
194  if (S_ISMPC (bits))
195    return 'm';
196#endif
197#ifdef S_ISNWK
198  if (S_ISNWK (bits))
199    return 'n';
200#endif
201#ifdef S_ISDOOR
202  if (S_ISDOOR (bits))
203    return 'D';
204#endif
205
206  /* The following two tests are for Cray DMF (Data Migration
207     Facility), which is a HSM file system.  A migrated file has a
208     `st_dm_mode' that is different from the normal `st_mode', so any
209     tests for migrated files should use the former.  */
210
211#ifdef S_ISOFD
212  if (S_ISOFD (bits))
213    /* off line, with data  */
214    return 'M';
215#endif
216#ifdef S_ISOFL
217  /* off line, with no data  */
218  if (S_ISOFL (bits))
219    return 'M';
220#endif
221  return '?';
222}
223
224/* Like filemodestring, but only the relevant part of the `struct stat'
225   is given as an argument.  */
226
227void
228mode_string (mode_t mode, char *str)
229{
230  str[0] = ftypelet (mode);
231  str[1] = mode & S_IRUSR ? 'r' : '-';
232  str[2] = mode & S_IWUSR ? 'w' : '-';
233  str[3] = mode & S_IXUSR ? 'x' : '-';
234  str[4] = mode & S_IRGRP ? 'r' : '-';
235  str[5] = mode & S_IWGRP ? 'w' : '-';
236  str[6] = mode & S_IXGRP ? 'x' : '-';
237  str[7] = mode & S_IROTH ? 'r' : '-';
238  str[8] = mode & S_IWOTH ? 'w' : '-';
239  str[9] = mode & S_IXOTH ? 'x' : '-';
240  setst (mode, str);
241}
242
243/* filemodestring - fill in string STR with an ls-style ASCII
244   representation of the st_mode field of file stats block STATP.
245   10 characters are stored in STR; no terminating null is added.
246   The characters stored in STR are:
247
248   0    File type.  'd' for directory, 'c' for character
249        special, 'b' for block special, 'm' for multiplex,
250        'l' for symbolic link, 's' for socket, 'p' for fifo,
251        '-' for regular, '?' for any other file type
252
253   1    'r' if the owner may read, '-' otherwise.
254
255   2    'w' if the owner may write, '-' otherwise.
256
257   3    'x' if the owner may execute, 's' if the file is
258        set-user-id, '-' otherwise.
259        'S' if the file is set-user-id, but the execute
260        bit isn't set.
261
262   4    'r' if group members may read, '-' otherwise.
263
264   5    'w' if group members may write, '-' otherwise.
265
266   6    'x' if group members may execute, 's' if the file is
267        set-group-id, '-' otherwise.
268        'S' if it is set-group-id but not executable.
269
270   7    'r' if any user may read, '-' otherwise.
271
272   8    'w' if any user may write, '-' otherwise.
273
274   9    'x' if any user may execute, 't' if the file is "sticky"
275        (will be retained in swap space after execution), '-'
276        otherwise.
277        'T' if the file is sticky but not executable.  */
278
279void
280filemodestring (struct stat *statp, char *str)
281{
282  mode_string (statp->st_mode, str);
283}
Note: See TracBrowser for help on using the repository browser.