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