gclib/spawn.c
author ali <ali@juiblex.co.uk>
Tue Jan 24 23:54:05 2012 +0000 (2012-01-24)
changeset 0 c2f4c0285180
permissions -rw-r--r--
Initial version
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@0
     6
#include <gclib/gclib.h>
ali@0
     7
ali@0
     8
#define SPAWN_BUFSIZE	128
ali@0
     9
ali@0
    10
boolean 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@0
    13
#if HAVE_GLIB && !defined(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@0
    36
    String *command_line,*string;
ali@0
    37
    command_line=string_new(NULL);
ali@0
    38
    for(i=0;argv[i];i++)
ali@0
    39
    {
ali@0
    40
	if (i)
ali@0
    41
	    string_append_c(command_line,' ');
ali@0
    42
	string_append(command_line,argv[i]);
ali@0
    43
    }
ali@0
    44
    fp=popen(command_line->str,"r");
ali@0
    45
    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@0
    51
    string=string_new(NULL);
ali@0
    52
    do
ali@0
    53
    {
ali@0
    54
	len=string->len;
ali@0
    55
	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@0
    61
	    string_free(string,TRUE);
ali@0
    62
	    return FALSE;
ali@0
    63
	}
ali@0
    64
	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@0
    70
	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@0
    78
	    *standard_output=string_free(string,FALSE);
ali@0
    79
	else
ali@0
    80
	    string_free(string,TRUE);
ali@0
    81
	return TRUE;
ali@0
    82
    }
ali@0
    83
#endif
ali@0
    84
}