From Jason Lango <jal@netapp.com>, a fix to a long-standing problem
[obnox/wireshark/wip.git] / wiretap / glib-new.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21 #ifdef HAVE_GLIB10
22
23 #include <glib.h>
24 #include <stdarg.h>
25 #include <string.h>
26
27 gpointer
28 g_memdup (const gpointer mem,
29           guint         byte_size)
30 {
31   gpointer new_mem;
32
33   if (mem)
34     {
35       new_mem = g_malloc (byte_size);
36       memcpy (new_mem, mem, byte_size);
37     }
38   else
39     new_mem = NULL;
40
41   return new_mem;
42 }
43
44 gchar*
45 g_strjoin (const gchar  *separator,
46            ...)
47 {
48   gchar *string, *s;
49   va_list args;
50   guint len;
51   guint separator_len;
52
53   if(separator == NULL)
54     separator = "";
55
56   separator_len = strlen (separator);
57
58   va_start(args, separator);
59
60   s = va_arg(args, gchar *);
61
62   if(s) {
63     len = strlen(s) + 1;
64
65     while((s = va_arg(args, gchar*)))
66       {
67         len += separator_len + strlen(s);
68       }
69     va_end(args);
70
71     string = g_new (gchar, len);
72
73     va_start(args, separator);
74
75     *string = 0;
76     s = va_arg(args, gchar*);
77     strcat (string, s);
78
79     while((s = va_arg(args, gchar*)))
80       {
81         strcat(string, separator);
82         strcat(string, s);
83       }
84
85   } else
86     string = g_strdup("");
87
88   va_end(args);
89
90   return string;
91 }
92
93 /* this was introduced sometime between glib-1.0.1 and glib-1.0.4 */
94 gpointer
95 g_slist_nth_data (GSList   *list,
96                           guint     n)
97 {
98           while ((n-- > 0) && list)
99                       list = list->next;
100
101             return list ? list->data : NULL;
102 }
103
104
105 #endif