]> git.sur5r.net Git - cc65/blob - cl65/spawn.c
Fixed _textcolor definition.
[cc65] / cl65 / spawn.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                spawn.c                                    */
4 /*                                                                           */
5 /*                    Execute other external programs                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1999     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <unistd.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41
42 #include "error.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Code                                    */
48 /*****************************************************************************/
49
50
51
52 int spawnvp (int Mode, const char* File, char* const argv [])
53 /* Execute the given program searching and wait til it terminates. The Mode
54  * argument is ignored (compatibility only). The result of the function is
55  * the return code of the program. The function will terminate the program
56  * on errors.
57  */
58 {
59     int Status = 0;
60
61     /* Fork */
62     int pid = fork ();
63     if (pid < 0) {
64
65         /* Error forking */
66         Error ("Cannot fork: %s", strerror (errno));
67
68     } else if (pid == 0) {
69
70         /* The son - exec the program */
71         if (execvp (File, argv) < 0) {
72             Error ("Cannot exec `%s': %s", File, strerror (errno));
73         }
74
75     } else {
76
77         /* The father: Wait for the subprocess to terminate */
78         if (waitpid (pid, &Status, 0) < 0) {
79             Error ("Failure waiting for subprocess: %s", strerror (errno));
80         }
81
82         /* Examine the child status */
83         if (!WIFEXITED (Status)) {
84             Error ("Subprocess `%s' aborted by signal %d", File, WTERMSIG (Status));
85         }
86     }
87
88     /* Only the father goes here, we place a return here regardless of that
89      * to avoid compiler warnings.
90      */
91     return WEXITSTATUS (Status);
92 }
93
94
95