#include #include #ifndef WIN32 #include #endif #include #define SPAWN_BUFSIZE 128 boolean spawn_sync(char **argv,char **standard_output,int *exit_status) { /* Don't use g_spawn_sync on WIN32 for now to avoid needing the helper */ #if HAVE_GLIB && !defined(WIN32) char *standard_error; GError *error=NULL; gboolean retval; GSpawnFlags flags=G_SPAWN_SEARCH_PATH; if (!standard_output) flags=G_SPAWN_STDOUT_TO_DEV_NULL; retval=g_spawn_sync(NULL,argv,NULL,flags,NULL,NULL,standard_output, &standard_error,exit_status,&error); fputs(standard_error,stderr); g_free(standard_error); if (!retval) { fprintf(stderr,"%s\n",error->message); g_error_free(error); } else if (exit_status) *exit_status=WEXITSTATUS(*exit_status); return retval; #else FILE *fp; int i,r; size_t n,len; String *command_line,*string; command_line=string_new(NULL); for(i=0;argv[i];i++) { if (i) string_append_c(command_line,' '); string_append(command_line,argv[i]); } fp=popen(command_line->str,"r"); string_free(command_line,TRUE); if (!fp) { perror(command_line->str); return FALSE; } string=string_new(NULL); do { len=string->len; string_set_size(string,len+SPAWN_BUFSIZE); n=fread(string->str+len,1,SPAWN_BUFSIZE,fp); if (n<0) { perror("fread"); (void)pclose(fp); string_free(string,TRUE); return FALSE; } string_set_size(string,len+n); } while(n); r=pclose(fp); if (r<0) { perror("pclose"); string_free(string,TRUE); return FALSE; } else { if (exit_status) *exit_status=r; if (standard_output) *standard_output=string_free(string,FALSE); else string_free(string,TRUE); return TRUE; } #endif }