| 1 | /* filemode.c -- make a string describing file modes
|
|---|
| 2 | Copyright (C) 1985, 1990, 1993, 2004 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
|
|---|
| 15 | License along with this program; if not, write to the Free
|
|---|
| 16 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|---|
| 17 | Boston, MA 02110-1301 USA. */
|
|---|
| 18 | |
|---|
| 19 |
|
|---|
| 20 | #ifdef HAVE_CONFIG_H
|
|---|
| 21 | #include <config.h>
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #include <sys/types.h>
|
|---|
| 25 | #include <sys/stat.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 | #ifdef STAT_MACROS_BROKEN
|
|---|
| 52 | #undef S_ISBLK
|
|---|
| 53 | #undef S_ISCHR
|
|---|
| 54 | #undef S_ISDIR
|
|---|
| 55 | #undef S_ISFIFO
|
|---|
| 56 | #undef S_ISLNK
|
|---|
| 57 | #undef S_ISMPB
|
|---|
| 58 | #undef S_ISMPC
|
|---|
| 59 | #undef S_ISNWK
|
|---|
| 60 | #undef S_ISREG
|
|---|
| 61 | #undef S_ISSOCK
|
|---|
| 62 | #endif /* STAT_MACROS_BROKEN. */
|
|---|
| 63 |
|
|---|
| 64 | #if !defined(S_ISBLK) && defined(S_IFBLK)
|
|---|
| 65 | #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
|
|---|
| 66 | #endif
|
|---|
| 67 | #if !defined(S_ISCHR) && defined(S_IFCHR)
|
|---|
| 68 | #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
|
|---|
| 69 | #endif
|
|---|
| 70 | #if !defined(S_ISDIR) && defined(S_IFDIR)
|
|---|
| 71 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
|---|
| 72 | #endif
|
|---|
| 73 | #if !defined(S_ISREG) && defined(S_IFREG)
|
|---|
| 74 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
|---|
| 75 | #endif
|
|---|
| 76 | #if !defined(S_ISFIFO) && defined(S_IFIFO)
|
|---|
| 77 | #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
|
|---|
| 78 | #endif
|
|---|
| 79 | #if !defined(S_ISLNK) && defined(S_IFLNK)
|
|---|
| 80 | #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
|
|---|
| 81 | #endif
|
|---|
| 82 | #if !defined(S_ISSOCK) && defined(S_IFSOCK)
|
|---|
| 83 | #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
|
|---|
| 84 | #endif
|
|---|
| 85 | #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
|
|---|
| 86 | #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
|
|---|
| 87 | #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
|
|---|
| 88 | #endif
|
|---|
| 89 | #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
|
|---|
| 90 | #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
|
|---|
| 91 | #endif
|
|---|
| 92 |
|
|---|
| 93 | /* Return a character indicating the type of file described by
|
|---|
| 94 | file mode BITS:
|
|---|
| 95 | 'd' for directories
|
|---|
| 96 | 'b' for block special files
|
|---|
| 97 | 'c' for character special files
|
|---|
| 98 | 'm' for multiplexor files
|
|---|
| 99 | 'l' for symbolic links
|
|---|
| 100 | 's' for sockets
|
|---|
| 101 | 'p' for fifos
|
|---|
| 102 | '-' for regular files
|
|---|
| 103 | '?' for any other file type. */
|
|---|
| 104 |
|
|---|
| 105 | static char
|
|---|
| 106 | ftypelet (long bits)
|
|---|
| 107 | {
|
|---|
| 108 | #ifdef S_ISBLK
|
|---|
| 109 | if (S_ISBLK (bits))
|
|---|
| 110 | return 'b';
|
|---|
| 111 | #endif
|
|---|
| 112 | if (S_ISCHR (bits))
|
|---|
| 113 | return 'c';
|
|---|
| 114 | if (S_ISDIR (bits))
|
|---|
| 115 | return 'd';
|
|---|
| 116 | if (S_ISREG (bits))
|
|---|
| 117 | return '-';
|
|---|
| 118 | #ifdef S_ISFIFO
|
|---|
| 119 | if (S_ISFIFO (bits))
|
|---|
| 120 | return 'p';
|
|---|
| 121 | #endif
|
|---|
| 122 | #ifdef S_ISLNK
|
|---|
| 123 | if (S_ISLNK (bits))
|
|---|
| 124 | return 'l';
|
|---|
| 125 | #endif
|
|---|
| 126 | #ifdef S_ISSOCK
|
|---|
| 127 | if (S_ISSOCK (bits))
|
|---|
| 128 | return 's';
|
|---|
| 129 | #endif
|
|---|
| 130 | #ifdef S_ISMPC
|
|---|
| 131 | if (S_ISMPC (bits))
|
|---|
| 132 | return 'm';
|
|---|
| 133 | #endif
|
|---|
| 134 | #ifdef S_ISNWK
|
|---|
| 135 | if (S_ISNWK (bits))
|
|---|
| 136 | return 'n';
|
|---|
| 137 | #endif
|
|---|
| 138 | return '?';
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /* Look at read, write, and execute bits in BITS and set
|
|---|
| 142 | flags in CHARS accordingly. */
|
|---|
| 143 |
|
|---|
| 144 | static void
|
|---|
| 145 | rwx (unsigned short bits, char *chars)
|
|---|
| 146 | {
|
|---|
| 147 | chars[0] = (bits & S_IRUSR) ? 'r' : '-';
|
|---|
| 148 | chars[1] = (bits & S_IWUSR) ? 'w' : '-';
|
|---|
| 149 | chars[2] = (bits & S_IXUSR) ? 'x' : '-';
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* Set the 's' and 't' flags in file attributes string CHARS,
|
|---|
| 153 | according to the file mode BITS. */
|
|---|
| 154 |
|
|---|
| 155 | static void
|
|---|
| 156 | setst (unsigned short bits, char *chars)
|
|---|
| 157 | {
|
|---|
| 158 | #ifdef S_ISUID
|
|---|
| 159 | if (bits & S_ISUID)
|
|---|
| 160 | {
|
|---|
| 161 | if (chars[3] != 'x')
|
|---|
| 162 | /* Set-uid, but not executable by owner. */
|
|---|
| 163 | chars[3] = 'S';
|
|---|
| 164 | else
|
|---|
| 165 | chars[3] = 's';
|
|---|
| 166 | }
|
|---|
| 167 | #endif
|
|---|
| 168 | #ifdef S_ISGID
|
|---|
| 169 | if (bits & S_ISGID)
|
|---|
| 170 | {
|
|---|
| 171 | if (chars[6] != 'x')
|
|---|
| 172 | /* Set-gid, but not executable by group. */
|
|---|
| 173 | chars[6] = 'S';
|
|---|
| 174 | else
|
|---|
| 175 | chars[6] = 's';
|
|---|
| 176 | }
|
|---|
| 177 | #endif
|
|---|
| 178 | #ifdef S_ISVTX
|
|---|
| 179 | if (bits & S_ISVTX)
|
|---|
| 180 | {
|
|---|
| 181 | if (chars[9] != 'x')
|
|---|
| 182 | /* Sticky, but not executable by others. */
|
|---|
| 183 | chars[9] = 'T';
|
|---|
| 184 | else
|
|---|
| 185 | chars[9] = 't';
|
|---|
| 186 | }
|
|---|
| 187 | #endif
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /* Like filemodestring (see below), but only the relevant part of the
|
|---|
| 191 | `struct stat' is given as an argument. */
|
|---|
| 192 |
|
|---|
| 193 | void
|
|---|
| 194 | mode_string (unsigned short mode, char *str)
|
|---|
| 195 | {
|
|---|
| 196 | str[0] = ftypelet ((long) mode);
|
|---|
| 197 | rwx ((mode & 0700) << 0, &str[1]);
|
|---|
| 198 | rwx ((mode & 0070) << 3, &str[4]);
|
|---|
| 199 | rwx ((mode & 0007) << 6, &str[7]);
|
|---|
| 200 | setst (mode, str);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /* filemodestring - fill in string STR with an ls-style ASCII
|
|---|
| 204 | representation of the st_mode field of file stats block STATP.
|
|---|
| 205 | 10 characters are stored in STR; no terminating null is added.
|
|---|
| 206 | The characters stored in STR are:
|
|---|
| 207 |
|
|---|
| 208 | 0 File type. 'd' for directory, 'c' for character
|
|---|
| 209 | special, 'b' for block special, 'm' for multiplex,
|
|---|
| 210 | 'l' for symbolic link, 's' for socket, 'p' for fifo,
|
|---|
| 211 | '-' for regular, '?' for any other file type
|
|---|
| 212 |
|
|---|
| 213 | 1 'r' if the owner may read, '-' otherwise.
|
|---|
| 214 |
|
|---|
| 215 | 2 'w' if the owner may write, '-' otherwise.
|
|---|
| 216 |
|
|---|
| 217 | 3 'x' if the owner may execute, 's' if the file is
|
|---|
| 218 | set-user-id, '-' otherwise.
|
|---|
| 219 | 'S' if the file is set-user-id, but the execute
|
|---|
| 220 | bit isn't set.
|
|---|
| 221 |
|
|---|
| 222 | 4 'r' if group members may read, '-' otherwise.
|
|---|
| 223 |
|
|---|
| 224 | 5 'w' if group members may write, '-' otherwise.
|
|---|
| 225 |
|
|---|
| 226 | 6 'x' if group members may execute, 's' if the file is
|
|---|
| 227 | set-group-id, '-' otherwise.
|
|---|
| 228 | 'S' if it is set-group-id but not executable.
|
|---|
| 229 |
|
|---|
| 230 | 7 'r' if any user may read, '-' otherwise.
|
|---|
| 231 |
|
|---|
| 232 | 8 'w' if any user may write, '-' otherwise.
|
|---|
| 233 |
|
|---|
| 234 | 9 'x' if any user may execute, 't' if the file is "sticky"
|
|---|
| 235 | (will be retained in swap space after execution), '-'
|
|---|
| 236 | otherwise.
|
|---|
| 237 | 'T' if the file is sticky but not executable. */
|
|---|
| 238 |
|
|---|
| 239 | void
|
|---|
| 240 | filemodestring (struct stat *statp, char *str)
|
|---|
| 241 | {
|
|---|
| 242 | mode_string (statp->st_mode, str);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|