1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990 Free Software Foundation, Inc.
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)
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.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include <sys/types.h>
21 #define S_IREAD S_IRUSR
22 #define S_IWRITE S_IWUSR
23 #define S_IEXEC S_IXUSR
25 #ifndef S_ISREG /* Doesn't have POSIX.1 stat stuff. */
26 #define mode_t unsigned short
28 #if !defined(S_ISBLK) && defined(S_IFBLK)
29 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
31 #if !defined(S_ISCHR) && defined(S_IFCHR)
32 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
34 #if !defined(S_ISDIR) && defined(S_IFDIR)
35 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
37 #if !defined(S_ISREG) && defined(S_IFREG)
38 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
40 #if !defined(S_ISFIFO) && defined(S_IFIFO)
41 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
43 #if !defined(S_ISLNK) && defined(S_IFLNK)
44 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
46 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
47 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
49 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
50 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
51 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
53 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
54 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
58 static char ftypelet ();
62 /* filemodestring - fill in string STR with an ls-style ASCII
63 representation of the st_mode field of file stats block STATP.
64 10 characters are stored in STR; no terminating null is added.
65 The characters stored in STR are:
67 0 File type. 'd' for directory, 'c' for character
68 special, 'b' for block special, 'm' for multiplex,
69 'l' for symbolic link, 's' for socket, 'p' for fifo,
70 '-' for regular, '?' for any other file type
72 1 'r' if the owner may read, '-' otherwise.
74 2 'w' if the owner may write, '-' otherwise.
76 3 'x' if the owner may execute, 's' if the file is
77 set-user-id, '-' otherwise.
78 'S' if the file is set-user-id, but the execute
81 4 'r' if group members may read, '-' otherwise.
83 5 'w' if group members may write, '-' otherwise.
85 6 'x' if group members may execute, 's' if the file is
86 set-group-id, '-' otherwise.
87 'S' if it is set-group-id but not executable.
89 7 'r' if any user may read, '-' otherwise.
91 8 'w' if any user may write, '-' otherwise.
93 9 'x' if any user may execute, 't' if the file is "sticky"
94 (will be retained in swap space after execution), '-'
96 'T' if the file is sticky but not executable. */
99 filemodestring (statp
, str
)
103 mode_string (statp
->st_mode
, str
);
106 /* Like filemodestring, but only the relevant part of the `struct stat'
107 is given as an argument. */
110 mode_string (mode
, str
)
114 str
[0] = ftypelet (mode
);
115 rwx ((mode
& 0700) << 0, &str
[1]);
116 rwx ((mode
& 0070) << 3, &str
[4]);
117 rwx ((mode
& 0007) << 6, &str
[7]);
121 /* Return a character indicating the type of file described by
124 'b' for block special files
125 'c' for character special files
126 'm' for multiplexor files
127 'l' for symbolic links
130 '-' for regular files
131 '?' for any other file type. */
170 /* Look at read, write, and execute bits in BITS and set
171 flags in CHARS accordingly. */
178 chars
[0] = (bits
& S_IREAD
) ? 'r' : '-';
179 chars
[1] = (bits
& S_IWRITE
) ? 'w' : '-';
180 chars
[2] = (bits
& S_IEXEC
) ? 'x' : '-';
183 /* Set the 's' and 't' flags in file attributes string CHARS,
184 according to the file mode BITS. */
195 /* Set-uid, but not executable by owner. */
205 /* Set-gid, but not executable by group. */
215 /* Sticky, but not executable by others. */