xref: /openbsd-src/usr.bin/cvs/config.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: config.c,v 1.7 2007/01/25 08:21:08 otto Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "includes.h"
19 
20 #include "cvs.h"
21 #include "config.h"
22 #include "diff.h"
23 #include "log.h"
24 
25 void
26 cvs_parse_configfile(void)
27 {
28 	FILE *fp;
29 	size_t len;
30 	struct rlimit rl;
31 	const char *errstr;
32 	char *p, *buf, *lbuf, *opt, *val, fpath[MAXPATHLEN];
33 
34 	if (cvs_path_cat(current_cvsroot->cr_dir, CVS_PATH_CONFIG,
35 	    fpath, sizeof(fpath)) >= sizeof(fpath))
36 		fatal("cvs_parse_configfile: truncation");
37 
38 	cvs_log(LP_TRACE, "cvs_parse_configfile(%s)", fpath);
39 
40 	if ((fp = fopen(fpath, "r")) == NULL)
41 		fatal("cvs_config_parse: %s: %s",
42 		    CVS_PATH_CONFIG, strerror(errno));
43 
44 	lbuf = NULL;
45 	while ((buf = fgetln(fp, &len))) {
46 		if (buf[len - 1] == '\n') {
47 			buf[len - 1] = '\0';
48 		} else {
49 			lbuf = xmalloc(len + 1);
50 			memcpy(lbuf, buf, len);
51 			lbuf[len] = '\0';
52 			buf = lbuf;
53 		}
54 
55 		p = buf;
56 		while (*p == ' ')
57 			p++;
58 
59 		if (p[0] == '#' || p[0] == '\0') {
60 			if (lbuf != NULL)
61 				xfree(lbuf);
62 			continue;
63 		}
64 
65 		opt = p;
66 		if ((val = strrchr(opt, '=')) == NULL)
67 			fatal("cvs_parse_configfile: bad option '%s'", opt);
68 
69 		*(val++) = '\0';
70 
71 		if (!strcmp(opt, "tag")) {
72 			cvs_tagname = xstrdup(val);
73 		} else if (!strcmp(opt, "umask")) {
74 			cvs_umask = (int)strtonum(val, 0, INT_MAX, &errstr);
75 			if (errstr != NULL)
76 				fatal("cvs_parse_configfile: %s: %s", val,
77 				    errstr);
78 		} else if (!strcmp(opt, "dlimit")) {
79 			if (getrlimit(RLIMIT_DATA, &rl) != -1) {
80 				rl.rlim_cur = (int)strtonum(val, 0, INT_MAX,
81 				    &errstr);
82 				if (errstr != NULL)
83 					fatal("cvs_parse_configfile: %s: %s",
84 					    val, errstr);
85 				rl.rlim_cur = rl.rlim_cur * 1024;
86 				(void)setrlimit(RLIMIT_DATA, &rl);
87 			}
88 		} else {
89 			cvs_log(LP_ERR, "ignoring unknown option '%s'", opt);
90 		}
91 
92 		if (lbuf != NULL)
93 			xfree(lbuf);
94 	}
95 
96 	(void)fclose(fp);
97 }
98