Tizen 2.0 Release
[framework/base/acl.git] / examples / copy-acl.c
1 /*
2   Copyright (C) 2009  Andreas Gruenbacher <agruen@suse.de>
3
4   This program is free software: you can redistribute it and/or modify it
5   under the terms of the GNU General Public License as published by
6   the Free Software Foundation, either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <libgen.h>
22 #include <sys/acl.h>
23
24 const char *progname;
25
26 int main(int argc, char *argv[])
27 {
28         acl_t acl, default_acl;
29         int n, ret = 0;
30
31         progname = basename(argv[0]);
32
33         if (argc < 3) {
34                 printf("%s -- copy access control lists between files \n"
35                         "Usage: %s file1 file2 ...\n",
36                         progname, progname);
37                 return 1;
38         }
39
40         acl = acl_get_file(argv[1], ACL_TYPE_ACCESS);
41         if (acl == NULL) {
42                 fprintf(stderr, "%s: getting acl of %s: %s\n",
43                         progname, argv[1], strerror(errno));
44                 return 1;
45         }
46         default_acl = acl_get_file(argv[1], ACL_TYPE_DEFAULT);
47         if (default_acl == NULL) {
48                 fprintf(stderr, "%s: getting default acl of %s: %s\n",
49                         progname, argv[1], strerror(errno));
50                 return 1;
51         }
52
53         for (n = 2; n < argc; n++) {
54                 if (acl_set_file(argv[n], ACL_TYPE_ACCESS, acl) != 0) {
55                         fprintf(stderr, "%s: setting acl for %s: %s\n",
56                                 progname, argv[n], strerror(errno));
57                         ret = 1;
58                 } else if (acl_set_file(argv[n], ACL_TYPE_DEFAULT,
59                                         default_acl) != 0) {
60                         fprintf(stderr, "%s: setting default acl for %s: %s\n",
61                                 progname, argv[n], strerror(errno));
62                         ret = 1;
63                 }
64         }
65
66         acl_free(acl);
67         acl_free(default_acl);
68
69         return ret;
70 }