Undoes mod: xfs-cmds:slinx:120772a
[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 <stdlib.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <getopt.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/acl.h>
47 #include <acl/libacl.h>
48
49 char *prog;
50
51 void usage(void)
52 {
53     fprintf(stderr, "usage: %s [-a] [-d] [-f] [-i] path\n"
54            "flags:\n"
55            "    -a - get access ACL\n"
56            "    -d - get default ACL\n" 
57            "    -f - get access ACL using file descriptor\n" 
58            ,prog);
59            
60 }
61
62
63
64 int
65 main(int argc, char **argv)
66 {
67         int c;
68         char *file;
69         char *acl_text;
70         int getaccess = 0;
71         int getdefault = 0;
72         int usefd = 0;
73         int fd = -1;
74         acl_t acl;
75
76         prog = basename(argv[0]);
77
78         while ((c = getopt(argc, argv, "adf")) != -1) {
79                 switch (c) {
80                 case 'a':
81                         getaccess = 1;
82                         break;
83                 case 'd':
84                         getdefault = 1;
85                         break;
86                 case 'f':
87                         usefd = 1;
88                         break;
89                 case '?':
90                         usage();
91                         return 1;
92                 }
93         }
94
95         if (getdefault && usefd) {
96             fprintf(stderr, "%s: -f and -d are not compatible\n", prog);
97             return 1;
98         }
99
100         /* need path */
101         if (optind == argc) {
102             usage();
103             exit(1);
104         }
105         else {
106             file = argv[optind];
107         } 
108
109         if (usefd) {
110             fd = open(file, O_RDONLY);
111             if (fd < 0) {
112                 fprintf (stderr, "%s: error opening \"%s\": %s\n",
113                              prog, file, strerror(errno));
114                 usage(); 
115                 return 1;
116
117             }   
118         }
119
120         if (getaccess) {
121             if (usefd) { 
122                 acl = acl_get_fd(fd);
123             }
124             else {
125                 acl = acl_get_file(file, ACL_TYPE_ACCESS);
126             }
127             if (acl == NULL) {
128                 fprintf(stderr, "%s: error getting access ACL on \"%s\": %s\n",
129                              prog, file, strerror(errno));
130                 return 0;
131             }
132             acl_text = acl_to_any_text(acl, NULL, ',', TEXT_ABBREVIATE);
133             if (acl_text == NULL) {
134                 fprintf(stderr, "%s: cannot get access ACL text on '%s': %s\n",
135                         prog, file, strerror(errno));
136                 return 0;
137             }
138             printf("%s: access %s", file, acl_text);
139             acl_free(acl_text);
140             acl_free(acl);
141         }
142
143         if (getdefault) {
144             acl = acl_get_file(file, ACL_TYPE_DEFAULT);
145             if (acl == NULL) {
146                 fprintf(stderr, "%s: error getting default ACL on \"%s\": %s\n",
147                              prog, file, strerror(errno));
148                 return 0;
149             }
150             acl_text = acl_to_any_text(acl, NULL, ',', TEXT_ABBREVIATE);
151             if (acl_text == NULL) {
152                 fprintf(stderr, "%s: cannot get default ACL text on '%s': %s\n",
153                         prog, file, strerror(errno));
154                 return 0;
155             }
156             printf("%s: default %s", file, acl_text);
157             acl_free(acl_text);
158             acl_free(acl);
159         }
160
161         return 0;
162 }