tools: Add helper functions to spawn a command from an strarray.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2021-09-28 18:18:49 +02:00
parent 97ca9f8a3d
commit 2a08d6ce80
3 changed files with 54 additions and 23 deletions

View File

@ -25,6 +25,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
# include <process.h>
#else
# include <sys/wait.h>
# include <unistd.h>
#endif
#if !defined(__GNUC__) && !defined(__attribute__)
#define __attribute__(x)
@ -200,5 +212,41 @@ static inline const char *strarray_bsearch( const struct strarray *array, const
return res ? *res : NULL;
}
static inline void strarray_trace( struct strarray args )
{
unsigned int i;
for (i = 0; i < args.count; i++)
{
if (strpbrk( args.str[i], " \t\n\r")) printf( "\"%s\"", args.str[i] );
else printf( "%s", args.str[i] );
putchar( i < args.count - 1 ? ' ' : '\n' );
}
}
static inline int strarray_spawn( struct strarray args )
{
#if defined(_WIN32) && !defined(__CYGWIN__)
strarray_add( &args, NULL );
return _spawnvp( _P_WAIT, args.str[0], args.str );
#else
pid_t pid, wret;
int status;
if (!(pid = fork()))
{
strarray_add( &args, NULL );
execvp( args.str[0], (char **)args.str );
_exit(1);
}
if (pid == -1) return -1;
while (pid != (wret = waitpid( pid, &status, 0 )))
if (wret == -1 && errno != EINTR) break;
if (pid == wret && WIFEXITED(status)) return WEXITSTATUS(status);
return 255; /* abnormal exit with an abort or an interrupt */
#endif
}
#endif /* __WINE_TOOLS_H */

View File

@ -206,17 +206,13 @@ static const char *find_binary( const char *prefix, const char *name )
void spawn( struct strarray args )
{
unsigned int i;
int status;
const char *argv0 = find_binary( NULL, args.str[0] );
if (argv0) args.str[0] = argv0;
strarray_add( &args, NULL );
if (verbose)
for (i = 0; args.str[i]; i++)
fprintf( stderr, "%s%c", args.str[i], args.str[i+1] ? ' ' : '\n' );
if (verbose) strarray_trace( args );
if ((status = _spawnvp( _P_WAIT, args.str[0], args.str )))
if ((status = strarray_spawn( args )))
{
if (status > 0) fatal_error( "%s failed with status %u\n", args.str[0], status );
else fatal_perror( "winebuild" );

View File

@ -197,27 +197,14 @@ const char *find_binary( struct strarray prefix, const char *name )
int spawn(struct strarray prefix, struct strarray args, int ignore_errors)
{
unsigned int i;
int status;
const char** argv;
strarray_add( &args, NULL);
argv = args.str;
argv[0] = find_binary( prefix, argv[0] );
args.str[0] = find_binary( prefix, args.str[0] );
if (verbose) strarray_trace( args );
if (verbose)
if ((status = strarray_spawn( args )) && !ignore_errors)
{
for(i = 0; argv[i]; i++)
{
if (strpbrk(argv[i], " \t\n\r")) printf("\"%s\" ", argv[i]);
else printf("%s ", argv[i]);
}
printf("\n");
}
if ((status = _spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors)
{
if (status > 0) error("%s failed\n", argv[0]);
if (status > 0) error("%s failed\n", args.str[0]);
else perror("winegcc");
exit(3);
}