File: cgi-llist.c

package info (click to toggle)
postman 2.0-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,036 kB
  • ctags: 3,078
  • sloc: cpp: 31,131; ansic: 1,948; sh: 787; makefile: 285
file content (106 lines) | stat: -rwxr-xr-x 2,045 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* cgi-llist.c - Minimal linked list library for revised CGI C library
   Eugene Kim, <eekim@eekim.com>
   $Id: cgi-llist.c,v 1.5 1998/05/04 01:39:55 eekim Exp $

   Copyright (C) 1995 Eugene Eric Kim
   All Rights Reserved
*/

#include <stdlib.h>
#include <string.h>
#include "cgi-llist.h"

char *newstrx(char *str)
  {
  char *tempstr = (char *)malloc(sizeof(char) * strlen(str) + 1);
  if (tempstr != NULL) strncpy(tempstr,str, strlen(str)+1);
  return tempstr;
  }

void list_create(llist *l)
{
  l->head = 0;
}

node* list_next(node* w)
{
  return w->next;
}

short on_list(llist *l, node* w)
{
  return (w != 0);
}

short on_list_debug(llist *l, node* w)
{
  node* current;

  if (w == 0)
    return 0;
  else {
    current = l->head;
    while ( (current != w) && (current != 0) )
      current = current->next;
    if (current == w)
      return 1;
    else
      return 0;
  }
}

void list_traverse(llist *l, void (*visit)(entrytype item))
{
  node* current;

  current = l->head;
  while (current != 0) {
    (*visit)(current->entry);
    current = current->next;
  }
}

node* list_insafter(llist* l, node* w, entrytype item)
{
  node* newnode = (node *)malloc(sizeof(node));

  newnode->entry.name = newstrx(item.name);
  newnode->entry.value = newstrx(item.value);
  if (l->head == 0) {
    newnode->next = 0;
    l->head = newnode;
  }
  else if (!on_list(l,w))
    /* ERROR: can't insert item after w since w is not on l */
    exit(1);
  else {
    /* insert node after */
    if (newnode == 0) /* can assume that w != NULL */
      /* ERROR: nothing to insert after */
      exit(1);
    else {
      newnode->next = w->next;
      w->next = newnode;
    }
  }
  return newnode;
}

void list_clear(llist* l)
  {
  node* nextnode;
  node* current;

  if (l)
    {
    current = l->head;
    while (current) 
      {
      nextnode=current->next;
      if (current->entry.name)  free (current->entry.name);
      if (current->entry.value) free (current->entry.value);
      free(current);
      current=nextnode;
      }
    }
  }