/* * file: system.c * purpose: Provide UNIX compatible system() function * Author: Piet Tutelaers * version: 1.0 (Nov. 1995) */ #include #include #include #include #include #include "basics.h" /* * Next function emulates UNIX system() with spawnvp() (EMX/gcc). */ int emx_system(char *cmd) { int cnt, count; char *argv[10], *p, *q, *outname = NULL; p = cmd; count = 0; while (isspace(*p)) p++; while (*p != '\0') { cnt = 0; if (*p == '"') { p++; q = p; while (*q != '"') { if (*q == '\0') fatal("emx_run: invalid cmd\n"); cnt++; q++; } q++; } else if (*p == '>') { p++; while (isspace(*p)) p++; q = p; while (!isspace(*q) && *q != '\0') { cnt++; q++; } if ((outname = malloc(cnt+1)) == NULL) fatal("Out of memory\n"); strncpy(outname, p, cnt); *(outname+cnt) = '\0'; p = q; continue; } else { q = p; while (!isspace(*q) && *q != '\0') { cnt++; q++; } } if (count == 9) fatal("More than ten arguments\n"); if ((argv[count] = malloc(cnt+1)) == NULL) fatal("Out of memory\n"); strncpy(argv[count], p, cnt); argv[count][cnt] = '\0'; count++; p = q; while (isspace(*p)) p++; } argv[count] = NULL; if (outname != NULL) (void) freopen(outname, "w", stdout); return spawnvp(P_WAIT, argv[0], argv); }