1/* Return the number of entries in an ACL.
2
3   Copyright (C) 2002-2003, 2005-2010 Free Software Foundation, Inc.
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 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU 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, see <http://www.gnu.org/licenses/>.
17
18   Written by Paul Eggert and Andreas Gruenbacher.  */
19
20#include <config.h>
21
22#include "acl-internal.h"
23
24/* This file assumes POSIX-draft like ACLs
25   (Linux, FreeBSD, MacOS X, IRIX, Tru64).  */
26
27/* Return the number of entries in ACL.
28   Return -1 and set errno upon failure to determine it.  */
29
30int
31acl_entries (acl_t acl)
32{
33  int count = 0;
34
35  if (acl != NULL)
36    {
37#if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD, MacOS X */
38# if HAVE_ACL_TYPE_EXTENDED /* MacOS X */
39      /* acl_get_entry returns 0 when it successfully fetches an entry,
40         and -1/EINVAL at the end.  */
41      acl_entry_t ace;
42      int got_one;
43
44      for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
45           got_one >= 0;
46           got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
47        count++;
48# else /* Linux, FreeBSD */
49      /* acl_get_entry returns 1 when it successfully fetches an entry,
50         and 0 at the end.  */
51      acl_entry_t ace;
52      int got_one;
53
54      for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
55           got_one > 0;
56           got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
57        count++;
58      if (got_one < 0)
59        return -1;
60# endif
61#else /* IRIX, Tru64 */
62# if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
63      /* Don't use acl_get_entry: it is undocumented.  */
64      count = acl->acl_cnt;
65# endif
66# if HAVE_ACL_FREE_TEXT /* Tru64 */
67      /* Don't use acl_get_entry: it takes only one argument and does not
68         work.  */
69      count = acl->acl_num;
70# endif
71#endif
72    }
73
74  return count;
75}
76