bl/spawn.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 16:18:02 2012 +0000 (2012-01-27)
changeset 6 faab25d520dd
parent 5 f600b0d1fc5d
child 7 721e468c10f3
permissions -rw-r--r--
Rely on glib being available everywhere
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@6
    10
gboolean spawn_sync(char **argv,char **standard_output,int *exit_status)
ali@0
    11
{
ali@0
    12
/* Don't use g_spawn_sync on WIN32 for now to avoid needing the helper */
ali@6
    13
#ifndef WIN32
ali@0
    14
    char *standard_error;
ali@0
    15
    GError *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@0
    21
      &standard_error,exit_status,&error);
ali@0
    22
    fputs(standard_error,stderr);
ali@0
    23
    g_free(standard_error);
ali@0
    24
    if (!retval)
ali@0
    25
    {
ali@0
    26
	fprintf(stderr,"%s\n",error->message);
ali@0
    27
	g_error_free(error);
ali@0
    28
    }
ali@0
    29
    else if (exit_status)
ali@0
    30
	*exit_status=WEXITSTATUS(*exit_status);
ali@0
    31
    return retval;
ali@0
    32
#else
ali@0
    33
    FILE *fp;
ali@0
    34
    int i,r;
ali@0
    35
    size_t n,len;
ali@6
    36
    GString *command_line,*string;
ali@6
    37
    command_line=g_string_new(NULL);
ali@0
    38
    for(i=0;argv[i];i++)
ali@0
    39
    {
ali@0
    40
	if (i)
ali@6
    41
	    g_string_append_c(command_line,' ');
ali@6
    42
	g_string_append(command_line,argv[i]);
ali@0
    43
    }
ali@0
    44
    fp=popen(command_line->str,"r");
ali@6
    45
    g_string_free(command_line,TRUE);
ali@0
    46
    if (!fp)
ali@0
    47
    {
ali@0
    48
	perror(command_line->str);
ali@0
    49
	return FALSE;
ali@0
    50
    }
ali@6
    51
    string=g_string_new(NULL);
ali@0
    52
    do
ali@0
    53
    {
ali@0
    54
	len=string->len;
ali@6
    55
	g_string_set_size(string,len+SPAWN_BUFSIZE);
ali@0
    56
	n=fread(string->str+len,1,SPAWN_BUFSIZE,fp);
ali@0
    57
	if (n<0)
ali@0
    58
	{
ali@0
    59
	    perror("fread");
ali@0
    60
	    (void)pclose(fp);
ali@6
    61
	    g_string_free(string,TRUE);
ali@0
    62
	    return FALSE;
ali@0
    63
	}
ali@6
    64
	g_string_set_size(string,len+n);
ali@0
    65
    } while(n);
ali@0
    66
    r=pclose(fp);
ali@0
    67
    if (r<0)
ali@0
    68
    {
ali@0
    69
	perror("pclose");
ali@6
    70
	g_string_free(string,TRUE);
ali@0
    71
	return FALSE;
ali@0
    72
    }
ali@0
    73
    else
ali@0
    74
    {
ali@0
    75
	if (exit_status)
ali@0
    76
	    *exit_status=r;
ali@0
    77
	if (standard_output)
ali@6
    78
	    *standard_output=g_string_free(string,FALSE);
ali@0
    79
	else
ali@6
    80
	    g_string_free(string,TRUE);
ali@0
    81
	return TRUE;
ali@0
    82
    }
ali@0
    83
#endif
ali@0
    84
}