summaryrefslogtreecommitdiffstats
path: root/stream.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-04-14 06:43:11 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-04-14 06:43:11 -0700
commit10b6ffd9a5409dc578dfaa58e0d137d3b6c74657 (patch)
treedd439448e4cc50279b763033e86e89ce6f98ad45 /stream.c
parentf5aa16fcc4431d7134da691891b489f6a74da944 (diff)
downloadtxr-10b6ffd9a5409dc578dfaa58e0d137d3b6c74657.tar.gz
txr-10b6ffd9a5409dc578dfaa58e0d137d3b6c74657.tar.bz2
txr-10b6ffd9a5409dc578dfaa58e0d137d3b6c74657.zip
Socket mode strings defaulted and checked.
* stream.c (do_parse_mode): New static function. (parse_mode): Logic moved into do_parse_mode, leaving this function as a wrapper for do_parse_mode. Check added for malformed mode, so functions which call parse_mode now have the check. Added defaulting for mode_str argument, inside do_parse_mode. (normalize_mode): Call do_parse_mode instead of parse_mode. Don't default mode_str argument, since do_parse_mode does that.
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/stream.c b/stream.c
index e07defb4..31cff28d 100644
--- a/stream.c
+++ b/stream.c
@@ -1155,10 +1155,10 @@ static struct strm_ops pipe_ops =
stdio_clear_error,
stdio_get_fd);
-struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl)
+static struct stdio_mode do_parse_mode(val mode_str, struct stdio_mode m_dfl)
{
struct stdio_mode m = stdio_mode_init_blank;
- const wchar_t *ms = c_str(mode_str);
+ const wchar_t *ms = c_str(default_arg(mode_str, lit("")));
switch (*ms) {
case 'r':
@@ -1228,6 +1228,14 @@ struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl)
return m;
}
+struct stdio_mode parse_mode(val mode_str, struct stdio_mode m_dfl)
+{
+ struct stdio_mode m = do_parse_mode(mode_str, m_dfl);
+ if (m.malformed)
+ uw_throwf(file_error_s, lit("invalid mode string ~a"), mode_str, nao);
+ return m;
+}
+
static val format_mode(const struct stdio_mode m)
{
wchar_t buf[8], *ptr = buf;
@@ -1256,11 +1264,9 @@ static val format_mode(const struct stdio_mode m)
return string(buf);
}
-val normalize_mode(struct stdio_mode *m, val mode_str_in, struct stdio_mode m_dfl)
+val normalize_mode(struct stdio_mode *m, val mode_str, struct stdio_mode m_dfl)
{
- val mode_str = default_arg(mode_str_in, lit(""));
-
- *m = parse_mode(mode_str, m_dfl);
+ *m = do_parse_mode(mode_str, m_dfl);
if (m->malformed)
uw_throwf(file_error_s, lit("invalid file open mode ~a"), mode_str, nao);