wrong error number
[mono.git] / mono / metadata / process.c
index 5fa57bcf0ed71501f3c3101b9a4859d60d867eb6..ade650b4c5462ab1ffd666bde06db5ed5195a479 100644 (file)
@@ -206,7 +206,8 @@ static gpointer process_get_versioninfo_block (gpointer data,
        data=((gunichar2 *)data)+(unicode_chars (block->key)+1);
 
        /* align on a 32-bit boundary */
-       data=(gpointer)(((unsigned)data+3) & (~3));
+       data=(gpointer)((char *)data + 3);
+       data=(gpointer)((char *)data - (GPOINTER_TO_INT(data) & 3));
        
        return(data);
 }
@@ -308,7 +309,9 @@ static gpointer process_read_string_block (MonoObject *filever,
                gunichar2 *value;
                
                /* align on a 32-bit boundary */
-               data_ptr=(gpointer)(((unsigned)data_ptr+3) & (~3));
+               data_ptr=(gpointer)((char *)data_ptr + 3);
+               data_ptr=(gpointer)((char *)data_ptr -
+                   (GPOINTER_TO_INT(data_ptr) & 3));
 
                data_ptr=process_get_versioninfo_block (data_ptr, &block);
                if(block.data_len==0) {
@@ -396,7 +399,9 @@ static gpointer process_read_stringtable_block (MonoObject *filever,
 
        while(string_len<data_len) {
                /* align on a 32-bit boundary */
-               data_ptr=(gpointer)(((unsigned)data_ptr+3) & (~3));
+               data_ptr=(gpointer)((char *)data_ptr + 3);
+               data_ptr=(gpointer)((char *)data_ptr -
+                   (GPOINTER_TO_INT(data_ptr) & 3));
 
                data_ptr=process_get_versioninfo_block (data_ptr, &block);
                if(block.data_len==0) {
@@ -566,7 +571,9 @@ static void process_get_fileversion (MonoObject *filever, MonoImage *image)
         */
        while(data_len > 0) {
                /* align on a 32-bit boundary */
-               data_ptr=(gpointer)(((unsigned)data_ptr+3) & (~3));
+               data_ptr=(gpointer)((char *)data_ptr + 3);
+               data_ptr=(gpointer)((char *)data_ptr -
+                   (GPOINTER_TO_INT(data_ptr) & 3));
 
                data_ptr=process_get_versioninfo_block (data_ptr, &block);
                if(block.data_len==0) {
@@ -726,21 +733,42 @@ void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoO
        mono_image_close (image);
 }
 
+/* Only used when UseShellExecute is false */
+static gchar *
+quote_path (const gchar *path)
+{
+       gchar *res = g_shell_quote (path);
+#ifdef PLATFORM_WIN32
+       {
+       gchar *q = res;
+       while (*q) {
+               if (*q == '\'')
+                       *q = '\"';
+               q++;
+       }
+       }
+#endif
+       return res;
+}
+
+/* Only used when UseShellExecute is false */
 static gboolean
-complete_path (const gunichar2 *appname, gunichar2 **completed)
+complete_path (const gunichar2 *appname, gchar **completed)
 {
        gchar *utf8app;
        gchar *found;
 
        utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
        if (g_path_is_absolute (utf8app)) {
+               *completed = quote_path (utf8app);
                g_free (utf8app);
-               return FALSE;
+               return TRUE;
        }
 
        if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
+               *completed = quote_path (utf8app);
                g_free (utf8app);
-               return FALSE;
+               return TRUE;
        }
        
        found = g_find_program_in_path (utf8app);
@@ -750,7 +778,7 @@ complete_path (const gunichar2 *appname, gunichar2 **completed)
                return FALSE;
        }
 
-       *completed = g_utf8_to_utf16 (found, -1, NULL, NULL, NULL);
+       *completed = quote_path (found);
        g_free (found);
        g_free (utf8app);
        return TRUE;
@@ -765,6 +793,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_Start_internal (MonoString *app
        gunichar2 *shell_path = NULL;
        gchar *env_vars = NULL;
        gboolean free_shell_path = TRUE;
+       gchar *newcmd, *tmp;
        
        MONO_ARCH_SAVE_REGS;
 
@@ -786,7 +815,6 @@ MonoBoolean ves_icall_System_Diagnostics_Process_Start_internal (MonoString *app
 #endif
                if (spath != NULL) {
                        gint dummy;
-                       gchar *newcmd, *tmp;
                        gchar *quoted;
 
                        shell_path = mono_unicode_from_external (spath, &dummy);
@@ -809,12 +837,27 @@ MonoBoolean ves_icall_System_Diagnostics_Process_Start_internal (MonoString *app
                        g_free (newcmd);
                }
        } else {
+               gchar *spath = NULL;
                shell_path = mono_string_chars (appname);
-               free_shell_path = complete_path (shell_path, &shell_path);
-               if (shell_path == NULL) {
+               complete_path (shell_path, &spath);
+               if (spath == NULL) {
                        process_info->pid = -ERROR_FILE_NOT_FOUND;
                        return FALSE;
                }
+#ifdef PLATFORM_WIN32
+               /* Seems like our CreateProcess does not work as the windows one.
+                * This hack is needed to deal with paths containing spaces */
+               shell_path = NULL;
+               free_shell_path = FALSE;
+               tmp = mono_string_to_utf8 (cmd);
+               newcmd = g_strdup_printf ("%s %s", spath, tmp);
+               cmd = mono_string_new_wrapper (newcmd);
+               g_free (newcmd);
+               g_free (tmp);
+#else
+               shell_path = g_utf8_to_utf16 (spath, -1, NULL, NULL, NULL);
+#endif
+               g_free (spath);
        }
 
        if (process_info->env_keys != NULL) {