From c727e8698020f69516cb62e001ac6f8254025fe1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 20 May 2010 06:59:25 +0200 Subject: input/file, output/{fifo,recorder}: add O_BINARY to open() flags Windows compatibility. --- src/input/file_input_plugin.c | 4 ++-- src/open.h | 41 +++++++++++++++++++++++++++++++++++++ src/output/fifo_output_plugin.c | 6 +++--- src/output/recorder_output_plugin.c | 5 +++-- 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/open.h diff --git a/src/input/file_input_plugin.c b/src/input/file_input_plugin.c index a18de67b8..3646c656e 100644 --- a/src/input/file_input_plugin.c +++ b/src/input/file_input_plugin.c @@ -21,9 +21,9 @@ #include "input/file_input_plugin.h" #include "input_plugin.h" #include "fd_util.h" +#include "open.h" #include -#include #include #include #include @@ -54,7 +54,7 @@ input_file_open(const char *filename, GError **error_r) if (!g_path_is_absolute(filename)) return false; - fd = open_cloexec(filename, O_RDONLY, 0); + fd = open_cloexec(filename, O_RDONLY|O_BINARY, 0); if (fd < 0) { if (errno != ENOENT && errno != ENOTDIR) g_set_error(error_r, file_quark(), errno, diff --git a/src/open.h b/src/open.h new file mode 100644 index 000000000..e39c64a97 --- /dev/null +++ b/src/open.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2010 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** \file + * + * Portability macros for opening files. + */ + +#ifndef MPD_OPEN_H +#define MPD_OPEN_H + +#include + +/* On Windows, files are opened in "text" mode by default, and the C + library will mangle data being read/written; this must be switched + off by specifying the proprietary "O_BINARY" flag. That sucks! */ +#ifndef O_BINARY +#ifdef _O_BINARY +#define O_BINARY _O_BINARY +#else +#define O_BINARY 0 +#endif +#endif + +#endif diff --git a/src/output/fifo_output_plugin.c b/src/output/fifo_output_plugin.c index a92c689f9..f4217ec4d 100644 --- a/src/output/fifo_output_plugin.c +++ b/src/output/fifo_output_plugin.c @@ -22,12 +22,12 @@ #include "utils.h" #include "timer.h" #include "fd_util.h" +#include "open.h" #include #include #include -#include #include #include #include @@ -154,7 +154,7 @@ fifo_open(struct fifo_data *fd, GError **error) if (!fifo_check(fd, error)) return false; - fd->input = open_cloexec(fd->path, O_RDONLY|O_NONBLOCK, 0); + fd->input = open_cloexec(fd->path, O_RDONLY|O_NONBLOCK|O_BINARY, 0); if (fd->input < 0) { g_set_error(error, fifo_output_quark(), errno, "Could not open FIFO \"%s\" for reading: %s", @@ -163,7 +163,7 @@ fifo_open(struct fifo_data *fd, GError **error) return false; } - fd->output = open_cloexec(fd->path, O_WRONLY|O_NONBLOCK, 0); + fd->output = open_cloexec(fd->path, O_WRONLY|O_NONBLOCK|O_BINARY, 0); if (fd->output < 0) { g_set_error(error, fifo_output_quark(), errno, "Could not open FIFO \"%s\" for writing: %s", diff --git a/src/output/recorder_output_plugin.c b/src/output/recorder_output_plugin.c index ce69a5116..c01d927c4 100644 --- a/src/output/recorder_output_plugin.c +++ b/src/output/recorder_output_plugin.c @@ -22,11 +22,11 @@ #include "encoder_plugin.h" #include "encoder_list.h" #include "fd_util.h" +#include "open.h" #include #include #include -#include #include #include @@ -158,7 +158,8 @@ recorder_output_open(void *data, struct audio_format *audio_format, /* create the output file */ - recorder->fd = open_cloexec(recorder->path, O_CREAT|O_WRONLY|O_TRUNC, + recorder->fd = open_cloexec(recorder->path, + O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, 0666); if (recorder->fd < 0) { g_set_error(error_r, recorder_output_quark(), 0, -- cgit v1.2.3