Use system headers a bit more: use host's fcntl.h for O_RDONLY etc.
Removes the need to fix up those in the simulator.

Also work around some posix-mingw incompatibilities (e.g. getcwd()).

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27904 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/codecs/libtta/ttadec.c b/apps/codecs/libtta/ttadec.c
index 9d53a32..cdaffcd 100644
--- a/apps/codecs/libtta/ttadec.c
+++ b/apps/codecs/libtta/ttadec.c
@@ -77,7 +77,7 @@
 /********************* rockbox helper functions *************************/
 
 /* emulate stdio functions */
-static int fread(void *ptr, size_t size, size_t nobj)
+static size_t tta_fread(void *ptr, size_t size, size_t nobj)
 {
     size_t read_size;
     unsigned char *buffer = ci->request_buffer(&read_size, size * nobj);
@@ -90,7 +90,7 @@
     return read_size;
 }
 
-static int fseek(long offset, int origin)
+static int tta_fseek(long offset, int origin)
 {
     switch (origin)
     {
@@ -129,7 +129,7 @@
 #define GET_BINARY(value, bits) \
     while (bit_count < bits) { \
         if (bitpos == iso_buffers_end) { \
-            if (!fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
+            if (!tta_fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
                 ttainfo->STATE = READ_ERROR; \
                 return -1; \
             } \
@@ -149,7 +149,7 @@
     value = 0; \
     while (!(bit_cache ^ bit_mask[bit_count])) { \
         if (bitpos == iso_buffers_end) { \
-            if (!fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
+            if (!tta_fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
                 ttainfo->STATE = READ_ERROR; \
                 return -1; \
             } \
@@ -207,7 +207,7 @@
 
     if (rbytes < sizeof(int)) {
         ci->memcpy(isobuffers, bitpos, 4);
-        if (!fread(isobuffers + rbytes, 1, ISO_BUFFERS_SIZE - rbytes))
+        if (!tta_fread(isobuffers + rbytes, 1, ISO_BUFFERS_SIZE - rbytes))
             return -1;
         bitpos = isobuffers;
     }
@@ -249,10 +249,10 @@
     ci->memset (info, 0, sizeof(tta_info));
 
     /* skip id3v2 tags */
-    fseek(ci->id3->id3v2len, SEEK_SET);
+    tta_fseek(ci->id3->id3v2len, SEEK_SET);
 
     /* read TTA header */
-    if (fread (&ttahdr, 1, sizeof (ttahdr)) == 0) {
+    if (tta_fread (&ttahdr, 1, sizeof (ttahdr)) == 0) {
         info->STATE = READ_ERROR;
         return -1;
     }
@@ -374,7 +374,7 @@
         return -1;
     }
     seek_pos = ttainfo->DATAPOS + seek_table[data_pos = pos];
-    if (fseek(seek_pos, SEEK_SET) < 0) {
+    if (tta_fseek(seek_pos, SEEK_SET) < 0) {
         ttainfo->STATE = READ_ERROR;
         return -1;
     }
@@ -418,7 +418,7 @@
     }
 
     /* read seek table */
-    if (!fread(seek_table, st_size, 1)) {
+    if (!tta_fread(seek_table, st_size, 1)) {
         ttainfo->STATE = READ_ERROR;
         return -1;
     }
diff --git a/apps/language.c b/apps/language.c
index fea4fb3..39903c4 100644
--- a/apps/language.c
+++ b/apps/language.c
@@ -19,7 +19,7 @@
  *
  ****************************************************************************/
 
-#include <file.h>
+#include "file.h"
 
 #include "language.h"
 #include "lang.h"
diff --git a/apps/metadata.h b/apps/metadata.h
index b73d92b..39da30e 100644
--- a/apps/metadata.h
+++ b/apps/metadata.h
@@ -23,8 +23,8 @@
 #define _METADATA_H
 
 #include <stdbool.h>
-#include "file.h"
 #include "config.h"
+#include "file.h"
 
 
 /* Audio file types. */
diff --git a/apps/tree.c b/apps/tree.c
index 4d915ca..730c59f 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -511,13 +511,13 @@
 
 /* Returns the current working directory and also writes cwd to buf if
    non-NULL.  In case of error, returns NULL. */
-char *getcwd(char *buf, size_t size)
+char *getcwd(char *buf, getcwd_size_t size)
 {
     if (!buf)
         return tc.currdir;
     else if (size)
     {
-        if (strlcpy(buf, tc.currdir, size) < size)
+        if ((getcwd_size_t)strlcpy(buf, tc.currdir, size) < size)
             return buf;
     }
     /* size == 0, or truncation in strlcpy */
diff --git a/apps/tree.h b/apps/tree.h
index e33fee0..993d1b4 100644
--- a/apps/tree.h
+++ b/apps/tree.h
@@ -79,7 +79,13 @@
 int rockbox_browse(const char *root, int dirfilter);
 bool create_playlist(void);
 void resume_directory(const char *dir);
-char *getcwd(char *buf, size_t size);
+#ifdef WIN32
+/* it takes an int on windows */
+#define getcwd_size_t int
+#else
+#define getcwd_size_t size_t
+#endif
+char *getcwd(char *buf, getcwd_size_t size);
 void reload_directory(void);
 bool check_rockboxdir(void);
 struct tree_context* tree_get_context(void);
diff --git a/firmware/export/load_code.h b/firmware/export/load_code.h
index f4fa8f9..e37af78 100644
--- a/firmware/export/load_code.h
+++ b/firmware/export/load_code.h
@@ -42,7 +42,13 @@
 
 /* don't call these directly for loading code
  * they're to be wrapped by platform specific functions */
-extern void *_lc_open(const char *filename, char *buf, size_t buf_size);
+#ifdef WIN32
+/* windows' LoadLibrary can only handle ucs2, no utf-8 */
+#define _lc_open_char wchar_t
+#else
+#define _lc_open_char char
+#endif
+extern void *_lc_open(const _lc_open_char *filename, char *buf, size_t buf_size);
 extern void *_lc_get_header(void *handle);
 extern void  _lc_close(void *handle);
 
diff --git a/firmware/include/file.h b/firmware/include/file.h
index a9d1d05..7799f3d 100644
--- a/firmware/include/file.h
+++ b/firmware/include/file.h
@@ -25,31 +25,19 @@
 #include <sys/types.h>
 #include "config.h"
 #include "gcc_extensions.h"
+#include <fcntl.h>
+#ifdef WIN32
+/* this has SEEK_SET et al */
+#include <stdio.h>
+#endif
+
 
 #undef MAX_PATH /* this avoids problems when building simulator */
 #define MAX_PATH 260
 #define MAX_OPEN_FILES 11
 
-#ifndef SEEK_SET
-#define SEEK_SET 0
-#endif
-#ifndef SEEK_CUR
-#define SEEK_CUR 1
-#endif
-#ifndef SEEK_END
-#define SEEK_END 2
-#endif
-
-#ifndef O_RDONLY
-#define O_RDONLY 0
-#define O_WRONLY 1
-#define O_RDWR   2
-#define O_CREAT  4
-#define O_APPEND 8
-#define O_TRUNC  0x10
-#endif
-
-#if (CONFIG_PLATFORM & PLATFORM_HOSTED) && !defined(PLUGIN) && !defined(CODEC)
+#if !defined(PLUGIN) && !defined(CODEC)
+#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
 #define open(x, ...) sim_open(x, __VA_ARGS__)
 #define creat(x,m) sim_creat(x,m)
 #define remove(x) sim_remove(x)
@@ -96,4 +84,5 @@
 extern off_t filesize(int fd);
 extern int release_files(int volume);
 int fdprintf (int fd, const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
+#endif /* !CODEC && !PLUGIN */
 #endif
diff --git a/firmware/libc/include/fcntl.h b/firmware/libc/include/fcntl.h
new file mode 100644
index 0000000..34740c9
--- /dev/null
+++ b/firmware/libc/include/fcntl.h
@@ -0,0 +1,40 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id$
+ *
+ * Copyright (C) 2002 by Björn Stenberg
+ *
+ * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#ifndef __FCNTL_H__
+#define __FCNTL_H__
+
+#ifndef O_RDONLY
+#define O_RDONLY 0
+#define O_WRONLY 1
+#define O_RDWR   2
+#define O_CREAT  4
+#define O_APPEND 8
+#define O_TRUNC  0x10
+#endif
+
+#ifndef SEEK_SET
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+#endif
+
+#endif /* __FCNTL_H__ */
diff --git a/firmware/load_code.c b/firmware/load_code.c
index 9e8e71f..75bac8b 100644
--- a/firmware/load_code.c
+++ b/firmware/load_code.c
@@ -80,13 +80,12 @@
 #else
 /* unix */
 #include <dlfcn.h>
-#define O_BINARY 0
 #endif
 #include <stdio.h>
 #include "rbpaths.h"
 #include "general.h"
 
-void * _lc_open(const char *filename, char *buf, size_t buf_size)
+void * _lc_open(const _lc_open_char *filename, char *buf, size_t buf_size)
 {
     (void)buf;
     (void)buf_size;
@@ -116,14 +115,13 @@
         char name[MAX_PATH];
         const char *_name = get_user_file_path(ROCKBOX_DIR, NEED_WRITE, name, sizeof(name));
         snprintf(temp_filename, sizeof(temp_filename),
-                 "%slibtemp_binary_%d.dll", _name, i);
+                 "%s/libtemp_binary_%d.dll", _name, i);
 #endif
-        fd = open(temp_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0766);
+        fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700);
         if (fd >= 0)
             break;  /* Created a file ok */
     }
 
-    DEBUGF("Creating %s\n", temp_filename);
     if (fd < 0)
     {
         DEBUGF("open failed\n");
diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c
index 9862b4a..6547421 100644
--- a/uisimulator/common/io.c
+++ b/uisimulator/common/io.c
@@ -179,9 +179,10 @@
 
 typedef struct mydir MYDIR;
 
-#if 1 /* maybe this needs disabling for MSVC... */
 static unsigned int rockbox2sim(int opt)
 {
+#if 0
+/* this shouldn't be needed since we use the host's versions */
     int newopt = O_BINARY;
 
     if(opt & 1)
@@ -196,8 +197,10 @@
         newopt |= O_TRUNC;
 
     return newopt;
-}
+#else
+    return opt|O_BINARY;
 #endif
+}
 
 /** Simulator I/O engine routines **/
 #define IO_YIELD_THRESHOLD 512
@@ -537,7 +540,7 @@
 void *lc_open(const char *filename, char *buf, size_t buf_size)
 {
     const char *sim_path = get_sim_pathname(filename);
-    void *handle = _lc_open((const char*)UTF8_TO_OS(sim_path), buf, buf_size);
+    void *handle = _lc_open(UTF8_TO_OS(sim_path), buf, buf_size);
 
     if (handle == NULL)
     {