|
|
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: 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 1, 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 License
15: along with this program; if not, write to the Free Software
16: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17:
18: In other words, you are welcome to use, share and improve this program.
19: You are forbidden to forbid anyone else to use, share and improve
20: what you give them. Help stamp out software-hoarding! */
21:
22:
23: #include <sys/types.h>
24: #include <sys/stat.h>
25: #include "filetypes.h"
26:
27: /* filemodestring - set file attribute data
28:
29: *** WARNING! FILE STRUCTURE DEPENDENT ***
30:
31: Filemodestring converts the data in the st_mode field of file status
32: block `s' to a 10 character attribute string, which it stores in
33: the block that `a' points to.
34: This attribute string is modelled after the string produced by the Berkeley ls.
35:
36: As usual under Unix, the elements of the string are numbered
37: from 0. Their meanings are:
38:
39: 0 File type. 'd' for directory, 'c' for character
40: special, 'b' for block special, 'm' for multiplex,
41: 'l' for symbolic link, 's' for socket, 'p' for fifo,
42: '-' for any other file type
43:
44: 1 'r' if the owner may read, '-' otherwise.
45:
46: 2 'w' if the owner may write, '-' otherwise.
47:
48: 3 'x' if the owner may execute, 's' if the file is
49: set-user-id, '-' otherwise.
50: 'S' if the file is set-user-id, but the execute
51: bit isn't set. (sys v `feature' which helps to
52: catch screw case.)
53:
54: 4 'r' if group members may read, '-' otherwise.
55:
56: 5 'w' if group members may write, '-' otherwise.
57:
58: 6 'x' if group members may execute, 's' if the file is
59: set-group-id, '-' otherwise.
60: 'S' if it is set-group-id but not executable.
61:
62: 7 'r' if any user may read, '-' otherwise.
63:
64: 8 'w' if any user may write, '-' otherwise.
65:
66: 9 'x' if any user may execute, 't' if the file is "sticky"
67: (will be retained in swap space after execution), '-'
68: otherwise.
69:
70: */
71:
72: #define VOID void
73:
74: static char ftypelet ();
75: static VOID rwx (), setst ();
76:
77: VOID
78: filemodestring (s,a)
79: struct stat *s;
80: char *a;
81: {
82: a[0] = ftypelet (s);
83: /* Aren't there symbolic names for these byte-fields? */
84: rwx ((s->st_mode & 0700) << 0, &(a[1]));
85: rwx ((s->st_mode & 0070) << 3, &(a[4]));
86: rwx ((s->st_mode & 0007) << 6, &(a[7]));
87: setst (s->st_mode, a);
88: }
89:
90: /* ftypelet - file type letter
91:
92: *** WARNING! FILE STRUCTURE DEPENDENT ***
93:
94: Ftypelet accepts a file status block and returns a character
95: code describing the type of the file. 'd' is returned for
96: directories, 'b' for block special files, 'c' for character
97: special files, 'm' for multiplexor files, 'l' for symbolic link,
98: 's' for socket, 'p' for fifo, '-' for any other file type
99: */
100:
101: static char
102: ftypelet(s)
103: struct stat *s;
104: {
105: switch (s->st_mode & S_IFMT)
106: {
107: default:
108: return '-';
109: case S_IFDIR:
110: return 'd';
111: #ifdef S_IFLNK
112: case S_IFLNK:
113: return 'l';
114: #endif
115: #ifdef S_IFCHR
116: case S_IFCHR:
117: return 'c';
118: #endif
119: #ifdef S_IFBLK
120: case S_IFBLK:
121: return 'b';
122: #endif
123: #ifdef S_IFMPC
124: /* These do not seem to exist */
125: case S_IFMPC:
126: case S_IFMPB:
127: return 'm';
128: #endif
129: #ifdef S_IFSOCK
130: case S_IFSOCK:
131: return 's';
132: #endif
133: #ifdef S_IFIFO
134: #if S_IFIFO != S_IFSOCK
135: case S_IFIFO:
136: return 'p';
137: #endif
138: #endif
139: #ifdef S_IFNWK /* hp-ux hack */
140: case S_IFNWK:
141: return 'n';
142: #endif
143: }
144: }
145:
146:
147: /* rwx - look at read, write, and execute bits and set character
148: flags accordingly
149:
150: *** WARNING! FILE STRUCTURE DEPENDENT ***
151:
152: */
153:
154: static VOID
155: rwx (bits, chars)
156: unsigned short bits;
157: char chars[];
158: {
159: chars[0] = (bits & S_IREAD) ? 'r' : '-';
160: chars[1] = (bits & S_IWRITE) ? 'w' : '-';
161: chars[2] = (bits & S_IEXEC) ? 'x' : '-';
162: }
163:
164:
165: /* setst - set s & t flags in a file attributes string */
166: /* *** WARNING! FILE STRUCTURE DEPENDENT *** */
167: static VOID
168: setst (bits, chars)
169: unsigned short bits;
170: char chars[];
171: {
172: #ifdef S_ISUID
173: if (bits & S_ISUID)
174: {
175: if (chars[3] != 'x')
176: /* Screw case: set-uid, but not executable. */
177: chars[3] = 'S';
178: else
179: chars[3] = 's';
180: }
181: #endif
182: #ifdef S_ISGID
183: if (bits & S_ISGID)
184: {
185: if (chars[6] != 'x')
186: /* Screw case: set-gid, but not executable. */
187: chars[6] = 'S';
188: else
189: chars[6] = 's';
190: }
191: #endif
192: #ifdef S_ISVTX
193: if (bits & S_ISVTX)
194: chars[9] = 't';
195: #endif
196: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.