container.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Virtual File System
  2. Copyright (C) 1995 The Free Software Foundation
  3. Written by: 1995 Ching Hui (mr854307@cs.nthu.edu.tw)
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License
  6. as published by the Free Software Foundation; either version 2 of
  7. the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <config.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include "../src/mad.h"
  20. #include "../src/util.h"
  21. #include "xdirentry.h"
  22. #include "container.h"
  23. struct linklist *
  24. linklist_init(void)
  25. {
  26. struct linklist *head;
  27. head = xmalloc(sizeof(struct linklist), "struct linklist");
  28. if (head) {
  29. head->prev = head->next = head;
  30. head->data = NULL;
  31. }
  32. return head;
  33. }
  34. void
  35. linklist_destroy(struct linklist *head, void (*destructor) (void *))
  36. {
  37. struct linklist *p, *q;
  38. for (p = head->next; p != head; p = q) {
  39. if (p->data && destructor)
  40. (*destructor) (p->data);
  41. q = p->next;
  42. free(p);
  43. }
  44. free(head);
  45. }
  46. int
  47. linklist_insert(struct linklist *head, void *data)
  48. {
  49. struct linklist *p;
  50. p = xmalloc(sizeof(struct linklist), "struct linklist");
  51. if (p == NULL)
  52. return 0;
  53. p->data = data;
  54. p->prev = head->prev;
  55. p->next = head;
  56. head->prev->next = p;
  57. head->prev = p;
  58. return 1;
  59. }
  60. void
  61. linklist_delete_all(struct linklist *head, void (*destructor) (void *))
  62. {
  63. struct linklist *p, *q;
  64. for (p = head->next; p != head; p = q) {
  65. destructor(p->data);
  66. q = p->next;
  67. free(p);
  68. }
  69. head->next = head->prev = head;
  70. head->data = NULL;
  71. }
  72. int
  73. linklist_delete(struct linklist *head, void *data)
  74. {
  75. struct linklist *h = head->next;
  76. while (h != head) {
  77. if (h->data == data) {
  78. h->prev->next = h->next;
  79. h->next->prev = h->prev;
  80. free(h);
  81. return 1;
  82. }
  83. h = h->next;
  84. }
  85. return 0;
  86. }