Make acl_entries work reliably.
[gnulib.git] / lib / acl_entries.c
1 /* Return the number of entries in an ACL.
2
3    Copyright (C) 2002-2003, 2005-2008 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
29 int
30 acl_entries (acl_t acl)
31 {
32   int count = 0;
33
34   if (acl != NULL)
35     {
36 #if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD, MacOS X */
37       acl_entry_t ace;
38       int at_end;
39
40       for (at_end = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
41            !at_end;
42            at_end = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
43         count++;
44 #else /* IRIX, Tru64 */
45 # if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
46       /* Don't use acl_get_entry: it is undocumented.  */
47       count = acl->acl_cnt;
48 # endif
49 # if HAVE_ACL_FREE_TEXT /* Tru64 */
50       /* Don't use acl_get_entry: it takes only one argument and does not
51          work.  */
52       count = acl->acl_num;
53 # endif
54 #endif
55     }
56
57   return count;
58 }