]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/win32/compat/compat.cpp
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / win32 / compat / compat.cpp
index e61a3c4072ee95b68d7d5c3b2bf3726c77847891..80a77f578d8152f78a349974c01b3064d599c5dd 100644 (file)
@@ -36,7 +36,7 @@
 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
 extern DWORD   g_platform_id;
 
-// from CYGWIN (should be diff between Jan 1 1601 and Jan 1 1970
+// from MicroSoft SDK (KES) is the diff between Jan 1 1601 and Jan 1 1970
 #ifdef HAVE_MINGW
 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000UL //Not sure it works
 #else
@@ -446,13 +446,14 @@ strncasecmp(const char *s1, const char *s2, int len)
         return -1;
     else if (!s2)
         return 1;
-    do
+    while (len--)
     {
         ch1 = *s1;
         ch2 = *s2;
         s1++;
         s2++;
-    } while (len-- && ch1 != 0 && tolower(ch1) == tolower(ch2));
+        if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break;
+    } 
 
     return(ch1 - ch2);
 }
@@ -479,8 +480,11 @@ gettimeofday(struct timeval *tv, struct timezone *)
 }
 
 int
-syslog(int, const char *, const char *)
+syslog(int type, const char *fmt, const char *msg)
 {
+/*#ifndef HAVE_CONSOLE
+    MessageBox(NULL, msg, "Bacula", MB_OK);
+#endif*/
     return 0;
 }
 
@@ -757,7 +761,7 @@ winver::winver(void)
     snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
              platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
 
-#if 1
+#if 0
     HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
     CloseHandle(h);
 #endif
@@ -777,19 +781,33 @@ VOID ReadFromPipe(VOID);
 VOID ErrorExit(LPTSTR);
 VOID ErrMsg(LPTSTR, BOOL);
 
-
+/**
+ * Check for a quoted path,  if an absolute path name is given and it contains
+ * spaces it will need to be quoted.  i.e.  "c:/Program Files/foo/bar.exe"
+ * CreateProcess() says the best way to ensure proper results with executables
+ * with spaces in path or filename is to quote the string.
+ */
 const char *
 getArgv0(const char *cmdline)
 {
-    const char *cp = cmdline;
 
-    while (*cp && !isspace(*cp)) cp++;
+    int inquote = 0;
+    for (const char *cp = cmdline; *cp; cp++)
+    {
+        if (*cp == '"') {
+            inquote = !inquote;
+        }
+        if (!inquote && isspace(*cp))
+            break;
+    }
 
+        
     int len = cp - cmdline;
     char *rval = (char *)malloc(len+1);
 
     cp = cmdline;
     char *rp = rval;
+    
     while (len--)
         *rp++ = *cp++;
 
@@ -797,6 +815,13 @@ getArgv0(const char *cmdline)
     return rval;
 }
 
+
+/**
+ * OK, so it would seem CreateProcess only handles true executables:
+ *  .com or .exe files.
+ * So test to see whether we're getting a .bat file and if so grab
+ * $COMSPEC value and pass batch file to it.
+ */
 HANDLE
 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
 {
@@ -832,37 +857,20 @@ CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
         siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
     // Create the child process.
 
-    char cmdLine[1024];
-    char exeFile[1024];
-    // retrive the first compont of the command line which should be the
-    // executable
-    const char *exeName = getArgv0(cmdline);
-    // check to see if absolute path was passed to us already?
-    if (exeName[1] != ':'
-        || (strchr(cmdline, '/') == NULL
-            && strchr(cmdline, '\\') == NULL))
-    {
-        // only command name so perform search of PATH to find
-        char *file;
-        DWORD rval = SearchPath(NULL,
-                                exeName,
-                                ".exe",
-                                sizeof(exeFile),
-                                exeFile,
-                                &file);
-        if (rval == 0)
-            return INVALID_HANDLE_VALUE;
-        if (rval > sizeof(exeFile))
-            return INVALID_HANDLE_VALUE;
+    char exeFile[256];
 
-    }
-    else
-        strcpy(exeFile, exeName);
+    const char *comspec = getenv("COMSPEC");
+    
+    if (comspec == NULL) // should never happen
+        return INVALID_HANDLE_VALUE;
+
+    char *cmdLine = (char *)alloca(strlen(cmdline) + strlen(comspec) + 16);
+    
+    strcpy(exeFile, comspec);
+    strcpy(cmdLine, comspec);
+    strcat(cmdLine, " /c ");
+    strcat(cmdLine, cmdline);
 
-    // exeFile now has absolute path to program to execute.
-    free((void *)exeName);
-    // copy original command line to pass to create process
-    strcpy(cmdLine, cmdline);
     // try to execute program
     bFuncRetn = CreateProcess(exeFile,
                               cmdLine, // command line
@@ -877,6 +885,10 @@ CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
 
     if (bFuncRetn == 0) {
         ErrorExit("CreateProcess failed\n");
+        const char *err = errorString();
+        d_msg(__FILE__, __LINE__, 99,
+              "CreateProcess(%s, %s, ...)=%s\n", exeFile, cmdLine, err);
+        LocalFree((void *)err);
         return INVALID_HANDLE_VALUE;
     }
     // we don't need a handle on the process primary thread so we close
@@ -961,13 +973,14 @@ open_bpipe(char *prog, int wait, const char *mode)
         }
 
         CloseHandle(hChildStdoutRd);
+        hChildStdoutRd = INVALID_HANDLE_VALUE;
     }
 
     if (mode_write) {
 
         // Create a pipe for the child process's STDIN.
 
-        if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
+        if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
             ErrorExit("Stdin pipe creation failed\n");
             goto cleanup;
         }
@@ -984,13 +997,14 @@ open_bpipe(char *prog, int wait, const char *mode)
         }
 
         CloseHandle(hChildStdinWr);
+        hChildStdinWr = INVALID_HANDLE_VALUE;
     }
     // spawn program with redirected handles as appropriate
     bpipe->worker_pid = (pid_t)
-        CreateChildProcess(prog, // commandline
-                           hChildStdinRd, // stdin HANDLE
-                           hChildStdoutWr, // stdout HANDLE
-                           hChildStdoutWr);// stderr HANDLE
+        CreateChildProcess(prog,             // commandline
+                           hChildStdinRd,    // stdin HANDLE
+                           hChildStdoutWr,   // stdout HANDLE
+                           hChildStdoutWr);  // stderr HANDLE
 
     if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
         goto cleanup;
@@ -1004,14 +1018,18 @@ open_bpipe(char *prog, int wait, const char *mode)
                                      // detect eof.
         // ugly but convert WIN32 HANDLE to FILE*
         int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
-        bpipe->rfd = _fdopen(rfd, "r");
+        if (rfd >= 0) {
+           bpipe->rfd = _fdopen(rfd, "r");
+        }
     }
     if (mode_write) {
         CloseHandle(hChildStdinRd); // close our read side so as not
                                     // to interfre with child's copy
         // ugly but convert WIN32 HANDLE to FILE*
         int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
-        bpipe->wfd = _fdopen(wfd, "w");
+        if (wfd >= 0) {
+           bpipe->wfd = _fdopen(wfd, "w");
+        }
     }
 
     if (wait > 0) {
@@ -1055,11 +1073,9 @@ close_bpipe(BPIPE *bpipe)
 
     if (bpipe->wait) {
         int remaining_wait = bpipe->wait;
-        do
-        {
+        do {
             DWORD exitCode;
-            if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode))
-            {
+            if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
                 const char *err = errorString();
                 rval = GetLastError();
                 d_msg(__FILE__, __LINE__, 0,
@@ -1146,7 +1162,10 @@ open(const char *file, int flags, int mode)
 int
 close(int fd)
 {
-    return _close(fd);
+    int rval = _close(fd);
+    if (rval == -1)
+        rval = closesocket(fd);
+    return rval;
 }
 
 #ifndef HAVE_WXCONSOLE
@@ -1166,7 +1185,7 @@ write(int fd, const void *buf, size_t len)
 off_t
 lseek(int fd, off_t offset, int whence)
 {
-    return _lseeki64(fd, offset, whence);
+    return (off_t)_lseeki64(fd, offset, whence);
 }
 
 int