ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/irc_string.c
Revision: 7584
Committed: Wed Jun 1 19:02:15 2016 UTC (9 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 6407 byte(s)
Log Message:
- token_vector(): use unsigned where appropriate

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 7006 * Copyright (c) 1997-2016 ircd-hybrid development team
5 adx 30 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file irc_string.c
23     * \brief IRC string functions.
24     * \version $Id$
25     */
26    
27 michael 1009 #include "config.h"
28 adx 30 #include "stdinc.h"
29     #include "irc_string.h"
30    
31    
32 michael 1400 int
33 michael 2883 has_wildcards(const char *str)
34 michael 1400 {
35 michael 2883 const unsigned char *p = (const unsigned char *)str;
36 michael 1400
37 michael 2883 for (; *p; ++p)
38     {
39     if (*p == '\\')
40     {
41     if (*++p == '\0')
42     return 0;
43     }
44     else if (IsMWildChar(*p))
45 michael 1400 return 1;
46 michael 2883 }
47 michael 1400
48     return 0;
49     }
50    
51 adx 30 /*
52     * strip_tabs(dst, src, length)
53     *
54     * Copies src to dst, while converting all \t (tabs) into spaces.
55     */
56     void
57     strip_tabs(char *dest, const char *src, size_t len)
58     {
59     char *d = dest;
60    
61     /* Sanity check; we don't want anything nasty... */
62 michael 3246 assert(dest);
63     assert(src);
64 adx 30 assert(len > 0);
65    
66     for (; --len && *src; ++src)
67     *d++ = *src == '\t' ? ' ' : *src;
68    
69     *d = '\0'; /* NUL terminate, thanks and goodbye */
70     }
71    
72     /*
73 michael 6546 * strtok_r - walk through a string of tokens, using a set of separators
74 adx 30 * argv 9/90
75     *
76     */
77     #ifndef HAVE_STRTOK_R
78     char *
79 michael 6546 strtok_r(char *str, const char *fs, char **save)
80 adx 30 {
81 michael 3565 char *pos = *save; /* keep last position across calls */
82     char *tmp = NULL;
83 adx 30
84     if (str)
85     pos = str; /* new string scan */
86    
87 michael 3246 while (pos && *pos && strchr(fs, *pos))
88 adx 30 ++pos; /* skip leading separators */
89    
90     if (!pos || !*pos)
91 michael 4915 return pos = *save = NULL; /* string contains only sep's */
92 adx 30
93     tmp = pos; /* now, keep position of the token */
94    
95     while (*pos && strchr(fs, *pos) == NULL)
96     ++pos; /* skip content of the token */
97    
98     if (*pos)
99     *pos++ = '\0'; /* remove first sep after the token */
100     else
101     pos = NULL; /* end of string */
102    
103     *save = pos;
104     return tmp;
105     }
106     #endif /* !HAVE_STRTOK_R */
107    
108 michael 4309 /** Fill a vector of tokens from a delimited input list.
109     * Empty tokens (when \a token occurs at the start or end of \a list,
110     * or when \a token occurs adjacent to itself) are ignored. When
111     * \a size tokens have been written to \a vector, the rest of the
112     * string is ignored.
113     * \param names Input buffer.
114     * \param token Delimiter used to split \a list.
115     * \param vector Output vector.
116     * \param size Maximum number of elements to put in \a vector.
117     * \return Number of elements written to \a vector.
118     */
119 michael 7584 unsigned int
120     token_vector(char *names, char token, char *vector[], unsigned int size)
121 michael 4309 {
122 michael 7584 unsigned int count = 0;
123 michael 4309 char *start = names;
124    
125     assert(names);
126     assert(vector);
127     assert(size > 1);
128    
129     vector[count++] = start;
130    
131 michael 7584 for (char *end = strchr(start, token); end;
132     end = strchr(start, token))
133 michael 4309 {
134     *end++ = '\0';
135     start = end;
136    
137     if (*start)
138     {
139     vector[count++] = start;
140 michael 4915
141 michael 4309 if (count < size)
142     continue;
143     }
144    
145     break;
146     }
147    
148     return count;
149     }
150    
151 michael 978 /* libio_basename()
152 adx 30 *
153     * input - i.e. "/usr/local/ircd/modules/m_whois.so"
154     * output - i.e. "m_whois.so"
155     * side effects - this will be overwritten on subsequent calls
156     */
157 michael 978 const char *
158     libio_basename(const char *path)
159 adx 30 {
160 michael 978 const char *s;
161 adx 30
162     if ((s = strrchr(path, '/')) == NULL)
163     s = path;
164     else
165     s++;
166    
167     return s;
168     }
169    
170     /*
171     * strlcat and strlcpy were ripped from openssh 2.5.1p2
172 michael 2916 * They had the following Copyright info:
173 adx 30 *
174     *
175     * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
176     * All rights reserved.
177     *
178     * Redistribution and use in source and binary forms, with or without
179     * modification, are permitted provided that the following conditions
180     * are met:
181     * 1. Redistributions of source code must retain the above copyright
182     * notice, this list of conditions and the following disclaimer.
183 michael 2916 * 2. Redistributions in binary form must reproduce the above copyright
184     * notice, this list of conditions and the following disclaimer in the
185     * documentation and/or other materials provided with the distribution.
186 adx 30 * 3. The name of the author may not be used to endorse or promote products
187     * derived from this software without specific prior written permission.
188     *
189     * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
190     * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
191     * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
192     * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
193     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
194     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
195     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
196     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
197     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
198     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199     */
200 michael 2916
201 adx 30 #ifndef HAVE_STRLCAT
202     size_t
203     strlcat(char *dst, const char *src, size_t siz)
204     {
205     char *d = dst;
206     const char *s = src;
207     size_t n = siz, dlen;
208    
209     while (n-- != 0 && *d != '\0')
210     d++;
211    
212     dlen = d - dst;
213     n = siz - dlen;
214    
215     if (n == 0)
216 michael 1122 return dlen + strlen(s);
217 adx 30
218     while (*s != '\0')
219     {
220     if (n != 1)
221     {
222     *d++ = *s;
223     n--;
224     }
225    
226     s++;
227     }
228    
229     *d = '\0';
230     return dlen + (s - src); /* count does not include NUL */
231     }
232     #endif
233    
234     #ifndef HAVE_STRLCPY
235     size_t
236     strlcpy(char *dst, const char *src, size_t siz)
237     {
238     char *d = dst;
239     const char *s = src;
240     size_t n = siz;
241    
242     /* Copy as many bytes as will fit */
243     if (n != 0 && --n != 0)
244     {
245     do
246     {
247     if ((*d++ = *s++) == 0)
248     break;
249     } while (--n != 0);
250     }
251    
252     /* Not enough room in dst, add NUL and traverse rest of src */
253     if (n == 0)
254     {
255     if (siz != 0)
256     *d = '\0'; /* NUL-terminate dst */
257     while (*s++)
258     ;
259     }
260    
261     return s - src - 1; /* count does not include NUL */
262     }
263     #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision