groupset.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "logfsos.h"
  2. #include "logfs.h"
  3. #include "local.h"
  4. struct GroupSet {
  5. int maxentries;
  6. int nentries;
  7. Group **entry;
  8. };
  9. char *
  10. logfsgroupsetnew(GroupSet **gsp)
  11. {
  12. GroupSet *gs = logfsrealloc(nil, sizeof(*gs));
  13. if(gs == nil)
  14. return Enomem;
  15. gs->entry = logfsrealloc(nil, sizeof(Group *));
  16. if(gs->entry == nil) {
  17. logfsfreemem(gs);
  18. return Enomem;
  19. }
  20. gs->maxentries = 1; /* most groups have one member */
  21. gs->nentries = 0;
  22. *gsp = gs;
  23. return nil;
  24. }
  25. void
  26. logfsgroupsetfree(GroupSet **gsp)
  27. {
  28. GroupSet *gs = *gsp;
  29. if(gs) {
  30. logfsfreemem(gs->entry);
  31. logfsfreemem(gs);
  32. *gsp = nil;
  33. }
  34. }
  35. int
  36. logfsgroupsetadd(GroupSet *gs, Group *g)
  37. {
  38. int x;
  39. for(x = 0; x < gs->nentries; x++)
  40. if(gs->entry[x] == g)
  41. return 1;
  42. if(gs->nentries >= gs->maxentries) {
  43. Group **ne = logfsrealloc(gs->entry, sizeof(Group*)*(gs->maxentries * 2));
  44. if(ne == nil)
  45. return 0;
  46. gs->entry = ne;
  47. gs->maxentries *= 2;
  48. }
  49. gs->entry[gs->nentries++] = g;
  50. return 1;
  51. }
  52. int
  53. logfsgroupsetremove(GroupSet *gs, Group *g)
  54. {
  55. int x;
  56. for(x = 0; x < gs->nentries; x++)
  57. if(gs->entry[x] == g)
  58. break;
  59. if(x == gs->nentries)
  60. return 0;
  61. gs->nentries--;
  62. memmove(&gs->entry[x], &gs->entry[x + 1], sizeof(Group *) * (gs->nentries - x));
  63. return 1;
  64. }
  65. int
  66. logfsgroupsetwalk(GroupSet *gs, LOGFSGROUPSETWALKFN *func, void *magic)
  67. {
  68. int x;
  69. for(x = 0; x < gs->nentries; x++) {
  70. int rv = (*func)(magic, gs->entry[x]);
  71. if(rv <= 0)
  72. return rv;
  73. }
  74. return 1;
  75. }
  76. int
  77. logfsgroupsetismember(GroupSet *gs, Group *g)
  78. {
  79. int x;
  80. for(x = 0; x < gs->nentries; x++)
  81. if(gs->entry[x] == g)
  82. return 1;
  83. return 0;
  84. }