1 /* filemode.c -- make a string describing file modes
2 Copyright (C) 1985, 1990, 1993, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 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 02110-1301,
24 #include <sys/types.h>
29 # define S_IRUSR S_IREAD
31 # define S_IRUSR 00400
37 # define S_IWUSR S_IWRITE
39 # define S_IWUSR 00200
45 # define S_IXUSR S_IEXEC
47 # define S_IXUSR 00100
51 #ifdef STAT_MACROS_BROKEN
62 #endif /* STAT_MACROS_BROKEN. */
64 #if !defined(S_ISBLK) && defined(S_IFBLK)
65 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
67 #if !defined(S_ISCHR) && defined(S_IFCHR)
68 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
70 #if !defined(S_ISDIR) && defined(S_IFDIR)
71 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73 #if !defined(S_ISREG) && defined(S_IFREG)
74 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76 #if !defined(S_ISFIFO) && defined(S_IFIFO)
77 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
79 #if !defined(S_ISLNK) && defined(S_IFLNK)
80 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
82 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
83 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
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)
89 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
90 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
93 void mode_string (short unsigned int mode
, char *str
);
94 static char ftypelet (long int bits
);
95 static void rwx (short unsigned int bits
, char *chars
);
96 static void setst (short unsigned int bits
, char *chars
);
98 /* filemodestring - fill in string STR with an ls-style ASCII
99 representation of the st_mode field of file stats block STATP.
100 10 characters are stored in STR; no terminating null is added.
101 The characters stored in STR are:
103 0 File type. 'd' for directory, 'c' for character
104 special, 'b' for block special, 'm' for multiplex,
105 'l' for symbolic link, 's' for socket, 'p' for fifo,
106 '-' for regular, '?' for any other file type
108 1 'r' if the owner may read, '-' otherwise.
110 2 'w' if the owner may write, '-' otherwise.
112 3 'x' if the owner may execute, 's' if the file is
113 set-user-id, '-' otherwise.
114 'S' if the file is set-user-id, but the execute
117 4 'r' if group members may read, '-' otherwise.
119 5 'w' if group members may write, '-' otherwise.
121 6 'x' if group members may execute, 's' if the file is
122 set-group-id, '-' otherwise.
123 'S' if it is set-group-id but not executable.
125 7 'r' if any user may read, '-' otherwise.
127 8 'w' if any user may write, '-' otherwise.
129 9 'x' if any user may execute, 't' if the file is "sticky"
130 (will be retained in swap space after execution), '-'
132 'T' if the file is sticky but not executable. */
135 filemodestring (struct stat
*statp
, char *str
)
137 mode_string (statp
->st_mode
, str
);
140 /* Like filemodestring, but only the relevant part of the `struct stat'
141 is given as an argument. */
144 mode_string (short unsigned int mode
, char *str
)
146 str
[0] = ftypelet ((long) mode
);
147 rwx ((mode
& 0700) << 0, &str
[1]);
148 rwx ((mode
& 0070) << 3, &str
[4]);
149 rwx ((mode
& 0007) << 6, &str
[7]);
153 /* Return a character indicating the type of file described by
156 'b' for block special files
157 'c' for character special files
158 'm' for multiplexor files
159 'l' for symbolic links
162 '-' for regular files
163 '?' for any other file type. */
166 ftypelet (long int bits
)
201 /* Look at read, write, and execute bits in BITS and set
202 flags in CHARS accordingly. */
205 rwx (short unsigned int bits
, char *chars
)
207 chars
[0] = (bits
& S_IRUSR
) ? 'r' : '-';
208 chars
[1] = (bits
& S_IWUSR
) ? 'w' : '-';
209 chars
[2] = (bits
& S_IXUSR
) ? 'x' : '-';
212 /* Set the 's' and 't' flags in file attributes string CHARS,
213 according to the file mode BITS. */
216 setst (short unsigned int bits
, char *chars
)
222 /* Set-uid, but not executable by owner. */
232 /* Set-gid, but not executable by group. */
242 /* Sticky, but not executable by others. */
250 /* arch-tag: 4340830c-15a5-47d2-b45f-1d43c45a91bb
251 (do not change this comment) */