|
|
1.1 root 1: /* Examine the result of stat and make a string describing file modes.
2: Copyright (C) 1985 Free Software Foundation, Inc.
3:
4: NO WARRANTY
5:
6: BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
7: NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
8: WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
9: RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
10: WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
11: BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
12: FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
13: AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
14: DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
15: CORRECTION.
16:
17: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
18: STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
19: WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
20: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
21: OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
22: USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
23: DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
24: A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
25: PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26: DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
27:
28: GENERAL PUBLIC LICENSE TO COPY
29:
30: 1. You may copy and distribute verbatim copies of this source file
31: as you receive it, in any medium, provided that you conspicuously and
32: appropriately publish on each copy a valid copyright notice "Copyright
33: (C) 1985 Free Software Foundation, Inc."; and include following the
34: copyright notice a verbatim copy of the above disclaimer of warranty
35: and of this License. You may charge a distribution fee for the
36: physical act of transferring a copy.
37:
38: 2. You may modify your copy or copies of this source file or
39: any portion of it, and copy and distribute such modifications under
40: the terms of Paragraph 1 above, provided that you also do the following:
41:
42: a) cause the modified files to carry prominent notices stating
43: that you changed the files and the date of any change; and
44:
45: b) cause the whole of any work that you distribute or publish,
46: that in whole or in part contains or is a derivative of this
47: program or any part thereof, to be licensed at no charge to all
48: third parties on terms identical to those contained in this
49: License Agreement (except that you may choose to grant more extensive
50: warranty protection to some or all third parties, at your option).
51:
52: c) You may charge a distribution fee for the physical act of
53: transferring a copy, and you may at your option offer warranty
54: protection in exchange for a fee.
55:
56: Mere aggregation of another unrelated program with this program (or its
57: derivative) on a volume of a storage or distribution medium does not bring
58: the other program under the scope of these terms.
59:
60: 3. You may copy and distribute this program (or a portion or derivative
61: of it, under Paragraph 2) in object code or executable form under the terms
62: of Paragraphs 1 and 2 above provided that you also do one of the following:
63:
64: a) accompany it with the complete corresponding machine-readable
65: source code, which must be distributed under the terms of
66: Paragraphs 1 and 2 above; or,
67:
68: b) accompany it with a written offer, valid for at least three
69: years, to give any third party free (except for a nominal
70: shipping charge) a complete machine-readable copy of the
71: corresponding source code, to be distributed under the terms of
72: Paragraphs 1 and 2 above; or,
73:
74: c) accompany it with the information you received as to where the
75: corresponding source code may be obtained. (This alternative is
76: allowed only for noncommercial distribution and only if you
77: received the program in object code or executable form alone.)
78:
79: For an executable file, complete source code means all the source code for
80: all modules it contains; but, as a special exception, it need not include
81: source code for modules which are standard libraries that accompany the
82: operating system on which the executable file runs.
83:
84: 4. You may not copy, sublicense, distribute or transfer this program
85: except as expressly provided under this License Agreement. Any attempt
86: otherwise to copy, sublicense, distribute or transfer this program is void and
87: your rights to use the program under this License agreement shall be
88: automatically terminated. However, parties who have received computer
89: software programs from you with this License Agreement will not have
90: their licenses terminated so long as such parties remain in full compliance.
91:
92: 5. If you wish to incorporate parts of this program into other free
93: programs whose distribution conditions are different, write to the Free
94: Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
95: worked out a simple rule that can be stated here, but we will often permit
96: this. We will be guided by the two goals of preserving the free status of
97: all derivatives of our free software and of promoting the sharing and reuse of
98: software.
99:
100:
101: In other words, you are welcome to use, share and improve this program.
102: You are forbidden to forbid anyone else to use, share and improve
103: what you give them. Help stamp out software-hoarding! */
104:
105:
106: #include <sys/types.h>
107: #include <sys/stat.h>
108:
109: /* filemodestring - set file attribute data
110:
111: *** WARNING! FILE STRUCTURE DEPENDENT ***
112:
113: Filemodestring converts the data in the st_mode field of file status
114: block `s' to a 10 character attribute string, which it stores in
115: the block that `a' points to.
116: This attribute string is modelled after the string produced by the Berkeley ls.
117:
118: As usual under Unix, the elements of the string are numbered
119: from 0. Their meanings are:
120:
121: 0 File type. 'd' for directory, 'c' for character
122: special, 'b' for block special, 'm' for multiplex,
123: 'l' for symbolic link, 's' for socket, 'p' for fifo,
124: '-' for any other file type
125:
126: 1 'r' if the owner may read, '-' otherwise.
127:
128: 2 'w' if the owner may write, '-' otherwise.
129:
130: 3 'x' if the owner may execute, 's' if the file is
131: set-user-id, '-' otherwise.
132: 'S' if the file is set-user-id, but the execute
133: bit isn't set. (sys v `feature' which helps to
134: catch screw case.)
135:
136: 4 'r' if group members may read, '-' otherwise.
137:
138: 5 'w' if group members may write, '-' otherwise.
139:
140: 6 'x' if group members may execute, 's' if the file is
141: set-group-id, '-' otherwise.
142: 'S' if it is set-group-id but not executable.
143:
144: 7 'r' if any user may read, '-' otherwise.
145:
146: 8 'w' if any user may write, '-' otherwise.
147:
148: 9 'x' if any user may execute, 't' if the file is "sticky"
149: (will be retained in swap space after execution), '-'
150: otherwise.
151:
152: */
153:
154: #define VOID void
155:
156: static char ftypelet ();
157: static VOID rwx (), setst ();
158:
159: VOID
160: filemodestring (s,a)
161: struct stat *s;
162: char *a;
163: {
164: a[0] = ftypelet (s);
165: /* Aren't there symbolic names for these byte-fields? */
166: rwx ((s->st_mode & 0700) << 0, &(a[1]));
167: rwx ((s->st_mode & 0070) << 3, &(a[4]));
168: rwx ((s->st_mode & 0007) << 6, &(a[7]));
169: setst (s->st_mode, a);
170: }
171:
172: /* ftypelet - file type letter
173:
174: *** WARNING! FILE STRUCTURE DEPENDENT ***
175:
176: Ftypelet accepts a file status block and returns a character
177: code describing the type of the file. 'd' is returned for
178: directories, 'b' for block special files, 'c' for character
179: special files, 'm' for multiplexor files, 'l' for symbolic link,
180: 's' for socket, 'p' for fifo, '-' for any other file type
181: */
182:
183: static char
184: ftypelet(s)
185: struct stat *s;
186: {
187: switch (s->st_mode & S_IFMT)
188: {
189: default:
190: return '-';
191: case S_IFDIR:
192: return 'd';
193: #ifdef S_IFLNK
194: case S_IFLNK:
195: return 'l';
196: #endif
197: #ifdef S_IFCHR
198: case S_IFCHR:
199: return 'c';
200: #endif
201: #ifdef S_IFBLK
202: case S_IFBLK:
203: return 'b';
204: #endif
205: #ifdef S_IFMPC
206: /* These do not seem to exist */
207: case S_IFMPC:
208: case S_IFMPB:
209: return 'm';
210: #endif
211: #ifdef S_IFSOCK
212: case S_IFSOCK:
213: return 's';
214: #endif
215: #ifdef S_IFIFO
216: #if S_IFIFO != S_IFSOCK
217: case S_IFIFO:
218: return 'p';
219: #endif
220: #endif
221: #ifdef S_IFNWK /* hp-ux hack */
222: case S_IFNWK:
223: return 'n';
224: #endif
225: }
226: }
227:
228:
229: /* rwx - look at read, write, and execute bits and set character
230: flags accordingly
231:
232: *** WARNING! FILE STRUCTURE DEPENDENT ***
233:
234: */
235:
236: static VOID
237: rwx (bits, chars)
238: unsigned short bits;
239: char chars[];
240: {
241: chars[0] = (bits & S_IREAD) ? 'r' : '-';
242: chars[1] = (bits & S_IWRITE) ? 'w' : '-';
243: chars[2] = (bits & S_IEXEC) ? 'x' : '-';
244: }
245:
246:
247: /* setst - set s & t flags in a file attributes string */
248: /* *** WARNING! FILE STRUCTURE DEPENDENT *** */
249: static VOID
250: setst (bits, chars)
251: unsigned short bits;
252: char chars[];
253: {
254: #ifdef S_ISUID
255: if (bits & S_ISUID)
256: {
257: if (chars[3] != 'x')
258: /* Screw case: set-uid, but not executable. */
259: chars[3] = 'S';
260: else
261: chars[3] = 's';
262: }
263: #endif
264: #ifdef S_ISGID
265: if (bits & S_ISGID)
266: {
267: if (chars[6] != 'x')
268: /* Screw case: set-gid, but not executable. */
269: chars[6] = 'S';
270: else
271: chars[6] = 's';
272: }
273: #endif
274: #ifdef S_ISVTX
275: if (bits & S_ISVTX)
276: chars[9] = 't';
277: #endif
278: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.