bl/spawn.c
changeset 10 f28ad4577863
parent 7 721e468c10f3
child 11 4a80c6053a66
     1.1 --- a/bl/spawn.c	Fri Jan 27 21:40:35 2012 +0000
     1.2 +++ b/bl/spawn.c	Mon Jan 30 09:11:07 2012 +0000
     1.3 @@ -7,8 +7,8 @@
     1.4  
     1.5  #define SPAWN_BUFSIZE	128
     1.6  
     1.7 -gboolean spawn_sync(char **argv,char **standard_output,int *exit_status,
     1.8 -  GError **error)
     1.9 +gboolean spawn_sync(const char *working_directory,char **argv,
    1.10 +  char **standard_output,int *exit_status,GError **error)
    1.11  {
    1.12  /* Don't use g_spawn_sync on WIN32 for now to avoid needing the helper */
    1.13  #ifndef WIN32
    1.14 @@ -17,8 +17,8 @@
    1.15      GSpawnFlags flags=G_SPAWN_SEARCH_PATH;
    1.16      if (!standard_output)
    1.17  	flags=G_SPAWN_STDOUT_TO_DEV_NULL;
    1.18 -    retval=g_spawn_sync(NULL,argv,NULL,flags,NULL,NULL,standard_output,
    1.19 -      &standard_error,exit_status,error);
    1.20 +    retval=g_spawn_sync(working_directory,argv,NULL,flags,NULL,NULL,
    1.21 +      standard_output,&standard_error,exit_status,error);
    1.22      if (standard_error)
    1.23  	fputs(standard_error,stderr);
    1.24      g_free(standard_error);
    1.25 @@ -29,7 +29,21 @@
    1.26      FILE *fp;
    1.27      int i,r;
    1.28      size_t n,len;
    1.29 +    char *current_dir;
    1.30      GString *command_line,*string;
    1.31 +    if (working_directory)
    1.32 +    {
    1.33 +	current_dir=g_get_current_dir();
    1.34 +	if (g_chdir(working_directory)<0)
    1.35 +	{
    1.36 +	    g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
    1.37 +	      "%s: %s",working_directory,g_strerror(errno));
    1.38 +	    g_free(current_dir);
    1.39 +	    return FALSE;
    1.40 +	}
    1.41 +    }
    1.42 +    else
    1.43 +	current_dir=NULL;
    1.44      command_line=g_string_new(NULL);
    1.45      for(i=0;argv[i];i++)
    1.46      {
    1.47 @@ -39,6 +53,13 @@
    1.48      }
    1.49      fp=popen(command_line->str,"r");
    1.50      g_string_free(command_line,TRUE);
    1.51 +    if (current_dir)
    1.52 +    {
    1.53 +	if (g_chdir(current_dir)<0)
    1.54 +	    g_error("Failed to restore current directory: %s",
    1.55 +	      g_strerror(errno));
    1.56 +	g_free(current_dir);
    1.57 +    }
    1.58      if (!fp)
    1.59      {
    1.60  	g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),