home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / vfs / container.c next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  2.3 KB  |  104 lines

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