Annotation of src/usr.bin/cvs/config.c, Revision 1.15
1.15 ! joris 1: /* $OpenBSD: config.c,v 1.14 2008/02/04 19:08:32 joris Exp $ */
1.1 joris 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:
1.9 otto 18: #include <sys/param.h>
19: #include <sys/dirent.h>
20: #include <sys/resource.h>
21:
22: #include <errno.h>
1.12 tobias 23: #include <limits.h>
1.9 otto 24: #include <stdlib.h>
25: #include <string.h>
1.1 joris 26:
27: #include "cvs.h"
28: #include "config.h"
29:
30: void
31: cvs_parse_configfile(void)
32: {
1.13 joris 33: cvs_log(LP_TRACE, "cvs_parse_configfile()");
34: cvs_read_config(CVS_PATH_CONFIG, config_parse_line);
35: }
36:
1.15 ! joris 37: int
1.14 joris 38: config_parse_line(char *line, int lineno)
1.13 joris 39: {
40: struct rlimit rl;
41: const char *errstr;
42: char *val, *opt, *ep;
43:
44: opt = line;
45: if ((val = strrchr(opt, '=')) == NULL)
46: fatal("cvs_parse_configfile: bad option '%s'", opt);
47:
48: *(val++) = '\0';
49:
50: if (!strcmp(opt, "tag")) {
51: if (cvs_tagname != NULL)
52: xfree(cvs_tagname);
53: cvs_tagname = xstrdup(val);
54: } else if (!strcmp(opt, "umask")) {
55: cvs_umask = strtol(val, &ep, 8);
56:
57: if (val == ep || *ep != '\0')
58: fatal("cvs_parse_configfile: umask %s is "
59: "invalid", val);
60: if (cvs_umask < 0 || cvs_umask > 07777)
61: fatal("cvs_parse_configfile: umask %s is "
62: "invalid", val);
63: } else if (!strcmp(opt, "dlimit")) {
64: if (getrlimit(RLIMIT_DATA, &rl) != -1) {
65: rl.rlim_cur = (int)strtonum(val, 0, INT_MAX,
66: &errstr);
67: if (errstr != NULL)
68: fatal("cvs_parse_configfile: %s: %s",
69: val, errstr);
70: rl.rlim_cur = rl.rlim_cur * 1024;
71: (void)setrlimit(RLIMIT_DATA, &rl);
72: }
73: } else {
74: cvs_log(LP_ERR, "ignoring unknown option '%s'", opt);
75: }
1.15 ! joris 76:
! 77: return (0);
1.13 joris 78: }
79:
80: void
1.15 ! joris 81: cvs_read_config(char *name, int (*cb)(char *, int))
1.13 joris 82: {
1.1 joris 83: FILE *fp;
84: size_t len;
1.14 joris 85: int lineno;
1.13 joris 86: char *p, *buf, *lbuf, fpath[MAXPATHLEN];
1.1 joris 87:
1.8 xsa 88: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
1.13 joris 89: current_cvsroot->cr_dir, name);
1.6 xsa 90:
1.13 joris 91: if ((fp = fopen(fpath, "r")) == NULL)
1.10 xsa 92: return;
1.1 joris 93:
94: lbuf = NULL;
1.14 joris 95: lineno = 0;
1.13 joris 96: while ((buf = fgetln(fp, &len)) != NULL) {
1.14 joris 97: lineno++;
1.1 joris 98: if (buf[len - 1] == '\n') {
99: buf[len - 1] = '\0';
100: } else {
101: lbuf = xmalloc(len + 1);
1.7 otto 102: memcpy(lbuf, buf, len);
103: lbuf[len] = '\0';
1.1 joris 104: buf = lbuf;
105: }
106:
1.2 joris 107: p = buf;
1.13 joris 108: while (*p == ' ' || *p == '\t')
1.2 joris 109: p++;
110:
1.13 joris 111: if (p[0] == '#' || p[0] == '\0')
1.2 joris 112: continue;
113:
1.15 ! joris 114: if (cb(p, lineno) < 0)
! 115: break;
1.13 joris 116: }
1.1 joris 117:
1.13 joris 118: if (lbuf != NULL)
119: xfree(lbuf);
1.1 joris 120:
121: (void)fclose(fp);
122: }