]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/getfacl.c
* getfacl.c (username): Fix ambiguous printf calls.
[newlib-cygwin.git] / winsup / utils / getfacl.c
1 /* getfacl.c
2
3 Copyright 2000, 2001, 2002 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 #include <string.h>
22
23 static const char version[] = "$Revision$";
24 static char *prog_name;
25
26 char *
27 permstr (mode_t perm)
28 {
29 static char pbuf[4];
30
31 pbuf[0] = (perm & S_IROTH) ? 'r' : '-';
32 pbuf[1] = (perm & S_IWOTH) ? 'w' : '-';
33 pbuf[2] = (perm & S_IXOTH) ? 'x' : '-';
34 pbuf[3] = '\0';
35 return pbuf;
36 }
37
38 const char *
39 username (uid_t uid)
40 {
41 static char ubuf[256];
42 struct passwd *pw;
43
44 if ((pw = getpwuid (uid)))
45 strcpy (ubuf, pw->pw_name);
46 else
47 sprintf (ubuf, "%lu <unknown>", (unsigned long)uid);
48 return ubuf;
49 }
50
51 const char *
52 groupname (gid_t gid)
53 {
54 static char gbuf[256];
55 struct group *gr;
56
57 if ((gr = getgrgid (gid)))
58 strcpy (gbuf, gr->gr_name);
59 else
60 sprintf (gbuf, "%lu <unknown>", (unsigned long)gid);
61 return gbuf;
62 }
63
64 static void
65 usage (FILE * stream)
66 {
67 fprintf (stream, "Usage: %s [-adn] FILE [FILE2...]\n"
68 "Display file and directory access control lists (ACLs).\n"
69 "\n"
70 " -a, --all display the filename, the owner, the group, and\n"
71 " the ACL of the file\n"
72 " -d, --dir display the filename, the owner, the group, and\n"
73 " the default ACL of the directory, if it exists\n"
74 " -h, --help output usage information and exit\n"
75 " -n, --noname display user and group IDs instead of names\n"
76 " -v, --version output version information and exit\n"
77 "\n"
78 "When multiple files are specified on the command line, a blank\n"
79 "line separates the ACLs for each file.\n", prog_name);
80 if (stream == stdout)
81 {
82 fprintf (stream, ""
83 "For each argument that is a regular file, special file or\n"
84 "directory, getfacl displays the owner, the group, and the ACL.\n"
85 "For directories getfacl displays additionally the default ACL.\n"
86 "\n"
87 "With no options specified, getfacl displays the filename, the\n"
88 "owner, the group, and both the ACL and the default ACL, if it\n"
89 "exists.\n"
90 "\n"
91 "The format for ACL output is as follows:\n"
92 " # file: filename\n"
93 " # owner: name or uid\n"
94 " # group: name or uid\n"
95 " user::perm\n"
96 " user:name or uid:perm\n"
97 " group::perm\n"
98 " group:name or gid:perm\n"
99 " mask:perm\n"
100 " other:perm\n"
101 " default:user::perm\n"
102 " default:user:name or uid:perm\n"
103 " default:group::perm\n"
104 " default:group:name or gid:perm\n"
105 " default:mask:perm\n"
106 " default:other:perm\n"
107 "\n");
108 }
109 }
110
111 struct option longopts[] = {
112 {"all", no_argument, NULL, 'a'},
113 {"dir", no_argument, NULL, 'd'},
114 {"help", no_argument, NULL, 'h'},
115 {"noname", no_argument, NULL, 'n'},
116 {"version", no_argument, NULL, 'v'},
117 {0, no_argument, NULL, 0}
118 };
119
120 static void
121 print_version ()
122 {
123 const char *v = strchr (version, ':');
124 int len;
125 if (!v)
126 {
127 v = "?";
128 len = 1;
129 }
130 else
131 {
132 v += 2;
133 len = strchr (v, ' ') - v;
134 }
135 printf ("\
136 getfacl (cygwin) %.*s\n\
137 ACL Utility\n\
138 Copyright (c) 2000, 2001, 2002 Red Hat, Inc.\n\
139 Compiled on %s\n\
140 ", len, v, __DATE__);
141 }
142
143 int
144 main (int argc, char **argv)
145 {
146 extern int optind;
147 int c, i;
148 int aopt = 0;
149 int dopt = 0;
150 int nopt = 0;
151 int first = 1;
152 struct stat st;
153 aclent_t acls[MAX_ACL_ENTRIES];
154
155 prog_name = strrchr (argv[0], '/');
156 if (prog_name == NULL)
157 prog_name = strrchr (argv[0], '\\');
158 if (prog_name == NULL)
159 prog_name = argv[0];
160 else
161 prog_name++;
162
163 while ((c = getopt_long (argc, argv, "adhnv", longopts, NULL)) != EOF)
164 switch (c)
165 {
166 case 'a':
167 aopt = 1;
168 break;
169 case 'd':
170 dopt = 1;
171 break;
172 case 'h':
173 usage (stdout);
174 return 0;
175 case 'n':
176 nopt = 1;
177 break;
178 case 'v':
179 print_version ();
180 return 0;
181 default:
182 usage (stderr);
183 return 1;
184 }
185 if (optind > argc - 1)
186 {
187 usage (stderr);
188 return 1;
189 }
190 while ((c = optind++) < argc)
191 {
192 if (stat (argv[c], &st))
193 {
194 perror (argv[0]);
195 continue;
196 }
197 if (!first)
198 putchar ('\n');
199 first = 0;
200 printf ("# file: %s\n", argv[c]);
201 if (nopt)
202 {
203 printf ("# owner: %lu\n", (unsigned long)st.st_uid);
204 printf ("# group: %lu\n", (unsigned long)st.st_gid);
205 }
206 else
207 {
208 printf ("# owner: %s\n", username (st.st_uid));
209 printf ("# group: %s\n", groupname (st.st_gid));
210 }
211 if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
212 {
213 perror (argv[0]);
214 continue;
215 }
216 for (i = 0; i < c; ++i)
217 {
218 if (acls[i].a_type & ACL_DEFAULT)
219 {
220 if (aopt)
221 continue;
222 printf ("default:");
223 }
224 else if (dopt)
225 continue;
226 switch (acls[i].a_type & ~ACL_DEFAULT)
227 {
228 case USER_OBJ:
229 printf ("user::");
230 break;
231 case USER:
232 if (nopt)
233 printf ("user:%lu\n", (unsigned long)acls[i].a_id);
234 else
235 printf ("user:%s:", username (acls[i].a_id));
236 break;
237 case GROUP_OBJ:
238 printf ("group::");
239 break;
240 case GROUP:
241 if (nopt)
242 printf ("group:%lu\n", (unsigned long)acls[i].a_id);
243 else
244 printf ("group:%s:", groupname (acls[i].a_id));
245 break;
246 case CLASS_OBJ:
247 printf ("mask:");
248 break;
249 case OTHER_OBJ:
250 printf ("other:");
251 break;
252 }
253 printf ("%s\n", permstr (acls[i].a_perm));
254 }
255 }
256 return 0;
257 }
This page took 0.045027 seconds and 6 git commands to generate.