Aggiornamento date copyright piĆ¹ TODO 5.3
[gapil.git] / sources / mygetfacl.c
1 /* mygetfacl.c
2  * 
3  * Copyright (C) 2012 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program mygetfacl.c: 
22  * A simple program that get a file acl
23  *
24  * Author: Simone Piccardi
25  * Jan 2012
26  *
27  ****************************************************************/
28 /* 
29  * Include needed headers
30  */
31 #include <sys/types.h>   /* primitive system data types */
32 #include <stdlib.h>      /* C standard library */
33 #include <unistd.h>      /* unix standard library */
34 #include <stdio.h>       /* standard I/O library */
35 #include <sys/acl.h>     /* acl library (use -l acl) */
36 #include <errno.h>     /* acl library (use -l acl) */
37
38
39 /* pass -D DEBUG to gcc to enable debug printing */
40 #ifdef DEBUG
41 #define debug printf
42 #else
43 #define debug(fmt, arg...)
44 #endif /* DEBUG */
45
46 /*
47  * Program mygetfacl
48  */
49 /* Help printing routine */
50 void usage(void);
51
52 int main(int argc, char *argv[])
53 {
54 /*
55  * Variables definition
56  */    
57     acl_t acl;
58     ssize_t size;
59     char * buffer;
60     int i;
61     /*
62      * Input section: decode command line parameters 
63      * Use getopt function
64      */
65     opterr = 0;  /* don't want writing to stderr */
66     while ( (i = getopt(argc, argv, "h")) != -1) {
67         switch (i) {
68         /* 
69          * Handling options 
70          */ 
71         case 'h':                                  /* help option */
72             printf("Wrong -h option use\n");
73             usage();
74             return -1;
75             break;
76         case '?':                                  /* unrecognized options */
77             printf("Unrecognized options -%c\n",optopt);
78             usage();
79         default:                                   /* should not reached */
80             usage();
81         }
82     }
83     /* must have an argument */
84     if ((argc - optind) != 1) {
85         printf("Wrong number of arguments %d\n", argc - optind);
86         usage();
87     }
88     /* main body */
89     errno = 0;
90     acl = acl_get_file(argv[1], ACL_TYPE_ACCESS);
91     if (acl == NULL) {
92         printf("error on getting ACL for file %s\n", argv[1]);
93         perror(argv[1]);
94         return 1;
95     }
96     buffer = acl_to_text(acl, &size);
97     if (buffer == NULL) {
98         perror("cannot convert acl");
99         return 1;
100     }
101     printf("ACL for file '%s':\n%s\n", argv[1], buffer);
102     acl_free(acl);
103     acl_free(buffer);
104     return 0;
105 }
106
107
108 /*
109  * routine to print usage info and exit
110  */
111 void usage(void) {
112     printf("Program mymount:  \n");
113     printf("Usage:\n");
114     printf("mygetfacl [-h] file\n");
115     printf("  -h          print this help\n");
116     exit(1);
117 }