279a88f88b7726a3e1bc5e5cb2fcbbcd4375d1bb
[p5sagit/p5-mst-13.2.git] / os2 / os2.c
1 /* $Header: os2.c,v 3.0.1.1 90/10/15 17:49:55 lwall Locked $
2  *
3  *    (C) Copyright 1989, 1990 Diomidis Spinellis.
4  *
5  *    You may distribute under the terms of the GNU General Public License
6  *    as specified in the README file that comes with the perl 3.0 kit.
7  *
8  * $Log:        os2.c,v $
9  * Revision 3.0.1.1  90/10/15  17:49:55  lwall
10  * patch29: Initial revision
11  * 
12  * Revision 3.0.1.1  90/03/27  16:10:41  lwall
13  * patch16: MSDOS support
14  *
15  * Revision 1.1  90/03/18  20:32:01  dds
16  * Initial revision
17  *
18  */
19
20 #define INCL_DOS
21 #define INCL_NOPM
22 #include <os2.h>
23
24 /*
25  * Various Unix compatibility functions for OS/2
26  */
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <process.h>
31
32 #include "EXTERN.h"
33 #include "perl.h"
34
35
36 /* dummies */
37
38 int ioctl(int handle, unsigned int function, char *data)
39 { return -1; }
40
41 int userinit()
42 { return -1; }
43
44 int syscall()
45 { return -1; }
46
47
48 /* extendd chdir() */
49
50 int chdir(char *path)
51 {
52   if ( path[0] != 0 && path[1] == ':' )
53     DosSelectDisk(tolower(path[0]) - '@');
54
55   DosChDir(path, 0L);
56 }
57
58
59 /* priorities */
60
61 int setpriority(int class, int pid, int val)
62 {
63   int flag = 0;
64
65   if ( pid < 0 )
66   {
67     flag++;
68     pid = -pid;
69   }
70
71   return DosSetPrty(flag ? PRTYS_PROCESSTREE : PRTYS_PROCESS, class, val, pid);
72 }
73
74 int getpriority(int which /* ignored */, int pid)
75 {
76   USHORT val;
77
78   if ( DosGetPrty(PRTYS_PROCESS, &val, pid) )
79     return -1;
80   else
81     return val;
82 }
83
84
85 /* get parent process id */
86
87 int getppid(void)
88 {
89   PIDINFO pi;
90
91   DosGetPID(&pi);
92   return pi.pidParent;
93 }
94
95
96 /* kill */
97
98 int kill(int pid, int sig)
99 {
100   int flag = 0;
101
102   if ( pid < 0 )
103   {
104     flag++;
105     pid = -pid;
106   }
107
108   switch ( sig & 3 )
109   {
110
111   case 0:
112     DosKillProcess(flag ? DKP_PROCESSTREE : DKP_PROCESS, pid);
113     break;
114
115   case 1: /* FLAG A */
116     DosFlagProcess(pid, flag ? FLGP_SUBTREE : FLGP_PID, PFLG_A, 0);
117     break;
118
119   case 2: /* FLAG B */
120     DosFlagProcess(pid, flag ? FLGP_SUBTREE : FLGP_PID, PFLG_B, 0);
121     break;
122
123   case 3: /* FLAG C */
124     DosFlagProcess(pid, flag ? FLGP_SUBTREE : FLGP_PID, PFLG_C, 0);
125     break;
126
127   }
128 }
129
130
131 /* Sleep function. */
132 void
133 sleep(unsigned len)
134 {
135    DosSleep(len * 1000L);
136 }
137
138 /* Just pretend that everyone is a superuser */
139
140 int setuid()
141 { return 0; }
142
143 int setgid()
144 { return 0; }
145
146 int getuid(void)
147 { return 0; }
148
149 int geteuid(void)
150 { return 0; }
151
152 int getgid(void)
153 { return 0; }
154
155 int getegid(void)
156 { return 0; }
157
158 /*
159  * The following code is based on the do_exec and do_aexec functions
160  * in file doio.c
161  */
162 int
163 do_aspawn(really,arglast)
164 STR *really;
165 int *arglast;
166 {
167     register STR **st = stack->ary_array;
168     register int sp = arglast[1];
169     register int items = arglast[2] - sp;
170     register char **a;
171     char **argv;
172     char *tmps;
173     int status;
174
175     if (items) {
176         New(1101,argv, items+1, char*);
177         a = argv;
178         for (st += ++sp; items > 0; items--,st++) {
179             if (*st)
180                 *a++ = str_get(*st);
181             else
182                 *a++ = "";
183         }
184         *a = Nullch;
185         if (really && *(tmps = str_get(really)))
186             status = spawnvp(P_WAIT,tmps,argv);
187         else
188             status = spawnvp(P_WAIT,argv[0],argv);
189         Safefree(argv);
190     }
191     return status;
192 }
193
194 char *getenv(char *name);
195
196 int
197 do_spawn(cmd)
198 char *cmd;
199 {
200     register char **a;
201     register char *s;
202     char **argv;
203     char flags[10];
204     int status;
205     char *shell, *cmd2;
206
207     /* save an extra exec if possible */
208     if ((shell = getenv("COMSPEC")) == 0)
209         shell = "C:\\OS2\\CMD.EXE";
210
211     /* see if there are shell metacharacters in it */
212     if (strchr(cmd, '>') || strchr(cmd, '<') || strchr(cmd, '|')
213         || strchr(cmd, '&') || strchr(cmd, '^'))
214           doshell:
215             return spawnl(P_WAIT,shell,shell,"/C",cmd,(char*)0);
216
217     New(1102,argv, strlen(cmd) / 2 + 2, char*);
218
219     New(1103,cmd2, strlen(cmd) + 1, char);
220     strcpy(cmd2, cmd);
221     a = argv;
222     for (s = cmd2; *s;) {
223         while (*s && isspace(*s)) s++;
224         if (*s)
225             *(a++) = s;
226         while (*s && !isspace(*s)) s++;
227         if (*s)
228             *s++ = '\0';
229     }
230     *a = Nullch;
231     if (argv[0])
232         if ((status = spawnvp(P_WAIT,argv[0],argv)) == -1) {
233             Safefree(argv);
234             Safefree(cmd2);
235             goto doshell;
236         }
237     Safefree(cmd2);
238     Safefree(argv);
239     return status;
240 }
241
242 usage(char *myname)
243 {
244 #ifdef MSDOS
245   printf("\nUsage: %s [-acdnpsSvw] [-Dnumber] [-i[extension]] [-Idirectory]"
246 #else
247   printf("\nUsage: %s [-acdnpPsSuUvw] [-Dnumber] [-i[extension]] [-Idirectory]"
248 #endif
249          "\n            [-e \"command\"] [-x[directory]] [filename] [arguments]\n", myname);
250
251   printf("\n  -a  autosplit mode with -n or -p"
252          "\n  -c  syntaxcheck only"
253          "\n  -d  run scripts under debugger"
254          "\n  -n  assume 'while (<>) { ...script... }' loop arround your script"
255          "\n  -p  assume loop like -n but print line also like sed"
256 #ifndef MSDOS
257          "\n  -P  run script through C preprocessor befor compilation"
258 #endif
259          "\n  -s  enable some switch parsing for switches after script name"
260          "\n  -S  look for the script using PATH environment variable");
261 #ifndef MSDOS
262   printf("\n  -u  dump core after compiling the script"
263          "\n  -U  allow unsafe operations");
264 #endif
265   printf("\n  -v  print version number and patchlevel of perl"
266          "\n  -w  turn warnings on for compilation of your script\n"
267          "\n  -Dnumber        set debugging flags"
268          "\n  -i[extension]   edit <> files in place (make backup if extension supplied)"
269          "\n  -Idirectory     specify include directory in conjunction with -P"
270          "\n  -e command      one line of script, multiple -e options are allowed"
271          "\n                  [filename] can be ommitted, when -e is used"
272          "\n  -x[directory]   strip off text before #!perl line and perhaps cd to directory\n");
273 }