1/* os/2 compatibility functions -- follows Ruby's license */
2
3#include "ruby.h"
4#include <stdio.h>
5#include <stdlib.h>
6#include <fcntl.h>
7#include <process.h>
8#include <limits.h>
9#include <errno.h>
10
11#define INCL_DOS
12#include <os2.h>
13
14int
15chown(char *path, int owner, int group)
16{
17	return 0;
18}
19
20#if 0
21int
22link(char *from, char *to)
23{
24	return -1;
25}
26#endif
27
28#if defined(EMX_REPLACE_GETCWD) && (EMX_REPLACE_GETCWD)  \
29 || defined(EMX_REPLACE_CHDIR)  && (EMX_REPLACE_CHDIR)
30#include <unistd.h>
31
32#if defined(EMX_REPLACE_GETCWD) && (EMX_REPLACE_GETCWD)
33/* to handle the drive letter and DBCS characters within a given path */
34char *
35getcwd(char *path, size_t len)
36{
37    return _getcwd2(path, (int)len);
38}
39#endif
40
41#if defined(EMX_REPLACE_CHDIR) && (EMX_REPLACE_CHDIR)
42/* to handle the drive letter and DBCS characters within a given path */
43int
44chdir(__const__ char *path)
45{
46    return _chdir2(path);
47}
48#endif
49#endif
50
51typedef char* CHARP;
52
53int
54do_spawn(cmd)
55char *cmd;
56{
57    register char **a;
58    register char *s;
59    char **argv;
60    char *shell, *sw, *cmd2;
61    int status;
62
63    if ((shell = getenv("RUBYSHELL")) != NULL && *shell != '\0') {
64	s = shell;
65	do
66	    *s = isupper(*s) ? tolower(*s) : *s;
67	while (*++s);
68	if (strstr(shell, "cmd") || strstr(shell, "4os2"))
69	    sw = "/c";
70	else
71	    sw = "-c";
72    } else if ((shell = getenv("SHELL")) != NULL && *shell != '\0') {
73	s = shell;
74	do
75	    *s = isupper(*s) ? tolower(*s) : *s;
76	while (*++s);
77	if (strstr(shell, "cmd") || strstr(shell, "4os2"))
78	    sw = "/c";
79	else
80	    sw = "-c";
81    } else if ((shell = getenv("COMSPEC")) != NULL && *shell != '\0') {
82	s = shell;
83	do
84	    *s = isupper(*s) ? tolower(*s) : *s;
85	while (*++s);
86	if (strstr(shell, "cmd") || strstr(shell, "4os2"))
87	    sw = "/c";
88	else
89	    sw = "-c";
90    }
91    /* see if there are shell metacharacters in it */
92    /*SUPPRESS 530*/
93    /*    for (s = cmd; *s && isalpha(*s); s++) ;
94    if (*s == '=')
95    goto doshell; */
96    for (s = cmd; *s; s++) {
97	if (*sw == '-' && *s != ' ' &&
98	    !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
99	    if (*s == '\n' && !s[1]) {
100		*s = '\0';
101		break;
102	    }
103	    goto doshell;
104	} else if (*sw == '/' && *s != ' ' &&
105	    !isalpha(*s) && index("^()<>|&\n",*s)) {
106	    if (*s == '\n' && !s[1]) {
107		*s = '\0';
108		break;
109	    }
110	  doshell:
111	    status = spawnlp(P_WAIT,shell,shell,sw,cmd,(char*)NULL);
112	    return status;
113	}
114    }
115    argv = ALLOC_N(CHARP,(strlen(cmd) / 2 + 2));
116    cmd2 = ALLOC_N(char, (strlen(cmd) + 1));
117    strcpy(cmd2, cmd);
118    a = argv;
119    for (s = cmd2; *s;) {
120	while (*s && isspace(*s)) s++;
121	if (*s)
122	    *(a++) = s;
123	while (*s && !isspace(*s)) s++;
124	if (*s)
125	    *s++ = '\0';
126    }
127    *a = NULL;
128    if (argv[0]) {
129	if ((status = spawnvp(P_WAIT, argv[0], argv)) == -1) {
130	    free(argv);
131	    free(cmd2);
132	    return -1;
133	}
134    }
135    free(cmd2);
136    free(argv);
137    return status;
138}
139