]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/getfacl.c
* getfacl.c (username): New function.
[newlib-cygwin.git] / winsup / utils / getfacl.c
1 /* getfacl.c
2
3 Copyright 2000, 2001 Red Hat Inc.
4
5 Written by Corinna Vinschen <vinschen@redhat.com>
6
7 This file is part of Cygwin.
8
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 details. */
12
13 #include <pwd.h>
14 #include <grp.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <getopt.h>
18 #include <sys/types.h>
19 #include <sys/acl.h>
20 #include <sys/stat.h>
21
22 char *
23 permstr (mode_t perm)
24 {
25 static char pbuf[4];
26
27 pbuf[0] = (perm & S_IREAD) ? 'r' : '-';
28 pbuf[1] = (perm & S_IWRITE) ? 'w' : '-';
29 pbuf[2] = (perm & S_IEXEC) ? 'x' : '-';
30 pbuf[3] = '\0';
31 return pbuf;
32 }
33
34 const char *
35 username (uid_t uid)
36 {
37 static char ubuf[256];
38 struct passwd *pw;
39
40 if ((pw = getpwuid (uid)))
41 strcpy (ubuf, pw->pw_name);
42 else
43 sprintf (ubuf, "%d <unknown>", uid);
44 return ubuf;
45 }
46
47 const char *
48 groupname (gid_t gid)
49 {
50 static char gbuf[256];
51 struct group *gr;
52
53 if ((gr = getgrgid (gid)))
54 strcpy (gbuf, gr->gr_name);
55 else
56 sprintf (gbuf, "%d <unknown>", gid);
57 return gbuf;
58 }
59
60 #define pn(txt) fprintf (fp, txt "\n", name)
61 #define p(txt) fprintf (fp, txt "\n")
62
63 int
64 usage (const char *name, int help)
65 {
66 FILE *fp = help ? stdout : stderr;
67
68 pn ("usage: %s [-adn] file...");
69 if (!help)
70 pn ("Try `%s --help' for more information.");
71 else
72 {
73 p ("");
74 p ("Display file and directory access control lists (ACLs).");
75 p ("");
76 p ("For each argument that is a regular file, special file or");
77 p ("directory, getfacl displays the owner, the group, and the ACL.");
78 p ("For directories getfacl displays additionally the default ACL.");
79 p ("");
80 p ("With no options specified, getfacl displays the filename, the");
81 p ("owner, the group, and both the ACL and the default ACL, if it");
82 p ("exists.");
83 p ("");
84 p ("The following options are supported:");
85 p ("");
86 p ("-a Display the filename, the owner, the group, and the ACL");
87 p (" of the file.");
88 p ("");
89 p ("-d Display the filename, the owner, the group, and the default");
90 p (" ACL of the directory, if it exists.");
91 p ("");
92 p ("-n Display user and group IDs instead of names.");
93 p ("");
94 p ("The format for ACL output is as follows:");
95 p (" # file: filename");
96 p (" # owner: name or uid");
97 p (" # group: name or uid");
98 p (" user::perm");
99 p (" user:name or uid:perm");
100 p (" group::perm");
101 p (" group:name or gid:perm");
102 p (" mask:perm");
103 p (" other:perm");
104 p (" default:user::perm");
105 p (" default:user:name or uid:perm");
106 p (" default:group::perm");
107 p (" default:group:name or gid:perm");
108 p (" default:mask:perm");
109 p (" default:other:perm");
110 p ("");
111 p ("When multiple files are specified on the command line, a blank");
112 p ("line separates the ACLs for each file.");
113 }
114 return 1;
115 }
116
117 struct option longopts[] = {
118 {"help", no_argument, NULL, 'h'},
119 {0, no_argument, NULL, 0}
120 };
121
122 int
123 main (int argc, char **argv)
124 {
125 extern int optind;
126 int c, i;
127 int aopt = 0;
128 int dopt = 0;
129 int nopt = 0;
130 int first = 1;
131 struct stat st;
132 aclent_t acls[MAX_ACL_ENTRIES];
133
134 while ((c = getopt_long (argc, argv, "adn", longopts, NULL)) != EOF)
135 switch (c)
136 {
137 case 'a':
138 aopt = 1;
139 break;
140 case 'd':
141 dopt = 1;
142 break;
143 case 'n':
144 nopt = 1;
145 break;
146 case 'h':
147 return usage (argv[0], 1);
148 default:
149 return usage (argv[0], 0);
150 }
151 if (optind > argc - 1)
152 return usage (argv[0], 0);
153 while ((c = optind++) < argc)
154 {
155 if (stat (argv[c], &st))
156 {
157 perror (argv[0]);
158 continue;
159 }
160 if (!first)
161 putchar ('\n');
162 first = 0;
163 printf ("# file: %s\n", argv[c]);
164 if (nopt)
165 {
166 printf ("# owner: %d\n", st.st_uid);
167 printf ("# group: %d\n", st.st_gid);
168 }
169 else
170 {
171 printf ("# owner: %s\n", username (st.st_uid));
172 printf ("# group: %s\n", groupname (st.st_gid));
173 }
174 if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
175 {
176 perror (argv[0]);
177 continue;
178 }
179 for (i = 0; i < c; ++i)
180 {
181 if (acls[i].a_type & ACL_DEFAULT)
182 {
183 if (aopt)
184 continue;
185 printf ("default:");
186 }
187 else if (dopt)
188 continue;
189 switch (acls[i].a_type & ~ACL_DEFAULT)
190 {
191 case USER_OBJ:
192 printf ("user::");
193 break;
194 case USER:
195 if (nopt)
196 printf ("user:%d:", acls[i].a_id);
197 else
198 printf ("user:%s:", username (acls[i].a_id));
199 break;
200 case GROUP_OBJ:
201 printf ("group::");
202 break;
203 case GROUP:
204 if (nopt)
205 printf ("group:%d:", acls[i].a_id);
206 else
207 printf ("group:%s:", groupname (acls[i].a_id));
208 break;
209 case CLASS_OBJ:
210 printf ("mask::");
211 break;
212 case OTHER_OBJ:
213 printf ("other::");
214 break;
215 }
216 printf ("%s\n", permstr (acls[i].a_perm));
217 }
218 }
219 return 0;
220 }
This page took 0.048096 seconds and 6 git commands to generate.