Module: wine Branch: master Commit: 2a08d6ce8086baf62f687fa664cc3995ba8cb11b URL: https://source.winehq.org/git/wine.git/?a=commit;h=2a08d6ce8086baf62f687fa66...
Author: Alexandre Julliard julliard@winehq.org Date: Tue Sep 28 18:18:49 2021 +0200
tools: Add helper functions to spawn a command from an strarray.
Signed-off-by: Alexandre Julliard julliard@winehq.org
---
tools/tools.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/winebuild/utils.c | 8 ++------ tools/winegcc/utils.c | 21 ++++----------------- 3 files changed, 54 insertions(+), 23 deletions(-)
diff --git a/tools/tools.h b/tools/tools.h index bfbbb60b9e8..7c0f4c20a42 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -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 */ diff --git a/tools/winebuild/utils.c b/tools/winebuild/utils.c index f6468312902..de0d26ddba6 100644 --- a/tools/winebuild/utils.c +++ b/tools/winebuild/utils.c @@ -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" ); diff --git a/tools/winegcc/utils.c b/tools/winegcc/utils.c index 95764289391..4fc315db69e 100644 --- a/tools/winegcc/utils.c +++ b/tools/winegcc/utils.c @@ -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] );
- if (verbose) - { - for(i = 0; argv[i]; i++) - { - if (strpbrk(argv[i], " \t\n\r")) printf(""%s" ", argv[i]); - else printf("%s ", argv[i]); - } - printf("\n"); - } + args.str[0] = find_binary( prefix, args.str[0] ); + if (verbose) strarray_trace( args );
- if ((status = _spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors) + if ((status = strarray_spawn( args )) && !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); }