Get rid of -I include path of /usr/include/acl.
[xfstests-dev.git] / src / acl_get.c
1 /*
2  * Copyright (c) 2001 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This prog is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This prog is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this prog with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this prog; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 /*
34  * Get an access or default acl on a file
35  * using IRIX semantics or Linux semantics
36  */
37  
38 #include "global.h"
39
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <sys/acl.h>
45
46 char *prog;
47
48 void usage(void)
49 {
50     fprintf(stderr, "usage: %s [-a] [-d] [-f] [-i] path\n"
51            "flags:\n"
52            "    -a - get access ACL\n"
53            "    -d - get default ACL\n" 
54            "    -f - get access ACL using file descriptor\n" 
55            "    -i - use irix semantics\n" 
56            ,prog);
57            
58 }
59
60
61
62 int
63 main(int argc, char **argv)
64 {
65         int c;
66         char *file;
67         int getaccess = 0;
68         int getdefault = 0;
69         int irixsemantics = 0;
70         int usefd = 0;
71         int fd = -1;
72         acl_t acl;
73         char *buf_acl;
74
75         prog = basename(argv[0]);
76
77         while ((c = getopt(argc, argv, "adif")) != -1) {
78                 switch (c) {
79                 case 'a':
80                         getaccess = 1;
81                         break;
82                 case 'd':
83                         getdefault = 1;
84                         break;
85                 case 'i':
86                         irixsemantics = 1;
87                         break;
88                 case 'f':
89                         usefd = 1;
90                         break;
91                 case '?':
92                         usage();
93                         return 1;
94                 }
95         }
96
97         if (getdefault && usefd) {
98             fprintf(stderr, "%s: -f and -d are not compatible\n", prog);
99             return 1;
100         }
101
102         /* need path */
103         if (optind == argc) {
104             usage();
105             exit(1);
106         }
107         else {
108             file = argv[optind];
109         } 
110
111         if (irixsemantics) {
112             acl_set_compat(ACL_COMPAT_IRIXGET);
113         }
114
115         if (usefd) {
116             fd = open(file, O_RDONLY);
117             if (fd < 0) {
118                 fprintf (stderr, "%s: error opening \"%s\": %s\n",
119                              prog, file, strerror(errno));
120                 usage(); 
121                 return 1;
122
123             }   
124         }
125
126         if (getaccess) {
127             if (usefd) { 
128                 acl = acl_get_fd(fd);
129             }
130             else {
131                 acl = acl_get_file(file, ACL_TYPE_ACCESS);
132             }
133             if (acl == NULL) {
134                 fprintf (stderr, "%s: error getting access ACL on \"%s\": %s\n",
135                              prog, file, strerror(errno));
136                 return 0;
137             }
138             if (irixsemantics && acl->acl_cnt == ACL_NOT_PRESENT) {
139                 buf_acl = strdup("irix-empty");
140             }
141             else {      
142                 buf_acl = acl_to_short_text (acl, (ssize_t *) NULL);
143                 if (buf_acl == NULL) {
144                     fprintf (stderr, "%s: error converting ACL to short text "
145                                      "for file \"%s\": %s\n",
146                                  prog, file, strerror(errno));
147                     return 0;
148                 }
149             }
150             printf("%s: access %s\n", file, buf_acl);
151             acl_free(acl);
152             acl_free(buf_acl);  
153         }
154
155         if (getdefault) {
156             acl = acl_get_file(file, ACL_TYPE_DEFAULT);
157             if (acl == NULL) {
158                 fprintf (stderr, "%s: error getting default ACL on \"%s\": %s\n",
159                              prog, file, strerror(errno));
160                 return 0;
161             }
162             if (irixsemantics && acl->acl_cnt == ACL_NOT_PRESENT) {
163                 buf_acl = strdup("irix-empty");
164             }
165             else if (!irixsemantics && acl->acl_cnt == 0) {
166                 buf_acl = strdup("linux-empty");
167             }
168             else {      
169                 buf_acl = acl_to_short_text (acl, (ssize_t *) NULL);
170                 if (buf_acl == NULL) {
171                     fprintf (stderr, "%s: error converting ACL to short text "
172                                      "for file \"%s\": %s\n",
173                                  prog, file, strerror(errno));
174                     return 0;
175                 }
176             }
177             printf("%s: default %s\n", file, buf_acl);
178             acl_free(acl);
179             acl_free(buf_acl);
180         }
181
182         return 0;
183 }