1 /* filemode.c -- make a string describing file modes
2 Copyright 1985, 1990, 1991, 1994, 1995, 1997, 1999, 2002, 2003, 2005,
3 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
24 static char ftypelet (unsigned long);
25 static void setst (unsigned long, char *);
27 /* filemodestring - fill in string STR with an ls-style ASCII
28 representation of the st_mode field of file stats block STATP.
29 10 characters are stored in STR; no terminating null is added.
30 The characters stored in STR are:
32 0 File type. 'd' for directory, 'c' for character
33 special, 'b' for block special, 'm' for multiplex,
34 'l' for symbolic link, 's' for socket, 'p' for fifo,
35 '-' for any other file type
37 1 'r' if the owner may read, '-' otherwise.
39 2 'w' if the owner may write, '-' otherwise.
41 3 'x' if the owner may execute, 's' if the file is
42 set-user-id, '-' otherwise.
43 'S' if the file is set-user-id, but the execute
46 4 'r' if group members may read, '-' otherwise.
48 5 'w' if group members may write, '-' otherwise.
50 6 'x' if group members may execute, 's' if the file is
51 set-group-id, '-' otherwise.
52 'S' if it is set-group-id but not executable.
54 7 'r' if any user may read, '-' otherwise.
56 8 'w' if any user may write, '-' otherwise.
58 9 'x' if any user may execute, 't' if the file is "sticky"
59 (will be retained in swap space after execution), '-'
61 'T' if the file is sticky but not executable. */
63 /* Get definitions for the file permission bits. */
104 /* Like filemodestring, but only the relevant part of the `struct stat'
105 is given as an argument. */
108 mode_string (unsigned long mode
, char *str
)
110 str
[0] = ftypelet ((unsigned long) mode
);
111 str
[1] = (mode
& S_IRUSR
) != 0 ? 'r' : '-';
112 str
[2] = (mode
& S_IWUSR
) != 0 ? 'w' : '-';
113 str
[3] = (mode
& S_IXUSR
) != 0 ? 'x' : '-';
114 str
[4] = (mode
& S_IRGRP
) != 0 ? 'r' : '-';
115 str
[5] = (mode
& S_IWGRP
) != 0 ? 'w' : '-';
116 str
[6] = (mode
& S_IXGRP
) != 0 ? 'x' : '-';
117 str
[7] = (mode
& S_IROTH
) != 0 ? 'r' : '-';
118 str
[8] = (mode
& S_IWOTH
) != 0 ? 'w' : '-';
119 str
[9] = (mode
& S_IXOTH
) != 0 ? 'x' : '-';
120 setst ((unsigned long) mode
, str
);
123 /* Return a character indicating the type of file described by
126 'b' for block special files
127 'c' for character special files
128 'm' for multiplexer files
129 'l' for symbolic links
132 '-' for any other file type. */
136 #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
137 #else /* ! defined (S_IFDIR) */
138 #define S_ISDIR(i) (((i) & 0170000) == 040000)
139 #endif /* ! defined (S_IFDIR) */
140 #endif /* ! defined (S_ISDIR) */
144 #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
145 #else /* ! defined (S_IFBLK) */
147 #endif /* ! defined (S_IFBLK) */
148 #endif /* ! defined (S_ISBLK) */
152 #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
153 #else /* ! defined (S_IFCHR) */
155 #endif /* ! defined (S_IFCHR) */
156 #endif /* ! defined (S_ISCHR) */
160 #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
161 #else /* ! defined (S_IFIFO) */
162 #define S_ISFIFO(i) 0
163 #endif /* ! defined (S_IFIFO) */
164 #endif /* ! defined (S_ISFIFO) */
168 #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
169 #else /* ! defined (S_IFSOCK) */
170 #define S_ISSOCK(i) 0
171 #endif /* ! defined (S_IFSOCK) */
172 #endif /* ! defined (S_ISSOCK) */
176 #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
177 #else /* ! defined (S_IFLNK) */
179 #endif /* ! defined (S_IFLNK) */
180 #endif /* ! defined (S_ISLNK) */
183 ftypelet (unsigned long bits
)
200 if ((bits
& S_IFMT
) == S_IFMPC
201 || (bits
& S_IFMT
) == S_IFMPB
)
205 if ((bits
& S_IFMT
) == S_IFNWK
)
213 /* Set the 's' and 't' flags in file attributes string CHARS,
214 according to the file mode BITS. */
217 setst (unsigned long bits ATTRIBUTE_UNUSED
, char *chars ATTRIBUTE_UNUSED
)
223 /* Set-uid, but not executable by owner. */
233 /* Set-gid, but not executable by group. */
243 /* Sticky, but not executable by others. */