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