From 01bee8e1e13a136b42c288fc356a6fad6d001638 Mon Sep 17 00:00:00 2001
From: IOhannes m zmoelnig <zmoelnig@iem.at>
Date: Mon, 17 Dec 2012 11:53:52 +0100
Subject: [PATCH] provide sys_close(),... on all platforms

removing sys_close (as was done with commit:78b81aa3) will break binary
compatibility with externals compiled against Pd-0.43.
this patch re-introduces sys_close() and also provides stubs for the rest
of the open/close family.

while we are there, we also run the filename through sys_bashfilename
(which currently is a noop on non-w32 platforms)
---
 src/m_pd.h   |   12 +-----------
 src/s_path.c |   47 ++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/src/m_pd.h b/src/m_pd.h
index cb81858..4bef720 100644
--- a/src/m_pd.h
+++ b/src/m_pd.h
@@ -74,9 +74,7 @@ typedef long int32_t;  /* a data type that has 32 bits */
 #endif
 
 /* for FILE, needed by sys_fopen() and sys_fclose() only */
-#ifdef _WIN32
-# include <stdio.h>
-#endif
+#include <stdio.h>
 
 #define MAXPDSTRING 1000        /* use this for anything you want */
 #define MAXPDARG 5              /* max number of args we can typecheck today */
@@ -537,18 +535,10 @@ EXTERN int (*sys_idlehook)(void);   /* hook to add idle time computation */
  * these internal versions that handle UTF-8 filenames the same across
  * all platforms.  They are recommended for use in external
  * objectclasses as well so they work with Unicode filenames on Windows */
-#ifdef _WIN32
 EXTERN int sys_open(const char *path, int oflag, ...);
 EXTERN int sys_close(int fd);
 EXTERN FILE *sys_fopen(const char *filename, const char *mode);
 EXTERN int sys_fclose(FILE *stream);
-#else
-# define sys_open open
-# define sys_close close
-# define sys_fopen fopen
-# define sys_fclose fclose
-#endif
-
 
 /* ------------  threading ------------------- */ 
 EXTERN void sys_lock(void);
diff --git a/src/s_path.c b/src/s_path.c
index fd3f845..d4efcb5 100644
--- a/src/s_path.c
+++ b/src/s_path.c
@@ -392,9 +392,7 @@ int open_via_path(const char *dir, const char *name, const char *ext,
 
     /* open a file with a UTF-8 filename
     This is needed because WIN32 does not support UTF-8 filenames, only UCS2.
-    On all other platforms, this is defined as "#define sys_open open" since
-    they all support UTF-8 filenames. Having this function prevents lots of
-    #ifdefs all over the place.
+    Having this function prevents lots of #ifdefs all over the place.
     */
 #ifdef _WIN32
 int sys_open(const char *path, int oflag, ...)
@@ -424,10 +422,50 @@ FILE *sys_fopen(const char *filename, const char *mode)
     mbstowcs(ucs2mode, mode, MAXPDSTRING);
     return (_wfopen(ucs2buf, ucs2mode));
 }
+#else
+#include <stdarg.h>
+int sys_open(const char *path, int oflag, ...)
+{
+    int i, fd;
+    char pathbuf[MAXPDSTRING];
+    sys_bashfilename(path, pathbuf);
+    if (oflag & O_CREAT)
+    {
+        mode_t mode;
+        va_list ap;
+        va_start(ap, oflag);
+        /* If mode_t is narrower than int, use the promoted type (int),
+           not mode_t.  Use sizeof to guess whether mode_t is narrower;
+           we don't know of any practical counterexamples.
+           -> http://www.mail-archive.com/bug-gnulib@gnu.org/msg14212.html
+           -> http://bugs.debian.org/647345
+        */
+        if(sizeof(mode_t) < sizeof(int))
+        {
+            int imode = va_arg (ap, int);
+            mode=(mode_t)imode;
+        }
+        else
+            mode = va_arg (ap, mode_t);
+        va_end(ap);
+        fd = open(pathbuf, oflag, mode);
+    }
+    else
+        fd = open(pathbuf, oflag);
+    return fd;
+}
+
+FILE *sys_fopen(const char *filename, const char *mode)
+{
+  char namebuf[MAXPDSTRING];
+  sys_bashfilename(filename, namebuf);
+  return fopen(namebuf, mode);
+}
+#endif /* _WIN32 */
 
    /* close a previously opened file
    this is needed on platforms where you cannot open/close resources
-   across dll-boundaries */
+   across dll-boundaries, but we provide it for other platforms as well */
 int sys_close(int fd)
 {
     return _close(fd);
@@ -437,7 +475,6 @@ int sys_fclose(FILE *stream)
 {
     return fclose(stream);
 }
-#endif /* _WIN32 */
 
 
     /* Open a help file using the help search path.  We expect the ".pd"
-- 
1.7.10.1

