home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mega Top 1
/
os2_top1.zip
/
os2_top1
/
APPS
/
UTILS
/
H-O
/
LESS177
/
SYSTEM.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-07
|
3KB
|
102 lines
/* system.c (emx+gcc) -- Copyright (c) 1990-1992 by Eberhard Mattes */
#include <process.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <errno.h>
int system (const char *name)
{
int argc, arga, i, rc, my_errno;
int bs, quote;
char *tmp, *args, *p, *q, *sh, *dst;
const char *add = " /c ";
char **argv, **tav;
tmp = NULL; args = NULL; argv = NULL;
sh = getenv ("COMSPEC");
if (sh == NULL)
{
errno = ENOENT;
return (-1);
}
if (name == NULL) /* Check for command interpreter */
return (access (sh, 0) == 0);
if (*name == 0)
add = "";
i = strlen (sh) + strlen (add) + strlen (name) + 1;
tmp = malloc (i);
args = malloc (i);
if (tmp == NULL || args == NULL)
{
rc = -1; my_errno = ENOMEM;
goto done;
}
(void)strcpy (tmp, sh);
(void)strcat (tmp, add);
(void)strcat (tmp, name);
argc = 0; arga = 0; p = tmp; dst = args;
do {
while (*p == ' ' || *p == '\t' || *p == '\n')
++p;
if (argc >= arga)
{
arga += 20;
tav = (char **)realloc (argv, arga * sizeof (char *));
if (tav == NULL)
{
rc = -1; my_errno = ENOMEM;
goto done;
}
argv = tav;
}
if (*p == 0)
q = NULL;
else
{
q = dst; bs = 0; quote = 0;
for (;;)
{
if (*p == '"')
{
while (bs >= 2)
{
*dst++ = '\\';
bs -= 2;
}
if (bs & 1)
*dst++ = '"';
else
quote = !quote;
bs = 0;
}
else if (*p == '\\')
++bs;
else
{
while (bs != 0)
{
*dst++ = '\\';
--bs;
}
if (*p == 0 || ((*p == ' ' || *p == '\t') && !quote))
break;
*dst++ = *p;
}
++p;
}
*dst++ = 0;
}
argv[argc++] = q;
} while (q != NULL);
rc = spawnvp (P_WAIT, argv[0], (char const * const *)argv);
my_errno = errno;
done:
if (tmp != NULL) free (tmp);
if (argv != NULL) free (argv);
if (args != NULL) free (args);
errno = my_errno;
return (rc);
}