xref: /freebsd/usr.sbin/fifolog/fifolog_create/fifolog_create.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*-
2  * Copyright (c) 2005-2008 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <err.h>
32 #include <libutil.h>
33 
34 #include "libfifolog.h"
35 
36 #define DEF_RECSIZE	512
37 #define DEF_RECCNT	(24 * 60 * 60)
38 
39 int
40 main(int argc, char * const *argv)
41 {
42 	int ch;
43 	int64_t size;
44 	int64_t recsize;
45 	int64_t reccnt;
46 	const char *s;
47 
48 	recsize = 0;
49 	size = 0;
50 	reccnt = 0;
51 	while((ch = getopt(argc, argv, "l:r:s:")) != -1) {
52 		switch (ch) {
53 		case 'l':
54 			if (expand_number(optarg, &recsize))
55 				err(1, "Couldn't parse -l argument");
56 			break;
57 		case 'r':
58 			if (expand_number(optarg, &reccnt))
59 				err(1, "Couldn't parse -r argument");
60 			break;
61 		case 's':
62 			if (expand_number(optarg, &size))
63 				err(1, "Couldn't parse -s argument");
64 			break;
65 		default:
66 			errx(1, "Usage");
67 		}
68 	}
69 	argc -= optind;
70 	argv += optind;
71 	if (argc != 1)
72 		errx(1, "Usage");
73 
74 	if (size != 0 && reccnt != 0 && recsize != 0) {		/* N N N */
75 		if (size !=  reccnt * recsize)
76 			errx(1, "Inconsistent -l, -r and -s values");
77 	} else if (size != 0 && reccnt != 0 && recsize == 0) {	/* N N Z */
78 		if (size % reccnt)
79 			errx(1,
80 			    "Inconsistent -r and -s values (gives remainder)");
81 		recsize = size / reccnt;
82 	} else if (size != 0 && reccnt == 0 && recsize != 0) {	/* N Z N */
83 		if (size % recsize)
84 		    errx(1, "-s arg not divisible by -l arg");
85 	} else if (size != 0 && reccnt == 0 && recsize == 0) {	/* N Z Z */
86 		recsize = DEF_RECSIZE;
87 		if (size % recsize)
88 		    errx(1, "-s arg not divisible by %jd", recsize);
89 	} else if (size == 0 && reccnt != 0 && recsize != 0) {	/* Z N N */
90 		size = reccnt * recsize;
91 	} else if (size == 0 && reccnt != 0 && recsize == 0) {	/* Z N Z */
92 		recsize = DEF_RECSIZE;
93 		size = reccnt * recsize;
94 	} else if (size == 0 && reccnt == 0 && recsize != 0) {	/* Z Z N */
95 		size = DEF_RECCNT * recsize;
96 	} else if (size == 0 && reccnt == 0 && recsize == 0) {	/* Z Z Z */
97 		recsize = DEF_RECSIZE;
98 		size = DEF_RECCNT * recsize;
99 	}
100 
101 	s = fifolog_create(argv[0], size, recsize);
102 	if (s == NULL)
103 		return (0);
104 	err(1, "%s", s);
105 }
106