(root)/
acl-2.3.1/
examples/
get-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/stat.h>
      23  #include <sys/acl.h>
      24  
      25  const char *progname;
      26  
      27  int main(int argc, char *argv[])
      28  {
      29  	int n, ret = 0;
      30  
      31  	progname = basename(argv[0]);
      32  
      33  	if (argc == 1) {
      34  		printf("%s -- get access control lists of files\n"
      35  			"Usage: %s file ...\n",
      36  			progname, progname);
      37  		return 1;
      38  	}
      39  
      40  	for (n = 1; n < argc; n++) {
      41  		struct stat st;
      42  		acl_t acl, default_acl;
      43  		char *acl_text, *default_acl_text, *token;
      44  		
      45  		if (stat(argv[n], &st) != 0) {
      46  			fprintf(stderr, "%s: %s: %s\n",
      47  				progname, argv[n], strerror(errno));
      48  			ret = 1;
      49  			continue;
      50  		}
      51  
      52  		acl = acl_get_file(argv[n], ACL_TYPE_ACCESS);
      53  		if (acl == NULL) {
      54  			fprintf(stderr, "%s: getting acl of %s: %s\n",
      55  				progname, argv[n], strerror(errno));
      56  			ret = 1;
      57  			continue;
      58  		}
      59  		acl_text = acl_to_text(acl, NULL);
      60  		acl_free(acl);
      61  
      62  		if (S_ISDIR(st.st_mode)) {
      63  			default_acl = acl_get_file(argv[n], ACL_TYPE_DEFAULT);
      64  			if (default_acl == NULL) {
      65  				acl_free(acl_text);
      66  				fprintf(stderr, "%s: getting default acl "
      67  					"of %s: %s\n", progname, argv[n],
      68  					strerror(errno));
      69  				ret = 1;
      70  				continue;
      71  			}
      72  			default_acl_text = acl_to_text(default_acl, NULL);
      73  			acl_free(default_acl);
      74  		}
      75  
      76  		printf("# file: %s\n"
      77  		       "# owner: %d\n"
      78  		       "# group: %d\n"
      79  		       "%s",
      80  			argv[n], st.st_uid, st.st_gid, acl_text);
      81  
      82  		if (S_ISDIR(st.st_mode)) {
      83  			token = strtok(default_acl_text, "\n");
      84  			while (token) {
      85  				printf("default:%s\n", token);
      86  				token = strtok(NULL, "\n");
      87  			}
      88  			acl_free(default_acl_text);
      89  		}
      90  		printf("\n");
      91  
      92  		acl_free(acl_text);
      93  	}
      94  	return ret;
      95  }
      96