ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/irc_string.c
Revision: 8752
Committed: Tue Jan 1 11:07:01 2019 UTC (7 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 6252 byte(s)
Log Message:
- Update copyright years

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 8752 * Copyright (c) 1997-2019 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 8658 bool
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 michael 8658 return false;
43 michael 2883 }
44     else if (IsMWildChar(*p))
45 michael 8658 return true;
46 michael 2883 }
47 michael 1400
48 michael 8658 return false;
49 michael 1400 }
50    
51 michael 7797 const char *
52 michael 7726 stripws(char *txt)
53 adx 30 {
54 michael 7726 while (*txt == '\t' || *txt == ' ')
55     ++txt;
56 adx 30
57 michael 7726 char *tmp = txt + strlen(txt) - 1;
58     while (tmp >= txt && (*tmp == '\t' || *tmp == ' '))
59     --tmp;
60 adx 30
61 michael 7726 *(tmp + 1) = '\0';
62 adx 30
63 michael 7726 return txt;
64 adx 30 }
65    
66     /*
67 michael 6546 * strtok_r - walk through a string of tokens, using a set of separators
68 adx 30 * argv 9/90
69     *
70     */
71     #ifndef HAVE_STRTOK_R
72     char *
73 michael 6546 strtok_r(char *str, const char *fs, char **save)
74 adx 30 {
75 michael 3565 char *pos = *save; /* keep last position across calls */
76     char *tmp = NULL;
77 adx 30
78     if (str)
79     pos = str; /* new string scan */
80    
81 michael 3246 while (pos && *pos && strchr(fs, *pos))
82 adx 30 ++pos; /* skip leading separators */
83    
84     if (!pos || !*pos)
85 michael 4915 return pos = *save = NULL; /* string contains only sep's */
86 adx 30
87     tmp = pos; /* now, keep position of the token */
88    
89     while (*pos && strchr(fs, *pos) == NULL)
90     ++pos; /* skip content of the token */
91    
92     if (*pos)
93     *pos++ = '\0'; /* remove first sep after the token */
94     else
95     pos = NULL; /* end of string */
96    
97     *save = pos;
98     return tmp;
99     }
100     #endif /* !HAVE_STRTOK_R */
101    
102 michael 4309 /** Fill a vector of tokens from a delimited input list.
103     * Empty tokens (when \a token occurs at the start or end of \a list,
104     * or when \a token occurs adjacent to itself) are ignored. When
105     * \a size tokens have been written to \a vector, the rest of the
106     * string is ignored.
107     * \param names Input buffer.
108     * \param token Delimiter used to split \a list.
109     * \param vector Output vector.
110     * \param size Maximum number of elements to put in \a vector.
111     * \return Number of elements written to \a vector.
112     */
113 michael 7584 unsigned int
114     token_vector(char *names, char token, char *vector[], unsigned int size)
115 michael 4309 {
116 michael 7584 unsigned int count = 0;
117 michael 4309 char *start = names;
118    
119     assert(names);
120     assert(vector);
121     assert(size > 1);
122    
123     vector[count++] = start;
124    
125 michael 7584 for (char *end = strchr(start, token); end;
126     end = strchr(start, token))
127 michael 4309 {
128     *end++ = '\0';
129     start = end;
130    
131     if (*start)
132     {
133     vector[count++] = start;
134 michael 4915
135 michael 4309 if (count < size)
136     continue;
137     }
138    
139     break;
140     }
141    
142     return count;
143     }
144    
145 michael 978 /* libio_basename()
146 adx 30 *
147     * input - i.e. "/usr/local/ircd/modules/m_whois.so"
148     * output - i.e. "m_whois.so"
149     * side effects - this will be overwritten on subsequent calls
150     */
151 michael 978 const char *
152     libio_basename(const char *path)
153 adx 30 {
154 michael 978 const char *s;
155 adx 30
156     if ((s = strrchr(path, '/')) == NULL)
157     s = path;
158     else
159     s++;
160    
161     return s;
162     }
163    
164 michael 8161 /* $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $ */
165     /* $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $ */
166 adx 30 /*
167 michael 8161 * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
168 adx 30 *
169 michael 8161 * Permission to use, copy, modify, and distribute this software for any
170     * purpose with or without fee is hereby granted, provided that the above
171     * copyright notice and this permission notice appear in all copies.
172 adx 30 *
173 michael 8161 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
174     * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
175     * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
176     * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
177     * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
178     * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
179     * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
180 adx 30 */
181 michael 2916
182 adx 30 #ifndef HAVE_STRLCAT
183 michael 8161 /*
184     * Appends src to string dst of size dsize (unlike strncat, dsize is the
185     * full size of dst, not space left). At most dsize-1 characters
186     * will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
187     * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
188     * If retval >= dsize, truncation occurred.
189     */
190 adx 30 size_t
191 michael 8161 strlcat(char *dst, const char *src, size_t dsize)
192 adx 30 {
193 michael 8161 const char *odst = dst;
194     const char *osrc = src;
195     size_t n = dsize;
196     size_t dlen;
197 adx 30
198 michael 8161 /* Find the end of dst and adjust bytes left but don't go past end. */
199     while (n-- != 0 && *dst != '\0')
200     dst++;
201 adx 30
202 michael 8161 dlen = dst - odst;
203     n = dsize - dlen;
204 adx 30
205 michael 8161 if (n-- == 0)
206     return dlen + strlen(src);
207 adx 30
208 michael 8161 while (*src != '\0')
209 adx 30 {
210 michael 8161 if (n != 0)
211 adx 30 {
212 michael 8161 *dst++ = *src;
213 adx 30 n--;
214     }
215    
216 michael 8161 src++;
217 adx 30 }
218    
219 michael 8161 *dst = '\0';
220    
221     return dlen + (src - osrc); /* count does not include NUL */
222 adx 30 }
223     #endif
224    
225     #ifndef HAVE_STRLCPY
226 michael 8161 /*
227     * Copy string src to buffer dst of size dsize. At most dsize-1
228     * chars will be copied. Always NUL terminates (unless dsize == 0).
229     * Returns strlen(src); if retval >= dsize, truncation occurred.
230     */
231 adx 30 size_t
232 michael 8161 strlcpy(char *dst, const char *src, size_t dsize)
233 adx 30 {
234 michael 8161 const char *osrc = src;
235     size_t nleft = dsize;
236 adx 30
237 michael 8161 /* Copy as many bytes as will fit. */
238     if (nleft != 0)
239 adx 30 {
240 michael 8161 while (--nleft != 0)
241 adx 30 {
242 michael 8161 if ((*dst++ = *src++) == '\0')
243 adx 30 break;
244 michael 8161 }
245 adx 30 }
246    
247 michael 8161 /* Not enough room in dst, add NUL and traverse rest of src. */
248     if (nleft == 0)
249 adx 30 {
250 michael 8161 if (dsize != 0)
251     *dst = '\0'; /* NUL-terminate dst */
252    
253     while (*src++)
254 adx 30 ;
255     }
256    
257 michael 8161 return src - osrc - 1; /* count does not include NUL */
258 adx 30 }
259     #endif

Properties

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