use the spoofed uname compiling git.
[AROS-Contrib.git] / rexx / src / systemx.c
blob391014f895f2f6d130ad86087393106d38687cfd
1 /*-----------------------------------------------------------------------*
2 * filename - systemx.c
4 * function(s)
5 * systemx - issues an MS-DOS command
6 * and returns the command return code
7 *-----------------------------------------------------------------------*/
9 #include <dos.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <string.h>
14 /* ------------- function prototypes ------------- */
15 static char *BDOSenv(char **envV, char *pathP, void **envSave);
16 int cdecl far _spawnx(char far *__path, char far *__cmd, char far *__env);
17 /* ------------- external variables -------------- */
18 extern void cdecl (*_exitbuf)(void);
20 /* ------------------ systemx -------------------- */
21 int far
22 systemx(const char far *cmd)
24 char *cmdP;
25 int cmdS;
26 char *envP;
27 void *envSave;
28 char *pathP;
29 int rc;
31 // Get COMMAND.COM path from COMSPEC
33 if ((pathP = getenv("COMSPEC")) == NULL) {
34 errno = ENOENT;
35 return (-1);
38 // Build command line "LEN /C cmd\r"
40 cmdS = 1 + 3 + strlen(cmd) + 1;
41 if (cmdS > 128) {
42 errno = E2BIG;
43 return (-1);
45 if ((cmdP = malloc(cmdS)) == NULL) {
46 errno = ENOMEM;
47 return (-1);
50 if (cmdS == 5) {
51 cmdP[0] = 0;
52 cmdP[1] = '\r';
53 } else {
54 *cmdP++ = cmdS - 2;
55 *cmdP++ = getswitchar();
56 cmdP = _stpcpy(cmdP, "c ");
57 cmdP = _stpcpy(cmdP, cmd);
58 *cmdP++ = '\r';
59 cmdP -= cmdS;
62 // Build environment for cmd
64 if ((envP = BDOSenv(environ, pathP, &envSave)) == NULL) {
65 errno = ENOMEM;
66 free(cmdP);
67 return (-1);
70 // Flush all byte streams before calling cmd
72 (*_exitbuf)();
74 // Now, call the low level _spawnx function
76 rc = _spawnx(pathP, cmdP, envP);
78 // Release all buffers, and exit
80 free(envSave);
81 free(cmdP);
82 return (rc);
83 } /* systemx */
85 /* ------------------ BDOSenv -------------------- */
86 //Description This function allocates a buffer and fill it with all the
87 // environment strings one after the others. If the pathP
88 // variable is nonzero, then it is appended to the end of the
89 // end of the environment assumed it is for a spawn or exec
90 // purpose.
92 //Return value BDOSenv returns a pointer to the environment buffer if
93 // successful, and NULL on error.
94 //----------------------------------------------------
95 static char *
96 BDOSenv(char **envV, char *pathP, void **envSave)
98 char **envW;
99 unsigned envS;
100 char *bufP;
102 // Compute the environment size including the NULL string at the
103 // end of the environment. (Environment size < 32 Kbytes)
105 envS = 1;
106 if ((envW = envV) != NULL)
107 for (envS = 0; *envW && **envW; envS += strlen(*envW++) + 1) ;
108 envS++;
109 if (pathP)
110 envS += 2 + strlen(pathP) + 1;
111 if (envS >= 0x2000)
112 return (NULL);
114 // Allocate a buffer
116 if ((bufP = malloc(envS + 15)) != NULL) {
118 // The environment MUST be paragraph aligned
120 *envSave = bufP;
121 bufP += 15;
122 (*((unsigned *)&(bufP))) &= 0xFFF0;
124 // Concatenate all environment strings
126 if ((envW = envV) != NULL && *envW != NULL)
127 while (*envW && **envW) {
128 bufP = _stpcpy(bufP, *envW++);
129 *bufP++ = '\0';
130 } else
131 *bufP++ = '\0';
132 *bufP++ = '\0';
134 // Append program name to the environment
136 if (pathP) {
137 *((short *)bufP)++ = 1;
138 bufP = _stpcpy(bufP, pathP);
139 *bufP++ = '\0';
141 return bufP - envS;
142 } else
143 return NULL;
144 } /* BDOSenv */