aboutsummaryrefslogtreecommitdiff
path: root/cfg.c
diff options
context:
space:
mode:
authorThomas Adam <thomas@xteddy.org>2012-12-08 20:29:37 +0000
committerThomas Adam <thomas@xteddy.org>2012-12-08 20:29:37 +0000
commitfb83914bd745996429374122f2289f2930170f71 (patch)
tree17e80c94f1605da8eebb6565244baee0cedb82d7 /cfg.c
parentd5de489dc4ae2e5eaa9e5dee86094afb43cc1387 (diff)
parent3fa4f691e32482b2d07d16be84b6e22657f9c7dd (diff)
downloadrtmux-fb83914bd745996429374122f2289f2930170f71.tar.gz
rtmux-fb83914bd745996429374122f2289f2930170f71.tar.bz2
rtmux-fb83914bd745996429374122f2289f2930170f71.zip
Merge branch 'obsd-master'
Sync from OpenBSD.
Diffstat (limited to 'cfg.c')
-rw-r--r--cfg.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/cfg.c b/cfg.c
index 74570e36..eb46bd94 100644
--- a/cfg.c
+++ b/cfg.c
@@ -73,46 +73,50 @@ cfg_add_cause(struct causelist *causes, const char *fmt, ...)
* Load configuration file. Returns -1 for an error with a list of messages in
* causes. Note that causes must be initialised by the caller!
*/
-int
+enum cmd_retval
load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
{
FILE *f;
u_int n;
char *buf, *line, *cause;
- size_t len;
+ size_t len, newlen;
struct cmd_list *cmdlist;
struct cmd_ctx ctx;
enum cmd_retval retval;
if ((f = fopen(path, "rb")) == NULL) {
cfg_add_cause(causes, "%s: %s", path, strerror(errno));
- return (-1);
+ return (CMD_RETURN_ERROR);
}
- n = 0;
cfg_references++;
+ n = 0;
line = NULL;
retval = CMD_RETURN_NORMAL;
while ((buf = fgetln(f, &len))) {
if (buf[len - 1] == '\n')
len--;
- if (line != NULL)
- line = xrealloc(line, 1, strlen(line) + len + 1);
- else {
- line = xmalloc(len + 1);
+ /* Current line is the continuation of the previous one. */
+ if (line != NULL) {
+ newlen = strlen(line) + len + 1;
+ line = xrealloc(line, 1, newlen);
+ } else {
+ newlen = len + 1;
+ line = xmalloc(newlen);
*line = '\0';
}
- /* Append buffer to line. strncat will terminate. */
- strncat(line, buf, len);
+ /* Append current line to the previous. */
+ strlcat(line, buf, newlen);
n++;
/* Continuation: get next line? */
len = strlen(line);
if (len > 0 && line[len - 1] == '\\') {
line[len - 1] = '\0';
+
/* Ignore escaped backslash at EOL. */
if (len > 1 && line[len - 2] != '\\')
continue;
@@ -127,11 +131,10 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
free(cause);
continue;
- } else
- free(buf);
+ }
+ free(buf);
if (cmdlist == NULL)
continue;
- cfg_cause = NULL;
if (ctxin == NULL) {
ctx.msgdata = NULL;
@@ -162,8 +165,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
}
cmd_list_free(cmdlist);
if (cfg_cause != NULL) {
- cfg_add_cause(
- causes, "%s: %d: %s", path, n, cfg_cause);
+ cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
free(cfg_cause);
}
}