#include "global.h"
#include "clog.h"
extern char compiled[], compiledby[], compilehost[];
static void
Usage()
{
printf( "cLOG is tool for managment syslogd operation with circular logs\n"
"=== %s === %s@%s ===\n\n"
" Syntax: clog [options] <file.log>\n\n"
"\t-i\t\tInit file.log, create and prepare for use\n"
"\t-s <size>\tUse with init for set needed file.log size\n"
"\t-f\t\tForce command\n"
"\n", compiled, compiledby, compilehost);
}
static int
initlog(const char *csLog, size_t size)
{
int f, ret = -1, fill = size;
struct clogFooter cf = { 0 };
char buffer[BUFLEN] = { 0 };
memcpy(&cf.cf_magic, MAGIC, sizeof cf.cf_magic);
cf.cf_max = size - sizeof cf;
f = open(csLog, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (f == -1) {
printf("Error:: in open log %s #%d - %s\n", csLog, errno, strerror(errno));
return -1;
}
while (fill > BUFLEN)
if (write(f, buffer, BUFLEN) == -1) {
printf("Error:: in fill log %s #%d - %s\n", csLog, errno, strerror(errno));
goto end;
} else
fill -= BUFLEN;
if (fill > BUFLEN) {
printf("Error:: in fill log %s uninspected result!!!\n", csLog);
goto end;
} else if (fill && write(f, buffer, fill) == -1) {
printf("Error:: in last fill log %s #%d - %s\n", csLog, errno, strerror(errno));
goto end;
}
// return to write cfooter
if (lseek(f, -(off_t)(sizeof cf), SEEK_END) == -1) {
printf("Error:: can`t set position for write footer #%d - %s\n", errno, strerror(errno));
goto end;
}
if (write(f, &cf, sizeof cf) == -1) {
printf("Error:: in footer log %s #%d - %s\n", csLog, errno, strerror(errno));
goto end;
}
ret = 0;
end:
close(f);
return ret;
}
static int
readlog(const char *csLog, char m)
{
int f, ret = -1;
size_t end;
struct clogFooter cf = { 0 };
char buffer[BUFLEN] = { 0 };
struct pollfd pfd = { 0 };
f = open(csLog, O_RDONLY);
if (f == -1) {
printf("Error:: in open read %s #%d - %s\n", csLog, errno, strerror(errno));
return -1;
}
end = lseek(f, 0, SEEK_END);
if (end == -1) {
printf("Error:: in get size %s #%d - %s\n", csLog, errno, strerror(errno));
goto end;
} else
lseek(f, 0, SEEK_SET);
ret = 0;
end:
close(f);
return ret;
}
int
main(int argc, char **argv)
{
char ch, m = 0, szLog[MAXPATHLEN];
size_t siz = 0;
while ((ch = getopt(argc, argv, "hfis:")) != -1)
switch (ch) {
case 'i':
m |= 1;
break;
case 'f':
m |= 2;
case 's':
siz = strtol(optarg, NULL, 0);
if (siz < 1) {
printf("Error:: size is invalid %u!\n", siz);
Usage();
return 1;
}
break;
case 'h':
default:
Usage();
return 1;
}
argc -= optind;
argv += optind;
if (m & 1 && !siz) {
printf("Error:: not specified size when use with init log!\n");
Usage();
return 1;
}
if (m == 3) {
printf("Error:: can`t in same time init and force!\n");
Usage();
return 1;
}
if (!argc) {
printf("Error:: not specified log file!\n");
Usage();
return 1;
} else
strlcpy(szLog, *argv, MAXPATHLEN);
if (m & 1 && initlog(szLog, siz) == -1)
return 2;
if (readlog(szLog, m) == -1)
return 3;
return 0;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>