xbot/src/util.c

81 lines
1.2 KiB
C
Raw Normal View History

2015-03-24 10:12:35 +00:00
/*
* xbot: Just another IRC bot
*
* Written by Aaron Blakely <aaron@ephasic.org>
**/
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
2015-03-26 23:20:59 +00:00
#include <ctype.h>
2015-03-24 10:12:35 +00:00
2024-03-06 09:45:01 +00:00
#include "util.h"
2024-03-01 02:49:33 +00:00
2015-03-24 10:12:35 +00:00
void eprint(char *fmt, ...)
{
2024-03-09 09:38:58 +00:00
char msg[4096];
2024-02-13 07:22:10 +00:00
char bufout[4096];
va_list ap;
va_start(ap, fmt);
2024-03-09 09:38:58 +00:00
vsnprintf(msg, sizeof msg, fmt, ap);
2024-02-13 07:22:10 +00:00
va_end(ap);
2024-03-09 09:38:58 +00:00
sprintf(bufout, "%s", msg);
2024-02-13 07:22:10 +00:00
if (fmt[0] && fmt[strlen(fmt) - 1] == ':')
{
2024-03-09 09:38:58 +00:00
sprintf(bufout, "%s\n", strerror(errno));
2024-02-13 07:22:10 +00:00
}
2024-03-09 09:38:58 +00:00
fprintf(stderr, "%s", bufout);
xlog("%s", bufout);
2015-03-24 10:12:35 +00:00
}
2024-03-11 10:22:05 +00:00
2024-02-13 07:22:10 +00:00
#if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 38)) || defined(_WIN32)
2024-03-06 09:45:01 +00:00
void strlcpy(char *to, const char *from, int len)
2015-03-24 10:12:35 +00:00
{
2024-02-13 07:22:10 +00:00
memccpy(to, from, '\0', len);
to[len-1] = '\0';
2015-03-24 10:12:35 +00:00
}
2024-02-13 07:22:10 +00:00
#endif
2015-03-24 10:12:35 +00:00
2024-03-01 02:49:33 +00:00
#ifdef _WIN32
2024-03-06 09:45:01 +00:00
char *basename(char *path)
2024-03-01 02:49:33 +00:00
{
char *p = strrchr(path, '\\');
return p ? p + 1 : path;
}
#endif
2024-03-11 10:22:05 +00:00
2024-03-06 09:45:01 +00:00
char *skip(char *s, char c)
2015-03-24 10:12:35 +00:00
{
2024-02-13 07:22:10 +00:00
while (*s != c && *s != '\0')
{
s++;
}
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
if (*s != '\0')
{
*s++ = '\0';
}
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
return s;
2015-03-24 10:12:35 +00:00
}
void trim(char *s)
{
2024-02-13 07:22:10 +00:00
char *e;
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
e = s + strlen(s) - 1;
while (isspace(*e) && e > s)
{
e--;
}
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
*(e + 1) = '\0';
}