89 lines
1.6 KiB
C
89 lines
1.6 KiB
C
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
|
||
|
#include <fcntl.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#include "bbs.h"
|
||
|
|
||
|
void die(const char *msg) {
|
||
|
dolog(msg);
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
void *malloz(size_t size) {
|
||
|
void *p = malloc(size);
|
||
|
if (p == NULL)
|
||
|
die("Out of memory");
|
||
|
memset(p, 0, size);
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
char *file2str(const char *path) {
|
||
|
struct stat s;
|
||
|
int fd;
|
||
|
|
||
|
memset(&s, 0, sizeof(s));
|
||
|
if (stat(path, &s) < 0)
|
||
|
return NULL;
|
||
|
if (!S_ISREG(s.st_mode))
|
||
|
return NULL;
|
||
|
fd = open(path, O_RDONLY);
|
||
|
if (fd < 0)
|
||
|
return NULL;
|
||
|
char *contents = malloz(s.st_size + 1);
|
||
|
if (read(fd, contents, s.st_size) != s.st_size) {
|
||
|
free(contents);
|
||
|
close(fd);
|
||
|
return NULL;
|
||
|
}
|
||
|
close(fd);
|
||
|
contents[s.st_size] = '\0';
|
||
|
return contents;
|
||
|
}
|
||
|
|
||
|
char *str5dup(const char *a, const char *b, const char *c, const char *d, const char *e) {
|
||
|
char *p;
|
||
|
size_t alen, blen, clen, dlen, elen;
|
||
|
|
||
|
if (a == NULL)
|
||
|
a = "";
|
||
|
if (b == NULL)
|
||
|
b = "";
|
||
|
if (c == NULL)
|
||
|
c = "";
|
||
|
if (d == NULL)
|
||
|
d = "";
|
||
|
if (e == NULL)
|
||
|
e = "";
|
||
|
|
||
|
alen = strlen(a);
|
||
|
blen = strlen(b);
|
||
|
clen = strlen(c);
|
||
|
dlen = strlen(d);
|
||
|
elen = strlen(e);
|
||
|
|
||
|
p = malloz(alen + blen + clen + dlen + elen + 1);
|
||
|
memmove(p, a, alen);
|
||
|
memmove(p + alen, b, blen);
|
||
|
memmove(p + alen + blen, c, clen);
|
||
|
memmove(p + alen + blen + clen, d, dlen);
|
||
|
memmove(p + alen + blen + clen + dlen, e, elen);
|
||
|
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
char *str4dup(const char *a, const char *b, const char *c, const char *d) {
|
||
|
return str5dup(a, b, c, d, "");
|
||
|
}
|
||
|
|
||
|
char *str3dup(const char *a, const char *b, const char *c) {
|
||
|
return str5dup(a, b, c, "", "");
|
||
|
}
|
||
|
|
||
|
char *str2dup(const char *a, const char *b) {
|
||
|
return str5dup(a, b, "", "", "");
|
||
|
}
|