xbot/src/util.c

81 lines
1.2 KiB
C
Raw Normal View History

2015-03-24 03:12:35 -07: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 16:20:59 -07:00
#include <ctype.h>
2015-03-24 03:12:35 -07:00
2024-03-06 01:45:01 -08:00
#include "util.h"
2024-02-29 18:49:33 -08:00
2015-03-24 03:12:35 -07:00
void eprint(char *fmt, ...)
{
2024-03-09 01:38:58 -08:00
char msg[4096];
2024-02-12 23:22:10 -08:00
char bufout[4096];
va_list ap;
va_start(ap, fmt);
2024-03-09 01:38:58 -08:00
vsnprintf(msg, sizeof msg, fmt, ap);
2024-02-12 23:22:10 -08:00
va_end(ap);
2024-03-09 01:38:58 -08:00
sprintf(bufout, "%s", msg);
2024-02-12 23:22:10 -08:00
if (fmt[0] && fmt[strlen(fmt) - 1] == ':')
{
2024-03-09 01:38:58 -08:00
sprintf(bufout, "%s\n", strerror(errno));
2024-02-12 23:22:10 -08:00
}
2024-03-09 01:38:58 -08:00
fprintf(stderr, "%s", bufout);
xlog("%s", bufout);
2015-03-24 03:12:35 -07:00
}
2024-03-11 03:22:05 -07:00
2024-02-12 23:22:10 -08:00
#if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 38)) || defined(_WIN32)
2024-03-06 01:45:01 -08:00
void strlcpy(char *to, const char *from, int len)
2015-03-24 03:12:35 -07:00
{
2024-02-12 23:22:10 -08:00
memccpy(to, from, '\0', len);
to[len-1] = '\0';
2015-03-24 03:12:35 -07:00
}
2024-02-12 23:22:10 -08:00
#endif
2015-03-24 03:12:35 -07:00
2024-02-29 18:49:33 -08:00
#ifdef _WIN32
2024-03-06 01:45:01 -08:00
char *basename(char *path)
2024-02-29 18:49:33 -08:00
{
char *p = strrchr(path, '\\');
return p ? p + 1 : path;
}
#endif
2024-03-11 03:22:05 -07:00
2024-03-06 01:45:01 -08:00
char *skip(char *s, char c)
2015-03-24 03:12:35 -07:00
{
2024-02-12 23:22:10 -08:00
while (*s != c && *s != '\0')
{
s++;
}
2015-03-24 03:12:35 -07:00
2024-02-12 23:22:10 -08:00
if (*s != '\0')
{
*s++ = '\0';
}
2015-03-24 03:12:35 -07:00
2024-02-12 23:22:10 -08:00
return s;
2015-03-24 03:12:35 -07:00
}
void trim(char *s)
{
2024-02-12 23:22:10 -08:00
char *e;
2015-03-24 03:12:35 -07:00
2024-02-12 23:22:10 -08:00
e = s + strlen(s) - 1;
while (isspace(*e) && e > s)
{
e--;
}
2015-03-24 03:12:35 -07:00
2024-02-12 23:22:10 -08:00
*(e + 1) = '\0';
}