newlib/winsup/utils/getfacl.c

127 lines
2.2 KiB
C
Raw Normal View History

2000-02-17 20:38:33 +01:00
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/acl.h>
#include <sys/stat.h>
char *
permstr (mode_t perm)
2000-02-17 20:38:33 +01:00
{
static char pbuf[4];
pbuf[0] = (perm & S_IREAD) ? 'r' : '-';
pbuf[1] = (perm & S_IWRITE) ? 'w' : '-';
pbuf[2] = (perm & S_IEXEC) ? 'x' : '-';
pbuf[3] = '\0';
return pbuf;
}
#if 0
char *
username (uid_t uid)
2000-02-17 20:38:33 +01:00
{
static char ubuf[256];
struct passwd *pw;
if (pw = getpwuid (uid))
strcpy (ubuf, pw->pw_name);
else
strcpy (ubuf, "<unknown>");
}
char *
groupname (gid_t gid)
2000-02-17 20:38:33 +01:00
{
static char gbuf[256];
struct group *gr;
if (gr = getgruid (gid))
strcpy (gbuf, gr->gr_name);
else
strcpy (gbuf, "<unknown>");
}
#endif
int
main (int argc, char **argv)
{
extern int optind;
int c, i;
int aopt = 0;
int dopt = 0;
int first = 1;
struct stat st;
aclent_t acls[MAX_ACL_ENTRIES];
while ((c = getopt (argc, argv, "ad")) != EOF)
switch (c)
{
case 'a':
aopt = 1;
break;
2000-02-17 20:38:33 +01:00
case 'd':
dopt = 1;
break;
2000-02-17 20:38:33 +01:00
default:
fprintf (stderr, "usage: %s [-ad] file...\n", argv[0]);
return 1;
2000-02-17 20:38:33 +01:00
}
while ((c = optind++) < argc)
{
if (stat (argv[c], &st))
{
perror (argv[0]);
continue;
}
if (!first)
putchar ('\n');
2000-02-17 20:38:33 +01:00
first = 0;
printf ("# file: %s\n", argv[c]);
printf ("# owner: %d\n", st.st_uid);
printf ("# group: %d\n", st.st_gid);
if ((c = acl (argv[c], GETACL, MAX_ACL_ENTRIES, acls)) < 0)
{
perror (argv[0]);
continue;
}
2000-02-17 20:38:33 +01:00
for (i = 0; i < c; ++i)
{
if (acls[i].a_type & ACL_DEFAULT)
{
if (aopt)
continue;
printf ("default:");
}
else if (dopt)
continue;
switch (acls[i].a_type & ~ACL_DEFAULT)
{
case USER_OBJ:
printf ("user::");
break;
case USER:
printf ("user:%d:", acls[i].a_id);
break;
case GROUP_OBJ:
printf ("group::");
break;
case GROUP:
printf ("group:%d:", acls[i].a_id);
break;
case CLASS_OBJ:
printf ("mask::");
break;
case OTHER_OBJ:
printf ("other::");
break;
}
printf ("%s\n", permstr (acls[i].a_perm));
}
2000-02-17 20:38:33 +01:00
}
return 0;
}