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@7: gboolean spawn_sync(char **argv,char **standard_output,int *exit_status, ali@7: GError **error) ali@0: { ali@0: /* Don't use g_spawn_sync on WIN32 for now to avoid needing the helper */ ali@6: #ifndef WIN32 ali@7: char *standard_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@7: &standard_error,exit_status,error); ali@7: if (standard_error) ali@7: fputs(standard_error,stderr); ali@0: g_free(standard_error); ali@7: if (retval && 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@6: GString *command_line,*string; ali@6: command_line=g_string_new(NULL); ali@0: for(i=0;argv[i];i++) ali@0: { ali@0: if (i) ali@6: g_string_append_c(command_line,' '); ali@6: g_string_append(command_line,argv[i]); ali@0: } ali@0: fp=popen(command_line->str,"r"); ali@6: g_string_free(command_line,TRUE); ali@0: if (!fp) ali@0: { ali@7: g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), ali@7: "%s: %s",command_line->str,g_strerror(errno)); ali@0: return FALSE; ali@0: } ali@6: string=g_string_new(NULL); ali@0: do ali@0: { ali@0: len=string->len; ali@6: g_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@7: g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), ali@7: "Error reading from bookloupe: %s",g_strerror(errno)); ali@0: (void)pclose(fp); ali@6: g_string_free(string,TRUE); ali@0: return FALSE; ali@0: } ali@6: g_string_set_size(string,len+n); ali@0: } while(n); ali@0: r=pclose(fp); ali@0: if (r<0) ali@0: { ali@7: g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), ali@7: "Error reading from bookloupe: %s",g_strerror(errno)); ali@6: g_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@6: *standard_output=g_string_free(string,FALSE); ali@0: else ali@6: g_string_free(string,TRUE); ali@0: return TRUE; ali@0: } ali@0: #endif ali@0: }