From 61e0cb5dddb89ddab1d68791eb28d892c114622f Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sun, 3 Mar 2013 09:58:49 +0100 Subject: Open files in binary mode on windows So that the Windows' C library doesn't munge line endings --- common/compat.h | 4 ++++ p11-kit/conf.c | 2 +- p11-kit/pin.c | 2 +- tools/tests/test.c | 11 +++++------ 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/common/compat.h b/common/compat.h index 7eb42a5..d4858f0 100644 --- a/common/compat.h +++ b/common/compat.h @@ -64,6 +64,10 @@ #endif #endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + #ifndef HAVE_GETPROGNAME const char * getprogname (void); #endif diff --git a/p11-kit/conf.c b/p11-kit/conf.c index 28008aa..778ea00 100644 --- a/p11-kit/conf.c +++ b/p11-kit/conf.c @@ -125,7 +125,7 @@ read_config_file (const char* filename, int flags) assert (filename); - f = fopen (filename, "r"); + f = fopen (filename, "rb"); if (f == NULL) { error = errno; if ((flags & CONF_IGNORE_MISSING) && diff --git a/p11-kit/pin.c b/p11-kit/pin.c index afcb8ca..dac635c 100644 --- a/p11-kit/pin.c +++ b/p11-kit/pin.c @@ -465,7 +465,7 @@ p11_kit_pin_file_callback (const char *pin_source, if (pin_flags & P11_KIT_PIN_FLAGS_RETRY) return NULL; - fd = open (pin_source, O_RDONLY); + fd = open (pin_source, O_BINARY | O_RDONLY); if (fd == -1) return NULL; diff --git a/tools/tests/test.c b/tools/tests/test.c index c445099..61fda83 100644 --- a/tools/tests/test.c +++ b/tools/tests/test.c @@ -54,20 +54,19 @@ read_file (CuTest *tc, const char *filename, long *len) { + struct stat sb; FILE *f = NULL; char *data; - f = fopen (filename, "r"); + f = fopen (filename, "rb"); if (f == NULL) CuFail_Line (tc, file, line, "Couldn't open file", filename); /* Figure out size */ - if (fseek (f, 0, SEEK_END) == -1 || - (*len = ftell (f)) == -1 || - fseek (f, 0, SEEK_SET) == -1) { - CuFail_Line (tc, file, line, "Couldn't get file length", filename); - } + if (stat (filename, &sb) < 0) + CuFail_Line (tc, file, line, "Couldn't stat file", filename); + *len = sb.st_size; data = malloc (*len ? *len : 1); assert (data != NULL); -- cgit v1.1