--- l2_ch_file.c 2003/01/06 11:41:51 1.25
+++ l2_ch_file.c 2003/06/30 11:13:08 1.26
@@ -39,6 +39,7 @@
int fd;
char *path;
int append;
+ int trunc;
int perm;
} l2_ch_file_t;
@@ -54,7 +55,8 @@
/* initialize configuration with reasonable defaults */
cfg->fd = -1;
cfg->path = NULL;
- cfg->append = TRUE;
+ cfg->append = -1;
+ cfg->trunc = -1;
cfg->perm = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
/* link private channel configuration into channel context */
@@ -74,11 +76,13 @@
/* feed and call generic parameter parsing engine */
L2_PARAM_SET(pa[0], path, STR, &cfg->path);
L2_PARAM_SET(pa[1], append, INT, &cfg->append);
- L2_PARAM_SET(pa[2], perm, INT, &cfg->perm);
- L2_PARAM_END(pa[3]);
+ L2_PARAM_SET(pa[2], trunc, INT, &cfg->trunc);
+ L2_PARAM_SET(pa[3], perm, INT, &cfg->perm);
+ L2_PARAM_END(pa[4]);
l2_channel_env(ch, &env);
rv = l2_util_setparams(env, pa, fmt, ap);
+
return rv;
}
@@ -89,16 +93,42 @@
int mode;
mode_t mask;
+ /* "append" backward compatibility; only cfg->trunc is used in the code
+ * make sure append/trunc either both use defaults, both are set different, or only one is set
+ *
+ * truth table for user input, append, trunc => resulting trunc
+ * -----------------+------+------+------------------
+ * -1 -1 0 (default)
+ * trunc=0 -1 0 0
+ * trunc=1 -1 1 1
+ * append=0 0 -1 1
+ * append=0, trunc=0 0 0 ERROR
+ * append=0, trunc=1 0 1 1
+ * append=1 1 -1 0
+ * append=1, trunc=0 1 0 0
+ * append=1, trunc=1 1 1 ERROR
+ */
+ if (cfg->append >= 1)
+ cfg->append = 1; /* reduce to -1 (undef), 0 (no), 1 (yes) */
+ if (cfg->trunc >= 1)
+ cfg->trunc = 1; /* reduce to -1 (undef), 0 (no), 1 (yes) */
+ if ( cfg->append != -1
+ && cfg->trunc != -1
+ && cfg->append == cfg->trunc) /* collision */
+ return L2_ERR_USE;
+ if ( cfg->trunc == -1)
+ cfg->trunc = (1 - cfg->append) & 1;
+
/* make sure a path was set */
if (cfg->path == NULL)
return L2_ERR_USE;
/* open channel file */
mode = O_WRONLY|O_CREAT;
- if (cfg->append)
- mode |= O_APPEND;
- else
+ if (cfg->trunc == 1)
mode |= O_TRUNC;
+ else
+ mode |= O_APPEND;
mask = umask(0);
cfg->fd = open(cfg->path, mode, cfg->perm);
umask(mask);
|