1 /* $OpenBSD: config.c,v 1.15 2008/03/02 11:58:45 joris 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 <sys/param.h> 19 #include <sys/dirent.h> 20 #include <sys/resource.h> 21 22 #include <errno.h> 23 #include <limits.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 #include "cvs.h" 28 #include "config.h" 29 30 void 31 cvs_parse_configfile(void) 32 { 33 cvs_log(LP_TRACE, "cvs_parse_configfile()"); 34 cvs_read_config(CVS_PATH_CONFIG, config_parse_line); 35 } 36 37 int 38 config_parse_line(char *line, int lineno) 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 } 76 77 return (0); 78 } 79 80 void 81 cvs_read_config(char *name, int (*cb)(char *, int)) 82 { 83 FILE *fp; 84 size_t len; 85 int lineno; 86 char *p, *buf, *lbuf, fpath[MAXPATHLEN]; 87 88 (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", 89 current_cvsroot->cr_dir, name); 90 91 if ((fp = fopen(fpath, "r")) == NULL) 92 return; 93 94 lbuf = NULL; 95 lineno = 0; 96 while ((buf = fgetln(fp, &len)) != NULL) { 97 lineno++; 98 if (buf[len - 1] == '\n') { 99 buf[len - 1] = '\0'; 100 } else { 101 lbuf = xmalloc(len + 1); 102 memcpy(lbuf, buf, len); 103 lbuf[len] = '\0'; 104 buf = lbuf; 105 } 106 107 p = buf; 108 while (*p == ' ' || *p == '\t') 109 p++; 110 111 if (p[0] == '#' || p[0] == '\0') 112 continue; 113 114 if (cb(p, lineno) < 0) 115 break; 116 } 117 118 if (lbuf != NULL) 119 xfree(lbuf); 120 121 (void)fclose(fp); 122 } 123