acl_entries.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Return the number of entries in an ACL.
  2. Copyright (C) 2002-2003, 2005-2015 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. Written by Paul Eggert and Andreas Gruenbacher. */
  14. #include <config.h>
  15. #include "acl-internal.h"
  16. /* This file assumes POSIX-draft like ACLs
  17. (Linux, FreeBSD, Mac OS X, IRIX, Tru64). */
  18. /* Return the number of entries in ACL.
  19. Return -1 and set errno upon failure to determine it. */
  20. int
  21. acl_entries (acl_t acl)
  22. {
  23. int count = 0;
  24. if (acl != NULL)
  25. {
  26. #if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD, Mac OS X */
  27. # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
  28. /* acl_get_entry returns 0 when it successfully fetches an entry,
  29. and -1/EINVAL at the end. */
  30. acl_entry_t ace;
  31. int got_one;
  32. for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
  33. got_one >= 0;
  34. got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
  35. count++;
  36. # else /* Linux, FreeBSD */
  37. /* acl_get_entry returns 1 when it successfully fetches an entry,
  38. and 0 at the end. */
  39. acl_entry_t ace;
  40. int got_one;
  41. for (got_one = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
  42. got_one > 0;
  43. got_one = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
  44. count++;
  45. if (got_one < 0)
  46. return -1;
  47. # endif
  48. #else /* IRIX, Tru64 */
  49. # if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
  50. /* Don't use acl_get_entry: it is undocumented. */
  51. count = acl->acl_cnt;
  52. # endif
  53. # if HAVE_ACL_FREE_TEXT /* Tru64 */
  54. /* Don't use acl_get_entry: it takes only one argument and does not
  55. work. */
  56. count = acl->acl_num;
  57. # endif
  58. #endif
  59. }
  60. return count;
  61. }