]>
jfr.im git - irc/thales.git/blob - src/misc.c
1 /* Thales - IRC to Relational Database Gateway
2 * Copyright (C) 2002 Lucas Nussbaum <lucas@lucas-nussbaum.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /*************************************************************************/
24 /* strscpy: Copy at most len-1 characters from a string to a buffer, and
25 * add a null terminator after the last character copied.
28 char *strscpy(char *d
, const char *s
, size_t len
)
34 while (--len
&& (*d
++ = *s
++))
40 /*************************************************************************/
42 /* stristr: Search case-insensitively for string s2 within string s1,
43 * returning the first occurrence of s2 or NULL if s2 was not
47 char *stristr(char *s1
, char *s2
)
49 register char *s
= s1
, *d
= s2
;
53 if (tolower(*s1
) == tolower(*d
))
69 /*************************************************************************/
71 /* strnrepl: Replace occurrences of `old' with `new' in string `s'. Stop
72 * replacing if a replacement would cause the string to exceed
73 * `size' bytes (including the null terminator). Return the
77 char *strnrepl(char *s
, int size
, const char *old
, const char *new)
81 int avail
= size
- (left
+ 1);
82 int oldlen
= strlen(old
);
83 int newlen
= strlen(new);
84 int diff
= newlen
- oldlen
;
86 while (left
>= oldlen
)
88 if (strncmp(ptr
, old
, oldlen
) != 0)
97 memmove(ptr
+ oldlen
+ diff
, ptr
+ oldlen
, left
+ 1);
98 strncpy(ptr
, new, newlen
);
105 /*************************************************************************/
106 /*************************************************************************/
108 /* merge_args: Take an argument count and argument vector and merge them
109 * into a single string in which each argument is separated by
113 char *merge_args(int argc
, char **argv
)
120 for (i
= 0; i
< argc
; i
++)
121 t
+= snprintf(t
, sizeof(s
) - (t
- s
), "%s%s", *argv
++,
122 (i
< argc
- 1) ? " " : "");
126 /*************************************************************************/
128 /* dotime: Return the number of seconds corresponding to the given time
129 * string. If the given string does not represent a valid time,
132 * A time string is either a plain integer (representing a number
133 * of seconds), or an integer followed by one of these characters:
134 * "s" (seconds), "m" (minutes), "h" (hours), or "d" (days).
137 int dotime(const char *s
)
141 amount
= strtol(s
, (char **) &s
, 10);
151 return amount
* 3600;
153 return amount
* 86400;
164 void strtolwr(char *ch
)