~ubuntu-branches/ubuntu/natty/curl/natty-proposed

« back to all changes in this revision

Viewing changes to lib/slist.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-05-24 21:12:19 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (3.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20090524211219-7jgcwuhl04ixuqsm
Tags: upstream-7.19.5
ImportĀ upstreamĀ versionĀ 7.19.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
 
9
 *
 
10
 * This software is licensed as described in the file COPYING, which
 
11
 * you should have received as part of this distribution. The terms
 
12
 * are also available at http://curl.haxx.se/docs/copyright.html.
 
13
 *
 
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
15
 * copies of the Software, and permit persons to whom the Software is
 
16
 * furnished to do so, under the terms of the COPYING file.
 
17
 *
 
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 
19
 * KIND, either express or implied.
 
20
 *
 
21
 * $Id: slist.c,v 1.2 2009-04-21 11:46:17 yangtse Exp $
 
22
 ***************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdarg.h>
 
28
#include <stdlib.h>
 
29
 
 
30
#include <string.h>
 
31
#include "curl_memory.h"
 
32
#include "slist.h"
 
33
 
 
34
/* The last #include file should be: */
 
35
#include "memdebug.h"
 
36
 
 
37
/* returns last node in linked list */
 
38
static struct curl_slist *slist_get_last(struct curl_slist *list)
 
39
{
 
40
  struct curl_slist     *item;
 
41
 
 
42
  /* if caller passed us a NULL, return now */
 
43
  if(!list)
 
44
    return NULL;
 
45
 
 
46
  /* loop through to find the last item */
 
47
  item = list;
 
48
  while(item->next) {
 
49
    item = item->next;
 
50
  }
 
51
  return item;
 
52
}
 
53
 
 
54
/*
 
55
 * curl_slist_append() appends a string to the linked list. It always returns
 
56
 * the address of the first record, so that you can use this function as an
 
57
 * initialization function as well as an append function. If you find this
 
58
 * bothersome, then simply create a separate _init function and call it
 
59
 * appropriately from within the program.
 
60
 */
 
61
struct curl_slist *curl_slist_append(struct curl_slist *list,
 
62
                                     const char *data)
 
63
{
 
64
  struct curl_slist     *last;
 
65
  struct curl_slist     *new_item;
 
66
 
 
67
  new_item = malloc(sizeof(struct curl_slist));
 
68
  if(new_item) {
 
69
    char *dupdata = strdup(data);
 
70
    if(dupdata) {
 
71
      new_item->next = NULL;
 
72
      new_item->data = dupdata;
 
73
    }
 
74
    else {
 
75
      free(new_item);
 
76
      return NULL;
 
77
    }
 
78
  }
 
79
  else
 
80
    return NULL;
 
81
 
 
82
  if(list) {
 
83
    last = slist_get_last(list);
 
84
    last->next = new_item;
 
85
    return list;
 
86
  }
 
87
 
 
88
  /* if this is the first item, then new_item *is* the list */
 
89
  return new_item;
 
90
}
 
91
 
 
92
/*
 
93
 * Curl_slist_duplicate() duplicates a linked list. It always returns the
 
94
 * address of the first record of the cloned list or NULL in case of an
 
95
 * error (or if the input list was NULL).
 
96
 */
 
97
struct curl_slist *Curl_slist_duplicate(struct curl_slist *inlist)
 
98
{
 
99
  struct curl_slist *outlist = NULL;
 
100
  struct curl_slist *tmp;
 
101
 
 
102
  while(inlist) {
 
103
    tmp = curl_slist_append(outlist, inlist->data);
 
104
 
 
105
    if (!tmp) {
 
106
      curl_slist_free_all(outlist);
 
107
      return NULL;
 
108
    }
 
109
 
 
110
    outlist = tmp;
 
111
    inlist = inlist->next;
 
112
  }
 
113
  return outlist;
 
114
}
 
115
 
 
116
/* be nice and clean up resources */
 
117
void curl_slist_free_all(struct curl_slist *list)
 
118
{
 
119
  struct curl_slist     *next;
 
120
  struct curl_slist     *item;
 
121
 
 
122
  if(!list)
 
123
    return;
 
124
 
 
125
  item = list;
 
126
  do {
 
127
    next = item->next;
 
128
 
 
129
    if(item->data) {
 
130
      free(item->data);
 
131
    }
 
132
    free(item);
 
133
    item = next;
 
134
  } while(next);
 
135
}
 
136