1 /* filemode.c -- make a string describing file modes
2 Copyright 1985, 1990, 1991, 1994, 1995, 1997, 2003
3 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 2, 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., 59 Temple Place - Suite 330, Boston, MA
23 static char ftypelet (unsigned long);
24 static void setst (unsigned long, char *);
26 /* filemodestring - fill in string STR with an ls-style ASCII
27 representation of the st_mode field of file stats block STATP.
28 10 characters are stored in STR; no terminating null is added.
29 The characters stored in STR are:
31 0 File type. 'd' for directory, 'c' for character
32 special, 'b' for block special, 'm' for multiplex,
33 'l' for symbolic link, 's' for socket, 'p' for fifo,
34 '-' for any other file type
36 1 'r' if the owner may read, '-' otherwise.
38 2 'w' if the owner may write, '-' otherwise.
40 3 'x' if the owner may execute, 's' if the file is
41 set-user-id, '-' otherwise.
42 'S' if the file is set-user-id, but the execute
45 4 'r' if group members may read, '-' otherwise.
47 5 'w' if group members may write, '-' otherwise.
49 6 'x' if group members may execute, 's' if the file is
50 set-group-id, '-' otherwise.
51 'S' if it is set-group-id but not executable.
53 7 'r' if any user may read, '-' otherwise.
55 8 'w' if any user may write, '-' otherwise.
57 9 'x' if any user may execute, 't' if the file is "sticky"
58 (will be retained in swap space after execution), '-'
60 'T' if the file is sticky but not executable. */
64 /* This is not used; only mode_string is used. */
67 filemodestring (struct stat
*statp
, char *str
)
69 mode_string ((unsigned long) statp
->st_mode
, str
);
74 /* Get definitions for the file permission bits. */
115 /* Like filemodestring, but only the relevant part of the `struct stat'
116 is given as an argument. */
119 mode_string (unsigned long mode
, char *str
)
121 str
[0] = ftypelet ((unsigned long) mode
);
122 str
[1] = (mode
& S_IRUSR
) != 0 ? 'r' : '-';
123 str
[2] = (mode
& S_IWUSR
) != 0 ? 'w' : '-';
124 str
[3] = (mode
& S_IXUSR
) != 0 ? 'x' : '-';
125 str
[4] = (mode
& S_IRGRP
) != 0 ? 'r' : '-';
126 str
[5] = (mode
& S_IWGRP
) != 0 ? 'w' : '-';
127 str
[6] = (mode
& S_IXGRP
) != 0 ? 'x' : '-';
128 str
[7] = (mode
& S_IROTH
) != 0 ? 'r' : '-';
129 str
[8] = (mode
& S_IWOTH
) != 0 ? 'w' : '-';
130 str
[9] = (mode
& S_IXOTH
) != 0 ? 'x' : '-';
131 setst ((unsigned long) mode
, str
);
134 /* Return a character indicating the type of file described by
137 'b' for block special files
138 'c' for character special files
139 'm' for multiplexer files
140 'l' for symbolic links
143 '-' for any other file type. */
147 #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
148 #else /* ! defined (S_IFDIR) */
149 #define S_ISDIR(i) (((i) & 0170000) == 040000)
150 #endif /* ! defined (S_IFDIR) */
151 #endif /* ! defined (S_ISDIR) */
155 #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
156 #else /* ! defined (S_IFBLK) */
158 #endif /* ! defined (S_IFBLK) */
159 #endif /* ! defined (S_ISBLK) */
163 #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
164 #else /* ! defined (S_IFCHR) */
166 #endif /* ! defined (S_IFCHR) */
167 #endif /* ! defined (S_ISCHR) */
171 #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
172 #else /* ! defined (S_IFIFO) */
173 #define S_ISFIFO(i) 0
174 #endif /* ! defined (S_IFIFO) */
175 #endif /* ! defined (S_ISFIFO) */
179 #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
180 #else /* ! defined (S_IFSOCK) */
181 #define S_ISSOCK(i) 0
182 #endif /* ! defined (S_IFSOCK) */
183 #endif /* ! defined (S_ISSOCK) */
187 #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
188 #else /* ! defined (S_IFLNK) */
190 #endif /* ! defined (S_IFLNK) */
191 #endif /* ! defined (S_ISLNK) */
194 ftypelet (unsigned long bits
)
211 if ((bits
& S_IFMT
) == S_IFMPC
212 || (bits
& S_IFMT
) == S_IFMPB
)
216 if ((bits
& S_IFMT
) == S_IFNWK
)
224 /* Set the 's' and 't' flags in file attributes string CHARS,
225 according to the file mode BITS. */
228 setst (unsigned long bits ATTRIBUTE_UNUSED
, char *chars ATTRIBUTE_UNUSED
)
234 /* Set-uid, but not executable by owner. */
244 /* Set-gid, but not executable by group. */
254 /* Sticky, but not executable by others. */