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