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