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