File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / Attic / clog.c
Revision 1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Tue Sep 28 15:50:21 2010 UTC (14 years, 3 months ago) by misho
Branches: tools1_0
add init log

#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, 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;
	}

end:
	close(f);
	return 0;
}


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;

	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>