2 * IRC - Internet Relay Chat, common/support.c
3 * Copyright (C) 1990, 1991 Armin Gruner
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <sys/timers.h>
35 #include "sprintf_irc.h"
45 * Walk through a string of tokens, using a set of separators.
48 char *strtoken(char **save, char *str, char *fs)
50 char *pos = *save; /* keep last position across calls */
54 pos = str; /* new string scan */
56 while (pos && *pos && strchr(fs, *pos) != NULL)
57 pos++; /* skip leading separators */
60 return (pos = *save = NULL); /* string contains only sep's */
62 tmp = pos; /* now, keep position of the token */
64 while (*pos && strchr(fs, *pos) == NULL)
65 pos++; /* skip content of the token */
68 *pos++ = '\0'; /* remove first sep after the token */
70 pos = NULL; /* end of string */
75 #endif /* !HAVE_STRTOKEN */
81 * Returns an appropriate system error string to a given errno.
85 char *strerror(int err_no)
90 errp = (err_no > sys_nerr ? (char *)NULL : sys_errlist[err_no]);
92 if (errp == (char *)NULL)
95 sprintf_irc(errp, "Unknown Error %d", err_no);
100 #endif /* !HAVE_STRERROR */
103 * inetntoa -- Changed the parameter to NOT take a pointer.
105 * inetntoa -- Changed name to remove collision possibility and
106 * so behaviour is garanteed to take a pointer arg.
108 * inet_ntoa -- Returned the dotted notation of a given
109 * internet number (some ULTRIX don't have this)
111 * inet_ntoa -- Its broken on some Ultrix/Dynix too. -avalon
113 char *inetntoa(struct in_addr in)
116 Reg1 unsigned char *s = (unsigned char *)&in.s_addr;
117 Reg2 unsigned char a, b, c, d;
123 sprintf_irc(buf, "%u.%u.%u.%u", a, b, c, d);
128 #ifndef HAVE_INET_NETOF
130 * inet_netof -- return the net portion of an internet number
133 int inet_netof(struct in_addr in)
137 if (addr & 0x80 == 0)
138 return ((int)in.s_net);
140 if (addr & 0x40 == 0)
141 return ((int)in.s_net * 256 + in.s_host);
143 return ((int)in.s_net * 256 + in.s_host * 256 + in.s_lh);
146 #endif /* !HAVE_INET_NETOF */
148 #ifndef HAVE_GETTIMEOFDAY
149 /* This is copied from ircu3.0.0 (with permission), not vica versa. */
150 int gettimeofday(struct timeval *tv, void * /*UNUSED*/)
153 static struct timespec tp;
155 if ((ret = getclock(TIMEOFDAY, &tp)))
157 tv->tv_sec = (long)tp.tv_sec;
158 tv->tv_usec = (tp.tv_nsec + 500) / 1000;
161 #endif /* !HAVE_GETTIMEOFDAY */
165 void dumpcore(const char *pattern, ...)
168 static time_t lastd = 0;
169 static int dumps = 0;
174 va_start(vl, pattern);
180 else if (now - lastd < 60 && dumps > 2)
186 if (now - lastd > 60)
200 sprintf_irc(corename, "core.%d", p);
201 rename("core", corename);
202 Debug((DEBUG_FATAL, "Dumped core : core.%d", p));
203 sendto_ops("Dumped core : core.%d", p);
204 vdebug(DEBUG_FATAL, pattern, vl);
205 vsendto_ops(pattern, vl);
216 int check_if_ipmask(const char *mask)
219 register const char *p;
221 for (p = mask; *p; ++p)
222 if (*p != '*' && *p != '?' && *p != '.')
232 /* Moved from logf() in whocmds.c to here. Modified a
233 * bit and used for most logging now.
234 * -Ghostwolf 12-Jul-99
237 extern void write_log(const char *filename, const char *pattern, ...)
241 static char logbuf[1024];
243 logfile = fbopen(filename, "a");
247 va_start(vl, pattern);
248 vsprintf_irc(logbuf, pattern, vl);
251 fbputs(logbuf, logfile);