Sarathy's patch
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
index a73d9e6..dcdda53 100644 (file)
@@ -11,6 +11,9 @@
 #define WIN32_LEAN_AND_MEAN
 #define WIN32IO_IS_STDIO
 #include <tchar.h>
+#ifdef __GNUC__
+#define Win32_Winsock
+#endif
 #include <windows.h>
 
 /* #include "config.h" */
 #include "XSUB.h"
 #include <fcntl.h>
 #include <sys/stat.h>
+#ifndef __GNUC__
+/* assert.h conflicts with #define of assert in perl.h */
 #include <assert.h>
+#endif
 #include <string.h>
 #include <stdarg.h>
 #include <float.h>
 
+#ifdef __GNUC__
+/* Mingw32 defaults to globing command line 
+ * So we turn it off like this:
+ */
+int _CRT_glob = 0;
+#endif
+
 #define EXECF_EXEC 1
 #define EXECF_SPAWN 2
 #define EXECF_SPAWN_NOWAIT 3
@@ -55,30 +68,30 @@ IsWinNT(void) {
 }
 
 char *
-win32PerlLibPath(void)
+win32PerlLibPath(char *sfx,...)
 {
+    va_list ap;
     char *end;
+    va_start(ap,sfx);
     GetModuleFileName((PerlDllHandle == INVALID_HANDLE_VALUE) 
                      ? GetModuleHandle(NULL)
                      : PerlDllHandle,
                      szPerlLibRoot, 
                      sizeof(szPerlLibRoot));
-
     *(end = strrchr(szPerlLibRoot, '\\')) = '\0';
     if (stricmp(end-4,"\\bin") == 0)
      end -= 4;
     strcpy(end,"\\lib");
+    while (sfx)
+     {
+      strcat(end,"\\");
+      strcat(end,sfx);
+      sfx = va_arg(ap,char *);
+     }
+    va_end(ap); 
     return (szPerlLibRoot);
 }
 
-char *
-win32SiteLibPath(void)
-{
-    static char szPerlSiteLib[MAX_PATH+1];
-    strcpy(szPerlSiteLib, win32PerlLibPath());
-    strcat(szPerlSiteLib, "\\site");
-    return (szPerlSiteLib);
-}
 
 BOOL
 HasRedirection(char *ptr)
@@ -532,15 +545,15 @@ getegid(void)
 }
 
 int
-setuid(uid_t uid)
+setuid(uid_t auid)
 { 
-    return (uid == ROOT_UID ? 0 : -1);
+    return (auid == ROOT_UID ? 0 : -1);
 }
 
 int
-setgid(gid_t gid)
+setgid(gid_t agid)
 {
-    return (gid == ROOT_GID ? 0 : -1);
+    return (agid == ROOT_GID ? 0 : -1);
 }
 
 /*
@@ -575,26 +588,13 @@ ioctl(int i, unsigned int u, char *data)
 }
 #endif
 
-unsigned int
-sleep(unsigned int t)
+DllExport unsigned int
+win32_sleep(unsigned int t)
 {
     Sleep(t*1000);
     return 0;
 }
 
-
-#undef rename
-
-int
-myrename(char *OldFileName, char *newname)
-{
-    if(_access(newname, 0) != -1) {    /* file exists */
-       _unlink(newname);
-    }
-    return rename(OldFileName, newname);
-}
-
-
 DllExport int
 win32_stat(const char *path, struct stat *buffer)
 {
@@ -670,9 +670,8 @@ FileTimeToClock(PFILETIME ft)
  return (long) qw;
 }
 
-#undef times
-int
-my_times(struct tms *timebuf)
+DllExport int
+win32_times(struct tms *timebuf)
 {
     FILETIME user;
     FILETIME kernel;
@@ -705,9 +704,8 @@ static VOID CALLBACK TimerProc(HWND win, UINT msg, UINT id, DWORD time)
  sighandler(14);
 }
 
-#undef alarm
-unsigned int
-my_alarm(unsigned int sec)
+DllExport unsigned int
+win32_alarm(unsigned int sec)
 {
     /* 
      * the 'obvious' implentation is SetTimer() with a callback
@@ -735,14 +733,7 @@ my_alarm(unsigned int sec)
     return 0;
 }
 
-#if defined(_DLL) || !defined(_MSC_VER)
-/* It may or may not be fixed (ok on NT), but DLL runtime
-   does not export the functions used in the workround
-*/
-#define WIN95_OSFHANDLE_FIXED
-#endif
-
-#if defined(_WIN32) && !defined(WIN95_OSFHANDLE_FIXED) && defined(_M_IX86)
+#ifdef USE_FIXED_OSFHANDLE
 
 EXTERN_C int __cdecl _alloc_osfhnd(void);
 EXTERN_C int __cdecl _set_osfhnd(int fh, long value);
@@ -841,20 +832,24 @@ my_open_osfhandle(long osfhandle, int flags)
 }
 
 #define _open_osfhandle my_open_osfhandle
-#endif /* _M_IX86 */
+#endif /* USE_FIXED_OSFHANDLE */
 
 /* simulate flock by locking a range on the file */
 
 #define LK_ERR(f,i)    ((f) ? (i = 0) : (errno = GetLastError()))
 #define LK_LEN         0xffff0000
 
-int
-my_flock(int fd, int oper)
+DllExport int
+win32_flock(int fd, int oper)
 {
     OVERLAPPED o;
     int i = -1;
     HANDLE fh;
 
+    if (!IsWinNT()) {
+       croak("flock() unimplemented on this platform");
+       return -1;
+    }
     fh = (HANDLE)_get_osfhandle(fd);
     memset(&o, 0, sizeof(o));
 
@@ -941,7 +936,15 @@ win32_feof(FILE *fp)
  * we have to roll our own.
  */
 
+#ifdef USE_THREADS
+#ifdef USE_DECLSPEC_THREAD
 __declspec(thread) char        strerror_buffer[512];
+#else
+#define strerror_buffer (thr->i.Wstrerror_buffer)
+#endif
+#else
+char   strerror_buffer[512];
+#endif
 
 DllExport char *
 win32_strerror(int e) 
@@ -952,6 +955,7 @@ win32_strerror(int e)
     DWORD source = 0;
 
     if(e < 0 || e > sys_nerr) {
+        dTHR;
        if(e < 0)
            e = GetLastError();
 
@@ -1014,6 +1018,11 @@ win32_fopen(const char *filename, const char *mode)
     return fopen(filename, mode);
 }
 
+#ifndef USE_SOCKETS_AS_HANDLES
+#undef fdopen
+#define fdopen my_fdopen
+#endif
+
 DllExport FILE *
 win32_fdopen( int handle, const char *mode)
 {
@@ -1031,7 +1040,7 @@ win32_freopen( const char *path, const char *mode, FILE *stream)
 DllExport int
 win32_fclose(FILE *pf)
 {
-    return my_fclose(pf);
+    return my_fclose(pf);      /* defined in win32sck.c */
 }
 
 DllExport int
@@ -1122,9 +1131,9 @@ win32_abort(void)
 }
 
 DllExport int
-win32_fstat(int fd,struct stat *bufptr)
+win32_fstat(int fd,struct stat *sbufptr)
 {
-    return fstat(fd,bufptr);
+    return fstat(fd,sbufptr);
 }
 
 DllExport int
@@ -1316,6 +1325,85 @@ win32_putchar(int c)
     return putchar(c);
 }
 
+#ifdef MYMALLOC
+
+#ifndef USE_PERL_SBRK
+
+static char *committed = NULL;
+static char *base      = NULL;
+static char *reserved  = NULL;
+static char *brk       = NULL;
+static DWORD pagesize  = 0;
+static DWORD allocsize = 0;
+
+void *
+sbrk(int need)
+{
+ void *result;
+ if (!pagesize)
+  {SYSTEM_INFO info;
+   GetSystemInfo(&info);
+   /* Pretend page size is larger so we don't perpetually
+    * call the OS to commit just one page ...
+    */
+   pagesize = info.dwPageSize << 3;
+   allocsize = info.dwAllocationGranularity;
+  }
+ /* This scheme fails eventually if request for contiguous
+  * block is denied so reserve big blocks - this is only 
+  * address space not memory ...
+  */
+ if (brk+need >= reserved)
+  {
+   DWORD size = 64*1024*1024;
+   char *addr;
+   if (committed && reserved && committed < reserved)
+    {
+     /* Commit last of previous chunk cannot span allocations */
+     addr = (char *) VirtualAlloc(committed,reserved-committed,MEM_COMMIT,PAGE_READWRITE);
+     if (addr)
+      committed = reserved;
+    }
+   /* Reserve some (more) space 
+    * Note this is a little sneaky, 1st call passes NULL as reserved
+    * so lets system choose where we start, subsequent calls pass
+    * the old end address so ask for a contiguous block
+    */
+   addr  = (char *) VirtualAlloc(reserved,size,MEM_RESERVE,PAGE_NOACCESS);
+   if (addr)
+    {
+     reserved = addr+size;
+     if (!base)
+      base = addr;
+     if (!committed)
+      committed = base;
+     if (!brk)
+      brk = committed;
+    }
+   else
+    {
+     return (void *) -1;
+    }
+  }
+ result = brk;
+ brk += need;
+ if (brk > committed)
+  {
+   DWORD size = ((brk-committed + pagesize -1)/pagesize) * pagesize;
+   char *addr = (char *) VirtualAlloc(committed,size,MEM_COMMIT,PAGE_READWRITE);
+   if (addr)
+    {
+     committed += size;
+    }
+   else
+    return (void *) -1;
+  }
+ return result;
+}
+
+#endif
+#endif
+
 DllExport void*
 win32_malloc(size_t size)
 {
@@ -1340,6 +1428,7 @@ win32_free(void *block)
     free(block);
 }
 
+
 int
 win32_open_osfhandle(long handle, int flags)
 {
@@ -1356,16 +1445,6 @@ win32_get_osfhandle(int fd)
  * Extras.
  */
 
-DllExport int
-win32_flock(int fd, int oper)
-{
-    if (!IsWinNT()) {
-       croak("flock() unimplemented on this platform");
-       return -1;
-    }
-    return my_flock(fd, oper);
-}
-
 static
 XS(w32_GetCwd)
 {
@@ -1617,7 +1696,7 @@ XS(w32_GetShortPathName)
 }
 
 void
-init_os_extras()
+Perl_init_os_extras()
 {
     char *file = __FILE__;
     dXSUB_SYS;
@@ -1663,11 +1742,41 @@ Perl_win32_init(int *argcp, char ***argvp)
      * want to be at the vendor's whim on the default, we set
      * it explicitly here.
      */
-#if !defined(_ALPHA_)
+#if !defined(_ALPHA_) && !defined(__GNUC__)
     _control87(MCW_EM, MCW_EM);
 #endif
+    MALLOC_INIT; 
+}
+
+#ifdef USE_BINMODE_SCRIPTS
+
+void
+win32_strip_return(SV *sv)
+{
+ char *s = SvPVX(sv);
+ char *e = s+SvCUR(sv);
+ char *d = s;
+ while (s < e)
+  {
+   if (*s == '\r' && s[1] == '\n')
+    {
+     *d++ = '\n';
+     s += 2;
+    }
+   else 
+    {
+     *d++ = *s++;
+    }   
+  }
+ SvCUR_set(sv,d-SvPVX(sv)); 
 }
 
+#endif
+
+
+
+
+