The Linux Programming Interface

(nextflipdebug5) #1
Access Control Lists 335

Listing 17-1: Display the access or default ACL on a file


––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– acl/acl_view.c
#include <acl/libacl.h>
#include <sys/acl.h>
#include "ugid_functions.h"
#include "tlpi_hdr.h"


static void
usageError(char *progName)
{
fprintf(stderr, "Usage: %s [-d] filename\n", progName);
exit(EXIT_FAILURE);
}


int
main(int argc, char argv[])
{
acl_t acl;
acl_type_t type;
acl_entry_t entry;
acl_tag_t tag;
uid_t
uidp;
gid_t gidp;
acl_permset_t permset;
char
name;
int entryId, permVal, opt;


type = ACL_TYPE_ACCESS;
while ((opt = getopt(argc, argv, "d")) != -1) {
switch (opt) {
case 'd': type = ACL_TYPE_DEFAULT; break;
case '?': usageError(argv[0]);
}
}


if (optind + 1 != argc)
usageError(argv[0]);


acl = acl_get_file(argv[optind], type);
if (acl == NULL)
errExit("acl_get_file");


/ Walk through each entry in this ACL /


for (entryId = ACL_FIRST_ENTRY; ; entryId = ACL_NEXT_ENTRY) {


if (acl_get_entry(acl, entryId, &entry) != 1)
break; / Exit on error or no more entries /

Free download pdf