bl/spawn.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 21:40:35 2012 +0000 (2012-01-27)
changeset 7 721e468c10f3
parent 6 faab25d520dd
child 9 6a13fe0fc19e
permissions -rw-r--r--
Add support for non-ASCII testcases
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <stdio.h>
ali@0
     3
#ifndef WIN32
ali@0
     4
#include <sys/wait.h>
ali@0
     5
#endif
ali@5
     6
#include <bl/bl.h>
ali@0
     7
ali@0
     8
#define SPAWN_BUFSIZE	128
ali@0
     9
ali@7
    10
gboolean spawn_sync(char **argv,char **standard_output,int *exit_status,
ali@7
    11
  GError **error)
ali@0
    12
{
ali@0
    13
/* Don't use g_spawn_sync on WIN32 for now to avoid needing the helper */
ali@6
    14
#ifndef WIN32
ali@7
    15
    char *standard_error=NULL;
ali@0
    16
    gboolean retval;
ali@0
    17
    GSpawnFlags flags=G_SPAWN_SEARCH_PATH;
ali@0
    18
    if (!standard_output)
ali@0
    19
	flags=G_SPAWN_STDOUT_TO_DEV_NULL;
ali@0
    20
    retval=g_spawn_sync(NULL,argv,NULL,flags,NULL,NULL,standard_output,
ali@7
    21
      &standard_error,exit_status,error);
ali@7
    22
    if (standard_error)
ali@7
    23
	fputs(standard_error,stderr);
ali@0
    24
    g_free(standard_error);
ali@7
    25
    if (retval && exit_status)
ali@0
    26
	*exit_status=WEXITSTATUS(*exit_status);
ali@0
    27
    return retval;
ali@0
    28
#else
ali@0
    29
    FILE *fp;
ali@0
    30
    int i,r;
ali@0
    31
    size_t n,len;
ali@6
    32
    GString *command_line,*string;
ali@6
    33
    command_line=g_string_new(NULL);
ali@0
    34
    for(i=0;argv[i];i++)
ali@0
    35
    {
ali@0
    36
	if (i)
ali@6
    37
	    g_string_append_c(command_line,' ');
ali@6
    38
	g_string_append(command_line,argv[i]);
ali@0
    39
    }
ali@0
    40
    fp=popen(command_line->str,"r");
ali@6
    41
    g_string_free(command_line,TRUE);
ali@0
    42
    if (!fp)
ali@0
    43
    {
ali@7
    44
	g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
ali@7
    45
	  "%s: %s",command_line->str,g_strerror(errno));
ali@0
    46
	return FALSE;
ali@0
    47
    }
ali@6
    48
    string=g_string_new(NULL);
ali@0
    49
    do
ali@0
    50
    {
ali@0
    51
	len=string->len;
ali@6
    52
	g_string_set_size(string,len+SPAWN_BUFSIZE);
ali@0
    53
	n=fread(string->str+len,1,SPAWN_BUFSIZE,fp);
ali@0
    54
	if (n<0)
ali@0
    55
	{
ali@7
    56
	    g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
ali@7
    57
	      "Error reading from bookloupe: %s",g_strerror(errno));
ali@0
    58
	    (void)pclose(fp);
ali@6
    59
	    g_string_free(string,TRUE);
ali@0
    60
	    return FALSE;
ali@0
    61
	}
ali@6
    62
	g_string_set_size(string,len+n);
ali@0
    63
    } while(n);
ali@0
    64
    r=pclose(fp);
ali@0
    65
    if (r<0)
ali@0
    66
    {
ali@7
    67
	g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
ali@7
    68
	  "Error reading from bookloupe: %s",g_strerror(errno));
ali@6
    69
	g_string_free(string,TRUE);
ali@0
    70
	return FALSE;
ali@0
    71
    }
ali@0
    72
    else
ali@0
    73
    {
ali@0
    74
	if (exit_status)
ali@0
    75
	    *exit_status=r;
ali@0
    76
	if (standard_output)
ali@6
    77
	    *standard_output=g_string_free(string,FALSE);
ali@0
    78
	else
ali@6
    79
	    g_string_free(string,TRUE);
ali@0
    80
	return TRUE;
ali@0
    81
    }
ali@0
    82
#endif
ali@0
    83
}