File: getpermit.c

package info (click to toggle)
tiger 2.2.4-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,188 kB
  • ctags: 240
  • sloc: sh: 8,388; ansic: 2,109; makefile: 141; perl: 106
file content (247 lines) | stat: -rw-r--r-- 4,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>

#ifdef __STDC__
void showperms(char *);
void putusrname(int);
void putgrpname(int);
#else
void showperms();
void putusrname();
void putgrpname();
#endif

#ifndef S_IRUSR
#define S_IRUSR 00400 
#endif

#ifndef S_IWUSR
#define S_IWUSR 00200
#endif

#ifndef S_IXUSR
#define S_IXUSR 00100
#endif

#ifndef S_IRGRP
#define S_IRGRP 00040
#endif

#ifndef S_IWGRP
#define S_IWGRP 00020
#endif

#ifndef S_IXGRP
#define S_IXGRP 00010
#endif

#ifndef S_IROTH
#define S_IROTH 00004
#endif

#ifndef S_IWOTH
#define S_IWOTH 00002
#endif

#ifndef S_IXOTH
#define S_IXOTH 00001
#endif

#ifndef S_ISUID
#define S_ISUID 04000
#endif

#ifndef S_ISGID
#define S_ISGID 02000
#endif

#ifndef S_ISVTX
#define S_ISVTX 01000
#endif


#ifdef __STDC__
int
main(int argc, char **argv)
#else
int main(argc, argv)
int argc;
char **argv;
#endif
{
     if(argc == 1){
	  char buf[1025];
	  
	  while(fgets(buf, 1024, stdin)){
	       buf[strlen(buf)-1] = 0;
	       showperms(buf);
	  }
     }
     else {
	  for(argv++;*argv;argv++)
	       showperms(*argv);
     }
}

#define outmode(m, b) {if((m)&(b))putchar('1');else putchar('0');putchar(' ');}

#ifdef __STDC__
void
showperms(char *fname)
#else
void
showperms(fname)
char *fname;
#endif
{
     struct stat sbuf;

     if(stat(fname, &sbuf) != -1){
	  fputs(fname, stdout);
	  putchar(' ');
	  putusrname(sbuf.st_uid);
	  putchar(' ');
	  putgrpname(sbuf.st_gid);
	  putchar(' ');

	  outmode(sbuf.st_mode, S_IRUSR);
	  outmode(sbuf.st_mode, S_IWUSR);
	  outmode(sbuf.st_mode, S_IXUSR);

	  outmode(sbuf.st_mode, S_IRGRP);
	  outmode(sbuf.st_mode, S_IWGRP);
	  outmode(sbuf.st_mode, S_IXGRP);

	  outmode(sbuf.st_mode, S_IROTH);
	  outmode(sbuf.st_mode, S_IWOTH);
	  outmode(sbuf.st_mode, S_IXOTH);

	  outmode(sbuf.st_mode, S_ISUID);
	  outmode(sbuf.st_mode, S_ISGID);
#ifdef S_ISVTX	  
	  outmode(sbuf.st_mode, S_ISVTX);
#else
	  putchar('0');
	  putchar(' ');
#endif
	  putchar('\n');
     }
     else {
	  perror(fname);
     }
}

struct gidcache {
     char *name;
     gid_t gid;
};

struct gidcache gcache[8192];

static int gc_init = 0;

#ifdef __STDC__
char *
cgetnamgid(int gid)
#else
char *
cgetnamgid(gid)
int gid;
#endif
{
     struct group *grent;
     int hash = gid & 8191;

     if(!gc_init){
	  (void)memset((void *)gcache, 0, sizeof(gcache));
	  gc_init = 1;
     }

     if(gcache[hash].name && gcache[hash].gid == gid){
	  return gcache[hash].name;
     }
     else if((grent = getgrgid(gid))){
	  if(!gcache[hash].name)
	       gcache[hash].name = (char *)malloc(9);
	  (void)strcpy(gcache[hash].name, grent->gr_name);
	  gcache[hash].gid = gid;
     }
     else {
	  if(!gcache[hash].name)
	       gcache[hash].name = (char *)malloc(9);
	  (void)sprintf(gcache[hash].name, "%8lu", (unsigned long)gid);
	  gcache[hash].gid = gid;
     }
     return gcache[hash].name;
}

struct uidcache {
     char *name;
     uid_t uid;
};

struct uidcache ucache[8192];

static int uc_init = 0;

#ifdef __STDC__
char *
cgetnamuid(int uid)
#else
char *
cgetnamuid(uid)
int uid;
#endif
{
     struct passwd *pwent;
     int hash = uid & 8191;

     if(!uc_init){
	  (void)memset((void *)ucache, 0, sizeof(ucache));
	  uc_init = 1;
     }

     if(ucache[hash].name && ucache[hash].uid == uid){
	  return ucache[hash].name;
     }
     else if((pwent = getpwuid(uid))){
	  if(!ucache[hash].name)
	       ucache[hash].name = (char *)malloc(9);
	  (void)strcpy(ucache[hash].name, pwent->pw_name);
	  ucache[hash].uid = uid;
     }
     else {
	  if(!ucache[hash].name)
	       ucache[hash].name = (char *)malloc(9);
	  (void)sprintf(ucache[hash].name, "%8lu", (unsigned long)uid);
	  ucache[hash].uid = uid;
     }
     return ucache[hash].name;
}

#ifdef __STDC__
void
putusrname(int uid)
#else
void
putusrname(uid)
int uid;
#endif
{
     fputs(cgetnamuid(uid), stdout);
}

#ifdef __STDC__
void
putgrpname(int gid)
#else
void
putgrpname(gid)
int gid;
#endif
{
     fputs(cgetnamgid(gid), stdout);
}