Commit | Line | Data |
a0d0e21e |
1 | /* doio.c |
a687059c |
2 | * |
bc89e66f |
3 | * Copyright (c) 1991-2001, Larry Wall |
a687059c |
4 | * |
6e21c824 |
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. |
a687059c |
7 | * |
a0d0e21e |
8 | */ |
9 | |
10 | /* |
11 | * "Far below them they saw the white waters pour into a foaming bowl, and |
12 | * then swirl darkly about a deep oval basin in the rocks, until they found |
13 | * their way out again through a narrow gate, and flowed away, fuming and |
14 | * chattering, into calmer and more level reaches." |
a687059c |
15 | */ |
16 | |
17 | #include "EXTERN.h" |
864dbfa3 |
18 | #define PERL_IN_DOIO_C |
a687059c |
19 | #include "perl.h" |
20 | |
fe14fcc3 |
21 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
aec308ec |
22 | #ifndef HAS_SEM |
c2ab57d4 |
23 | #include <sys/ipc.h> |
aec308ec |
24 | #endif |
fe14fcc3 |
25 | #ifdef HAS_MSG |
c2ab57d4 |
26 | #include <sys/msg.h> |
e5d73d77 |
27 | #endif |
fe14fcc3 |
28 | #ifdef HAS_SHM |
c2ab57d4 |
29 | #include <sys/shm.h> |
a0d0e21e |
30 | # ifndef HAS_SHMAT_PROTOTYPE |
20ce7b12 |
31 | extern Shmat_t shmat (int, char *, int); |
a0d0e21e |
32 | # endif |
c2ab57d4 |
33 | #endif |
e5d73d77 |
34 | #endif |
c2ab57d4 |
35 | |
663a0e37 |
36 | #ifdef I_UTIME |
3730b96e |
37 | # if defined(_MSC_VER) || defined(__MINGW32__) |
3fe9a6f1 |
38 | # include <sys/utime.h> |
39 | # else |
40 | # include <utime.h> |
41 | # endif |
663a0e37 |
42 | #endif |
85aff577 |
43 | |
85aff577 |
44 | #ifdef O_EXCL |
45 | # define OPEN_EXCL O_EXCL |
46 | #else |
47 | # define OPEN_EXCL 0 |
48 | #endif |
a687059c |
49 | |
76121258 |
50 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
51 | #include <signal.h> |
52 | #endif |
53 | |
a687059c |
54 | bool |
6170680b |
55 | Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
56 | int rawmode, int rawperm, PerlIO *supplied_fp) |
57 | { |
a567e93b |
58 | return do_openn(gv, name, len, as_raw, rawmode, rawperm, |
59 | supplied_fp, (SV **) NULL, 0); |
6170680b |
60 | } |
61 | |
62 | bool |
63 | Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
64 | int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs, |
65 | I32 num_svs) |
a687059c |
66 | { |
a567e93b |
67 | return do_openn(gv, name, len, as_raw, rawmode, rawperm, |
68 | supplied_fp, &svs, 1); |
69 | } |
70 | |
71 | bool |
72 | Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
73 | int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp, |
74 | I32 num_svs) |
75 | { |
a0d0e21e |
76 | register IO *io = GvIOn(gv); |
760ac839 |
77 | PerlIO *saveifp = Nullfp; |
78 | PerlIO *saveofp = Nullfp; |
ee518936 |
79 | int savefd = -1; |
9f37169a |
80 | char savetype = IoTYPE_CLOSED; |
c07a80fd |
81 | int writing = 0; |
760ac839 |
82 | PerlIO *fp; |
c07a80fd |
83 | int fd; |
84 | int result; |
3500f679 |
85 | bool was_fdopen = FALSE; |
16fe6d59 |
86 | bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0; |
b931b1d9 |
87 | char *type = NULL; |
ee518936 |
88 | char mode[8]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */ |
89 | SV *namesv; |
a687059c |
90 | |
b931b1d9 |
91 | Zero(mode,sizeof(mode),char); |
3280af22 |
92 | PL_forkprocess = 1; /* assume true if no fork */ |
c07a80fd |
93 | |
b931b1d9 |
94 | /* Collect default raw/crlf info from the op */ |
16fe6d59 |
95 | if (PL_op && PL_op->op_type == OP_OPEN) { |
96 | /* set up disciplines */ |
97 | U8 flags = PL_op->op_private; |
98 | in_raw = (flags & OPpOPEN_IN_RAW); |
99 | in_crlf = (flags & OPpOPEN_IN_CRLF); |
100 | out_raw = (flags & OPpOPEN_OUT_RAW); |
101 | out_crlf = (flags & OPpOPEN_OUT_CRLF); |
102 | } |
103 | |
b931b1d9 |
104 | /* If currently open - close before we re-open */ |
a0d0e21e |
105 | if (IoIFP(io)) { |
760ac839 |
106 | fd = PerlIO_fileno(IoIFP(io)); |
ee518936 |
107 | if (IoTYPE(io) == IoTYPE_STD) { |
108 | /* This is a clone of one of STD* handles */ |
c2ab57d4 |
109 | result = 0; |
ee518936 |
110 | } |
111 | else if (fd >= 0 && fd <= PL_maxsysfd) { |
112 | /* This is one of the original STD* handles */ |
113 | saveifp = IoIFP(io); |
114 | saveofp = IoOFP(io); |
8990e307 |
115 | savetype = IoTYPE(io); |
ee518936 |
116 | savefd = fd; |
117 | result = 0; |
6e21c824 |
118 | } |
50952442 |
119 | else if (IoTYPE(io) == IoTYPE_PIPE) |
3028581b |
120 | result = PerlProc_pclose(IoIFP(io)); |
8990e307 |
121 | else if (IoIFP(io) != IoOFP(io)) { |
122 | if (IoOFP(io)) { |
760ac839 |
123 | result = PerlIO_close(IoOFP(io)); |
6170680b |
124 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
125 | } |
126 | else |
760ac839 |
127 | result = PerlIO_close(IoIFP(io)); |
a687059c |
128 | } |
a687059c |
129 | else |
760ac839 |
130 | result = PerlIO_close(IoIFP(io)); |
ee518936 |
131 | if (result == EOF && fd > PL_maxsysfd) { |
132 | /* Why is this not Perl_warn*() call ? */ |
bf49b057 |
133 | PerlIO_printf(Perl_error_log, |
6170680b |
134 | "Warning: unable to close filehandle %s properly.\n", |
135 | GvENAME(gv)); |
ee518936 |
136 | } |
8990e307 |
137 | IoOFP(io) = IoIFP(io) = Nullfp; |
a687059c |
138 | } |
c07a80fd |
139 | |
140 | if (as_raw) { |
b931b1d9 |
141 | /* sysopen style args, i.e. integer mode and permissions */ |
ee518936 |
142 | STRLEN ix = 0; |
4a7d1889 |
143 | if (num_svs != 0) { |
b94c04ac |
144 | Perl_croak(aTHX_ "panic: sysopen with multiple args"); |
4a7d1889 |
145 | } |
d1da7611 |
146 | if (rawmode & (O_WRONLY|O_RDWR|O_CREAT |
147 | #ifdef O_APPEND /* Not fully portable. */ |
148 | |O_APPEND |
149 | #endif |
150 | #ifdef O_TRUNC /* Not fully portable. */ |
151 | |O_TRUNC |
152 | #endif |
153 | )) |
b94c04ac |
154 | TAINT_PROPER("sysopen"); |
ee518936 |
155 | mode[ix++] = '#'; /* Marker to openn to use numeric "sysopen" */ |
b931b1d9 |
156 | |
09458382 |
157 | #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE) |
b94c04ac |
158 | rawmode |= O_LARGEFILE; /* Transparently largefiley. */ |
5ff3f7a4 |
159 | #endif |
160 | |
9d116dd7 |
161 | #ifndef O_ACCMODE |
162 | #define O_ACCMODE 3 /* Assume traditional implementation */ |
163 | #endif |
5ff3f7a4 |
164 | |
9d116dd7 |
165 | switch (result = rawmode & O_ACCMODE) { |
166 | case O_RDONLY: |
50952442 |
167 | IoTYPE(io) = IoTYPE_RDONLY; |
9d116dd7 |
168 | break; |
169 | case O_WRONLY: |
50952442 |
170 | IoTYPE(io) = IoTYPE_WRONLY; |
9d116dd7 |
171 | break; |
172 | case O_RDWR: |
173 | default: |
50952442 |
174 | IoTYPE(io) = IoTYPE_RDWR; |
9d116dd7 |
175 | break; |
176 | } |
ef0d4f5b |
177 | writing = (result != O_RDONLY); |
9d116dd7 |
178 | |
ee518936 |
179 | if (result == O_RDONLY) { |
180 | mode[ix++] = 'r'; |
181 | } |
360e5741 |
182 | #ifdef O_APPEND |
ee518936 |
183 | else if (rawmode & O_APPEND) { |
184 | mode[ix++] = 'a'; |
185 | if (result != O_WRONLY) |
186 | mode[ix++] = '+'; |
187 | } |
360e5741 |
188 | #endif |
ee518936 |
189 | else { |
190 | if (result == O_WRONLY) |
191 | mode[ix++] = 'w'; |
16fe6d59 |
192 | else { |
ee518936 |
193 | mode[ix++] = 'r'; |
194 | mode[ix++] = '+'; |
16fe6d59 |
195 | } |
c07a80fd |
196 | } |
ee518936 |
197 | if (rawmode & O_BINARY) |
198 | mode[ix++] = 'b'; |
199 | mode[ix] = '\0'; |
200 | |
201 | namesv = sv_2mortal(newSVpvn(name,strlen(name))); |
202 | num_svs = 1; |
203 | svp = &namesv; |
1141d9f8 |
204 | type = Nullch; |
b94c04ac |
205 | fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp); |
a687059c |
206 | } |
c07a80fd |
207 | else { |
b931b1d9 |
208 | /* Regular (non-sys) open */ |
faecd977 |
209 | char *oname = name; |
faecd977 |
210 | STRLEN olen = len; |
b931b1d9 |
211 | char *tend; |
212 | int dodup = 0; |
c07a80fd |
213 | |
faecd977 |
214 | type = savepvn(name, len); |
b931b1d9 |
215 | tend = type+len; |
faecd977 |
216 | SAVEFREEPV(type); |
b931b1d9 |
217 | /* Loose trailing white space */ |
218 | while (tend > type && isSPACE(tend[-1])) |
219 | *tend-- = '\0'; |
6170680b |
220 | if (num_svs) { |
b931b1d9 |
221 | /* New style explict name, type is just mode and discipline/layer info */ |
fe9745bf |
222 | STRLEN l = 0; |
f6c77cf1 |
223 | name = SvOK(*svp) ? SvPV(*svp, l) : ""; |
faecd977 |
224 | len = (I32)l; |
225 | name = savepvn(name, len); |
226 | SAVEFREEPV(name); |
b931b1d9 |
227 | /*SUPPRESS 530*/ |
228 | for (; isSPACE(*type); type++) ; |
6170680b |
229 | } |
faecd977 |
230 | else { |
faecd977 |
231 | name = type; |
b931b1d9 |
232 | len = tend-type; |
faecd977 |
233 | } |
6170680b |
234 | IoTYPE(io) = *type; |
516a5887 |
235 | if ((*type == IoTYPE_RDWR) && /* scary */ |
01a8ea99 |
236 | (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) && |
516a5887 |
237 | ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) { |
dd531b3b |
238 | TAINT_PROPER("open"); |
6170680b |
239 | mode[1] = *type++; |
c07a80fd |
240 | writing = 1; |
a687059c |
241 | } |
c07a80fd |
242 | |
9f37169a |
243 | if (*type == IoTYPE_PIPE) { |
b931b1d9 |
244 | if (num_svs) { |
245 | if (type[1] != IoTYPE_STD) { |
246 | unknown_desr: |
247 | Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname); |
248 | } |
249 | type++; |
6170680b |
250 | } |
c07a80fd |
251 | /*SUPPRESS 530*/ |
b931b1d9 |
252 | for (type++; isSPACE(*type); type++) ; |
faecd977 |
253 | if (!num_svs) { |
6170680b |
254 | name = type; |
b931b1d9 |
255 | len = tend-type; |
faecd977 |
256 | } |
4a7d1889 |
257 | if (*name == '\0') { |
258 | /* command is missing 19990114 */ |
06eaf0bc |
259 | if (ckWARN(WARN_PIPE)) |
cea2e8a9 |
260 | Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open"); |
06eaf0bc |
261 | errno = EPIPE; |
262 | goto say_false; |
263 | } |
6170680b |
264 | if (strNE(name,"-") || num_svs) |
c07a80fd |
265 | TAINT_ENV(); |
266 | TAINT_PROPER("piped open"); |
b931b1d9 |
267 | if (!num_svs && name[len-1] == '|') { |
faecd977 |
268 | name[--len] = '\0' ; |
599cee73 |
269 | if (ckWARN(WARN_PIPE)) |
9a7dcd9c |
270 | Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe"); |
7b8d334a |
271 | } |
a1d180c4 |
272 | mode[0] = 'w'; |
c07a80fd |
273 | writing = 1; |
a1d180c4 |
274 | if (out_raw) |
275 | strcat(mode, "b"); |
276 | else if (out_crlf) |
277 | strcat(mode, "t"); |
4a7d1889 |
278 | if (num_svs > 1) { |
279 | fp = PerlProc_popen_list(mode, num_svs, svp); |
280 | } |
281 | else { |
282 | fp = PerlProc_popen(name,mode); |
283 | } |
c07a80fd |
284 | } |
9f37169a |
285 | else if (*type == IoTYPE_WRONLY) { |
c07a80fd |
286 | TAINT_PROPER("open"); |
6170680b |
287 | type++; |
9f37169a |
288 | if (*type == IoTYPE_WRONLY) { |
289 | /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */ |
50952442 |
290 | mode[0] = IoTYPE(io) = IoTYPE_APPEND; |
6170680b |
291 | type++; |
a0d0e21e |
292 | } |
ee518936 |
293 | else { |
c07a80fd |
294 | mode[0] = 'w'; |
ee518936 |
295 | } |
c07a80fd |
296 | writing = 1; |
297 | |
16fe6d59 |
298 | if (out_raw) |
299 | strcat(mode, "b"); |
300 | else if (out_crlf) |
301 | strcat(mode, "t"); |
302 | |
6170680b |
303 | if (*type == '&') { |
c07a80fd |
304 | duplicity: |
305 | dodup = 1; |
e620cd72 |
306 | type++; |
307 | if (*type == '=') { |
c07a80fd |
308 | dodup = 0; |
e620cd72 |
309 | type++; |
4a7d1889 |
310 | } |
ee518936 |
311 | if (!num_svs && !*type && supplied_fp) { |
4a7d1889 |
312 | /* "<+&" etc. is used by typemaps */ |
c07a80fd |
313 | fp = supplied_fp; |
ee518936 |
314 | } |
a0d0e21e |
315 | else { |
e620cd72 |
316 | if (num_svs > 1) { |
317 | Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io)); |
318 | } |
ee518936 |
319 | if (num_svs && SvIOK(*svp)) { |
e620cd72 |
320 | fd = SvUV(*svp); |
ee518936 |
321 | } |
e620cd72 |
322 | else if (isDIGIT(*type)) { |
323 | /*SUPPRESS 530*/ |
324 | for (; isSPACE(*type); type++) ; |
325 | fd = atoi(type); |
326 | } |
c07a80fd |
327 | else { |
328 | IO* thatio; |
e620cd72 |
329 | if (num_svs) { |
330 | thatio = sv_2io(*svp); |
331 | } |
332 | else { |
333 | GV *thatgv; |
334 | /*SUPPRESS 530*/ |
335 | for (; isSPACE(*type); type++) ; |
336 | thatgv = gv_fetchpv(type,FALSE,SVt_PVIO); |
337 | thatio = GvIO(thatgv); |
338 | } |
c07a80fd |
339 | if (!thatio) { |
6e21c824 |
340 | #ifdef EINVAL |
c07a80fd |
341 | SETERRNO(EINVAL,SS$_IVCHAN); |
6e21c824 |
342 | #endif |
c07a80fd |
343 | goto say_false; |
344 | } |
345 | if (IoIFP(thatio)) { |
54195c32 |
346 | PerlIO *fp = IoIFP(thatio); |
7211d486 |
347 | /* Flush stdio buffer before dup. --mjd |
348 | * Unfortunately SEEK_CURing 0 seems to |
349 | * be optimized away on most platforms; |
350 | * only Solaris and Linux seem to flush |
351 | * on that. --jhi */ |
2c534a3f |
352 | #ifdef USE_SFIO |
353 | /* sfio fails to clear error on next |
354 | sfwrite, contrary to documentation. |
355 | -- Nick Clark */ |
356 | if (PerlIO_seek(fp, 0, SEEK_CUR) == -1) |
357 | PerlIO_clearerr(fp); |
358 | #endif |
7211d486 |
359 | /* On the other hand, do all platforms |
360 | * take gracefully to flushing a read-only |
361 | * filehandle? Perhaps we should do |
362 | * fsetpos(src)+fgetpos(dst)? --nik */ |
363 | PerlIO_flush(fp); |
54195c32 |
364 | fd = PerlIO_fileno(fp); |
0759c907 |
365 | /* When dup()ing STDIN, STDOUT or STDERR |
366 | * explicitly set appropriate access mode */ |
367 | if (IoIFP(thatio) == PerlIO_stdout() |
368 | || IoIFP(thatio) == PerlIO_stderr()) |
369 | IoTYPE(io) = IoTYPE_WRONLY; |
370 | else if (IoIFP(thatio) == PerlIO_stdin()) |
371 | IoTYPE(io) = IoTYPE_RDONLY; |
372 | /* When dup()ing a socket, say result is |
373 | * one as well */ |
374 | else if (IoTYPE(thatio) == IoTYPE_SOCKET) |
50952442 |
375 | IoTYPE(io) = IoTYPE_SOCKET; |
c07a80fd |
376 | } |
377 | else |
378 | fd = -1; |
a0d0e21e |
379 | } |
fec02dd3 |
380 | if (dodup) |
3028581b |
381 | fd = PerlLIO_dup(fd); |
3500f679 |
382 | else |
383 | was_fdopen = TRUE; |
ee518936 |
384 | if (!num_svs) |
1141d9f8 |
385 | type = Nullch; |
ee518936 |
386 | if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) { |
c07a80fd |
387 | if (dodup) |
3028581b |
388 | PerlLIO_close(fd); |
faecd977 |
389 | } |
c07a80fd |
390 | } |
ee518936 |
391 | } /* & */ |
c07a80fd |
392 | else { |
4a7d1889 |
393 | if (num_svs > 1) { |
394 | Perl_croak(aTHX_ "More than one argument to '>' open"); |
395 | } |
c07a80fd |
396 | /*SUPPRESS 530*/ |
6170680b |
397 | for (; isSPACE(*type); type++) ; |
b931b1d9 |
398 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
399 | /*SUPPRESS 530*/ |
400 | type++; |
760ac839 |
401 | fp = PerlIO_stdout(); |
50952442 |
402 | IoTYPE(io) = IoTYPE_STD; |
c07a80fd |
403 | } |
404 | else { |
ee518936 |
405 | if (!num_svs) { |
406 | namesv = sv_2mortal(newSVpvn(type,strlen(type))); |
407 | num_svs = 1; |
408 | svp = &namesv; |
1141d9f8 |
409 | type = Nullch; |
ee518936 |
410 | } |
6e60e805 |
411 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
c07a80fd |
412 | } |
ee518936 |
413 | } /* !& */ |
bf38876a |
414 | } |
9f37169a |
415 | else if (*type == IoTYPE_RDONLY) { |
4a7d1889 |
416 | if (num_svs > 1) { |
417 | Perl_croak(aTHX_ "More than one argument to '<' open"); |
418 | } |
c07a80fd |
419 | /*SUPPRESS 530*/ |
6170680b |
420 | for (type++; isSPACE(*type); type++) ; |
bf38876a |
421 | mode[0] = 'r'; |
16fe6d59 |
422 | if (in_raw) |
423 | strcat(mode, "b"); |
424 | else if (in_crlf) |
425 | strcat(mode, "t"); |
426 | |
6170680b |
427 | if (*type == '&') { |
bf38876a |
428 | goto duplicity; |
6170680b |
429 | } |
b931b1d9 |
430 | if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) { |
431 | /*SUPPRESS 530*/ |
432 | type++; |
760ac839 |
433 | fp = PerlIO_stdin(); |
50952442 |
434 | IoTYPE(io) = IoTYPE_STD; |
a687059c |
435 | } |
ee518936 |
436 | else { |
437 | if (!num_svs) { |
438 | namesv = sv_2mortal(newSVpvn(type,strlen(type))); |
439 | num_svs = 1; |
440 | svp = &namesv; |
1141d9f8 |
441 | type = Nullch; |
ee518936 |
442 | } |
6e60e805 |
443 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
ee518936 |
444 | } |
a687059c |
445 | } |
b931b1d9 |
446 | else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) || |
447 | (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) { |
6170680b |
448 | if (num_svs) { |
b931b1d9 |
449 | type += 2; /* skip over '-|' */ |
6170680b |
450 | } |
451 | else { |
b931b1d9 |
452 | *--tend = '\0'; |
453 | while (tend > type && isSPACE(tend[-1])) |
454 | *--tend = '\0'; |
6170680b |
455 | /*SUPPRESS 530*/ |
456 | for (; isSPACE(*type); type++) ; |
457 | name = type; |
b931b1d9 |
458 | len = tend-type; |
6170680b |
459 | } |
4a7d1889 |
460 | if (*name == '\0') { |
461 | /* command is missing 19990114 */ |
06eaf0bc |
462 | if (ckWARN(WARN_PIPE)) |
cea2e8a9 |
463 | Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open"); |
06eaf0bc |
464 | errno = EPIPE; |
465 | goto say_false; |
466 | } |
6170680b |
467 | if (strNE(name,"-") || num_svs) |
79072805 |
468 | TAINT_ENV(); |
469 | TAINT_PROPER("piped open"); |
a1d180c4 |
470 | mode[0] = 'r'; |
471 | if (in_raw) |
472 | strcat(mode, "b"); |
473 | else if (in_crlf) |
474 | strcat(mode, "t"); |
4a7d1889 |
475 | if (num_svs > 1) { |
476 | fp = PerlProc_popen_list(mode,num_svs,svp); |
477 | } |
e620cd72 |
478 | else { |
4a7d1889 |
479 | fp = PerlProc_popen(name,mode); |
480 | } |
50952442 |
481 | IoTYPE(io) = IoTYPE_PIPE; |
a687059c |
482 | } |
483 | else { |
6170680b |
484 | if (num_svs) |
485 | goto unknown_desr; |
486 | name = type; |
50952442 |
487 | IoTYPE(io) = IoTYPE_RDONLY; |
99b89507 |
488 | /*SUPPRESS 530*/ |
489 | for (; isSPACE(*name); name++) ; |
88b61e10 |
490 | mode[0] = 'r'; |
491 | if (in_raw) |
492 | strcat(mode, "b"); |
493 | else if (in_crlf) |
494 | strcat(mode, "t"); |
a687059c |
495 | if (strEQ(name,"-")) { |
760ac839 |
496 | fp = PerlIO_stdin(); |
50952442 |
497 | IoTYPE(io) = IoTYPE_STD; |
a687059c |
498 | } |
16fe6d59 |
499 | else { |
ee518936 |
500 | if (!num_svs) { |
501 | namesv = sv_2mortal(newSVpvn(type,strlen(type))); |
502 | num_svs = 1; |
503 | svp = &namesv; |
1141d9f8 |
504 | type = Nullch; |
ee518936 |
505 | } |
6e60e805 |
506 | fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp); |
16fe6d59 |
507 | } |
a687059c |
508 | } |
509 | } |
bee1dbe2 |
510 | if (!fp) { |
50952442 |
511 | if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n')) |
cea2e8a9 |
512 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open"); |
6e21c824 |
513 | goto say_false; |
bee1dbe2 |
514 | } |
a00b5bd3 |
515 | |
516 | if (ckWARN(WARN_IO)) { |
517 | if ((IoTYPE(io) == IoTYPE_RDONLY) && |
518 | (fp == PerlIO_stdout() || fp == PerlIO_stderr())) { |
c5e2d979 |
519 | Perl_warner(aTHX_ WARN_IO, |
520 | "Filehandle STD%s opened only for input", |
521 | (fp == PerlIO_stdout()) ? "OUT" : "ERR"); |
a00b5bd3 |
522 | } |
ee518936 |
523 | else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) { |
c5e2d979 |
524 | Perl_warner(aTHX_ WARN_IO, |
525 | "Filehandle STDIN opened only for output"); |
a00b5bd3 |
526 | } |
527 | } |
528 | |
f6c77cf1 |
529 | if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && |
530 | /* FIXME: This next term is a hack to avoid fileno on PerlIO::Scalar */ |
531 | !(num_svs && SvROK(*svp))) { |
3280af22 |
532 | if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) { |
760ac839 |
533 | (void)PerlIO_close(fp); |
6e21c824 |
534 | goto say_false; |
a687059c |
535 | } |
3280af22 |
536 | if (S_ISSOCK(PL_statbuf.st_mode)) |
50952442 |
537 | IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */ |
99b89507 |
538 | #ifdef HAS_SOCKET |
539 | else if ( |
c623bd54 |
540 | #ifdef S_IFMT |
3280af22 |
541 | !(PL_statbuf.st_mode & S_IFMT) |
99b89507 |
542 | #else |
b28d0864 |
543 | !PL_statbuf.st_mode |
99b89507 |
544 | #endif |
0759c907 |
545 | && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */ |
546 | && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */ |
547 | ) { /* on OS's that return 0 on fstat()ed pipe */ |
96827780 |
548 | char tmpbuf[256]; |
549 | Sock_size_t buflen = sizeof tmpbuf; |
3028581b |
550 | if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf, |
d574b85e |
551 | &buflen) >= 0 |
552 | || errno != ENOTSOCK) |
50952442 |
553 | IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */ |
99b89507 |
554 | /* but some return 0 for streams too, sigh */ |
555 | } |
bf38876a |
556 | #endif |
a687059c |
557 | } |
6e21c824 |
558 | if (saveifp) { /* must use old fp? */ |
f5b9d040 |
559 | /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR |
24c23ab4 |
560 | then dup the new fileno down |
f5b9d040 |
561 | */ |
6e60e805 |
562 | fd = PerlIO_fileno(fp); |
6e21c824 |
563 | if (saveofp) { |
f5b9d040 |
564 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 |
565 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 |
566 | PerlIO_close(saveofp); |
6e21c824 |
567 | } |
568 | } |
6e60e805 |
569 | if (savefd != fd) { |
d8a83dd3 |
570 | Pid_t pid; |
79072805 |
571 | SV *sv; |
bd4a5668 |
572 | if (PerlLIO_dup2(fd, savefd) < 0) { |
573 | (void)PerlIO_close(fp); |
574 | goto say_false; |
575 | } |
d082dcd6 |
576 | #ifdef VMS |
6e60e805 |
577 | if (savefd != PerlIO_fileno(PerlIO_stdin())) { |
d082dcd6 |
578 | char newname[FILENAME_MAX+1]; |
a15cef0c |
579 | if (PerlIO_getname(fp, newname)) { |
fd8cd3a3 |
580 | if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname); |
581 | if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname); |
d082dcd6 |
582 | } |
583 | } |
584 | #endif |
4755096e |
585 | LOCK_FDPID_MUTEX; |
6e60e805 |
586 | sv = *av_fetch(PL_fdpid,fd,TRUE); |
a0d0e21e |
587 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 |
588 | pid = SvIVX(sv); |
589 | SvIVX(sv) = 0; |
6e60e805 |
590 | sv = *av_fetch(PL_fdpid,savefd,TRUE); |
4755096e |
591 | UNLOCK_FDPID_MUTEX; |
a0d0e21e |
592 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 |
593 | SvIVX(sv) = pid; |
3500f679 |
594 | if (!was_fdopen) |
595 | PerlIO_close(fp); |
6e21c824 |
596 | } |
597 | fp = saveifp; |
760ac839 |
598 | PerlIO_clearerr(fp); |
6e21c824 |
599 | } |
a0d0e21e |
600 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
a8710ca1 |
601 | { |
602 | int save_errno = errno; |
603 | fd = PerlIO_fileno(fp); |
604 | fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */ |
605 | errno = save_errno; |
606 | } |
1462b684 |
607 | #endif |
8990e307 |
608 | IoIFP(io) = fp; |
b931b1d9 |
609 | |
684bef36 |
610 | IoFLAGS(io) &= ~IOf_NOLINE; |
bf38876a |
611 | if (writing) { |
50952442 |
612 | if (IoTYPE(io) == IoTYPE_SOCKET |
ee518936 |
613 | || (IoTYPE(io) == IoTYPE_WRONLY && S_ISCHR(PL_statbuf.st_mode)) ) { |
b931b1d9 |
614 | mode[0] = 'w'; |
1141d9f8 |
615 | if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,mode,PerlIO_fileno(fp),0,0,NULL,num_svs,svp))) { |
760ac839 |
616 | PerlIO_close(fp); |
8990e307 |
617 | IoIFP(io) = Nullfp; |
6e21c824 |
618 | goto say_false; |
fe14fcc3 |
619 | } |
1462b684 |
620 | } |
621 | else |
8990e307 |
622 | IoOFP(io) = fp; |
bf38876a |
623 | } |
a687059c |
624 | return TRUE; |
6e21c824 |
625 | |
626 | say_false: |
8990e307 |
627 | IoIFP(io) = saveifp; |
628 | IoOFP(io) = saveofp; |
629 | IoTYPE(io) = savetype; |
6e21c824 |
630 | return FALSE; |
a687059c |
631 | } |
632 | |
760ac839 |
633 | PerlIO * |
864dbfa3 |
634 | Perl_nextargv(pTHX_ register GV *gv) |
a687059c |
635 | { |
79072805 |
636 | register SV *sv; |
99b89507 |
637 | #ifndef FLEXFILENAMES |
c623bd54 |
638 | int filedev; |
639 | int fileino; |
99b89507 |
640 | #endif |
761237fe |
641 | Uid_t fileuid; |
642 | Gid_t filegid; |
18708f5a |
643 | IO *io = GvIOp(gv); |
fe14fcc3 |
644 | |
3280af22 |
645 | if (!PL_argvoutgv) |
646 | PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); |
18708f5a |
647 | if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) { |
648 | IoFLAGS(io) &= ~IOf_START; |
7a1c5554 |
649 | if (PL_inplace) { |
650 | if (!PL_argvout_stack) |
651 | PL_argvout_stack = newAV(); |
18708f5a |
652 | av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv)); |
7a1c5554 |
653 | } |
18708f5a |
654 | } |
3280af22 |
655 | if (PL_filemode & (S_ISUID|S_ISGID)) { |
656 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ |
fe14fcc3 |
657 | #ifdef HAS_FCHMOD |
3280af22 |
658 | (void)fchmod(PL_lastfd,PL_filemode); |
fe14fcc3 |
659 | #else |
b28d0864 |
660 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
fe14fcc3 |
661 | #endif |
662 | } |
3280af22 |
663 | PL_filemode = 0; |
79072805 |
664 | while (av_len(GvAV(gv)) >= 0) { |
85aff577 |
665 | STRLEN oldlen; |
79072805 |
666 | sv = av_shift(GvAV(gv)); |
8990e307 |
667 | SAVEFREESV(sv); |
79072805 |
668 | sv_setsv(GvSV(gv),sv); |
669 | SvSETMAGIC(GvSV(gv)); |
3280af22 |
670 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
9d116dd7 |
671 | if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) { |
3280af22 |
672 | if (PL_inplace) { |
79072805 |
673 | TAINT_PROPER("inplace open"); |
3280af22 |
674 | if (oldlen == 1 && *PL_oldname == '-') { |
4633a7c4 |
675 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a0d0e21e |
676 | return IoIFP(GvIOp(gv)); |
c623bd54 |
677 | } |
99b89507 |
678 | #ifndef FLEXFILENAMES |
b28d0864 |
679 | filedev = PL_statbuf.st_dev; |
680 | fileino = PL_statbuf.st_ino; |
99b89507 |
681 | #endif |
3280af22 |
682 | PL_filemode = PL_statbuf.st_mode; |
683 | fileuid = PL_statbuf.st_uid; |
684 | filegid = PL_statbuf.st_gid; |
685 | if (!S_ISREG(PL_filemode)) { |
0453d815 |
686 | if (ckWARN_d(WARN_INPLACE)) |
687 | Perl_warner(aTHX_ WARN_INPLACE, |
688 | "Can't do inplace edit: %s is not a regular file", |
689 | PL_oldname ); |
79072805 |
690 | do_close(gv,FALSE); |
c623bd54 |
691 | continue; |
692 | } |
3280af22 |
693 | if (*PL_inplace) { |
694 | char *star = strchr(PL_inplace, '*'); |
2d259d92 |
695 | if (star) { |
3280af22 |
696 | char *begin = PL_inplace; |
2d259d92 |
697 | sv_setpvn(sv, "", 0); |
698 | do { |
699 | sv_catpvn(sv, begin, star - begin); |
3280af22 |
700 | sv_catpvn(sv, PL_oldname, oldlen); |
2d259d92 |
701 | begin = ++star; |
702 | } while ((star = strchr(begin, '*'))); |
3d66d7bb |
703 | if (*begin) |
704 | sv_catpv(sv,begin); |
2d259d92 |
705 | } |
706 | else { |
3280af22 |
707 | sv_catpv(sv,PL_inplace); |
2d259d92 |
708 | } |
c623bd54 |
709 | #ifndef FLEXFILENAMES |
b28d0864 |
710 | if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0 |
711 | && PL_statbuf.st_dev == filedev |
712 | && PL_statbuf.st_ino == fileino |
39e571d4 |
713 | #ifdef DJGPP |
714 | || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0 |
715 | #endif |
f248d071 |
716 | ) |
717 | { |
718 | if (ckWARN_d(WARN_INPLACE)) |
719 | Perl_warner(aTHX_ WARN_INPLACE, |
720 | "Can't do inplace edit: %s would not be unique", |
721 | SvPVX(sv)); |
79072805 |
722 | do_close(gv,FALSE); |
c623bd54 |
723 | continue; |
724 | } |
725 | #endif |
fe14fcc3 |
726 | #ifdef HAS_RENAME |
d308986b |
727 | #if !defined(DOSISH) && !defined(__CYGWIN__) |
3280af22 |
728 | if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 |
729 | if (ckWARN_d(WARN_INPLACE)) |
a1d180c4 |
730 | Perl_warner(aTHX_ WARN_INPLACE, |
0453d815 |
731 | "Can't rename %s to %s: %s, skipping file", |
732 | PL_oldname, SvPVX(sv), Strerror(errno) ); |
79072805 |
733 | do_close(gv,FALSE); |
c623bd54 |
734 | continue; |
735 | } |
a687059c |
736 | #else |
79072805 |
737 | do_close(gv,FALSE); |
3028581b |
738 | (void)PerlLIO_unlink(SvPVX(sv)); |
b28d0864 |
739 | (void)PerlLIO_rename(PL_oldname,SvPVX(sv)); |
9d116dd7 |
740 | do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp); |
55497cff |
741 | #endif /* DOSISH */ |
ff8e2863 |
742 | #else |
463ee0b2 |
743 | (void)UNLINK(SvPVX(sv)); |
b28d0864 |
744 | if (link(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 |
745 | if (ckWARN_d(WARN_INPLACE)) |
746 | Perl_warner(aTHX_ WARN_INPLACE, |
747 | "Can't rename %s to %s: %s, skipping file", |
748 | PL_oldname, SvPVX(sv), Strerror(errno) ); |
79072805 |
749 | do_close(gv,FALSE); |
c623bd54 |
750 | continue; |
751 | } |
b28d0864 |
752 | (void)UNLINK(PL_oldname); |
a687059c |
753 | #endif |
754 | } |
755 | else { |
a8c18271 |
756 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 |
757 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 |
758 | if (UNLINK(PL_oldname) < 0) { |
0453d815 |
759 | if (ckWARN_d(WARN_INPLACE)) |
760 | Perl_warner(aTHX_ WARN_INPLACE, |
761 | "Can't remove %s: %s, skipping file", |
762 | PL_oldname, Strerror(errno) ); |
79072805 |
763 | do_close(gv,FALSE); |
fe14fcc3 |
764 | continue; |
765 | } |
edc7bc49 |
766 | # endif |
ff8e2863 |
767 | #else |
cea2e8a9 |
768 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 |
769 | #endif |
a687059c |
770 | } |
771 | |
3280af22 |
772 | sv_setpvn(sv,">",!PL_inplace); |
773 | sv_catpvn(sv,PL_oldname,oldlen); |
748a9306 |
774 | SETERRNO(0,0); /* in case sprintf set errno */ |
4119ab01 |
775 | #ifdef VMS |
776 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
18708f5a |
777 | O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) |
4119ab01 |
778 | #else |
3280af22 |
779 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
18708f5a |
780 | O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) |
4119ab01 |
781 | #endif |
18708f5a |
782 | { |
0453d815 |
783 | if (ckWARN_d(WARN_INPLACE)) |
784 | Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s", |
785 | PL_oldname, Strerror(errno) ); |
79072805 |
786 | do_close(gv,FALSE); |
fe14fcc3 |
787 | continue; |
788 | } |
3280af22 |
789 | setdefout(PL_argvoutgv); |
790 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); |
791 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); |
fe14fcc3 |
792 | #ifdef HAS_FCHMOD |
3280af22 |
793 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c |
794 | #else |
3e3baf6d |
795 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
796 | /* Borland runtime creates a readonly file! */ |
b28d0864 |
797 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
3e3baf6d |
798 | # endif |
a687059c |
799 | #endif |
3280af22 |
800 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
fe14fcc3 |
801 | #ifdef HAS_FCHOWN |
3280af22 |
802 | (void)fchown(PL_lastfd,fileuid,filegid); |
a687059c |
803 | #else |
fe14fcc3 |
804 | #ifdef HAS_CHOWN |
b28d0864 |
805 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
a687059c |
806 | #endif |
b1248f16 |
807 | #endif |
fe14fcc3 |
808 | } |
a687059c |
809 | } |
a0d0e21e |
810 | return IoIFP(GvIOp(gv)); |
a687059c |
811 | } |
4d61ec05 |
812 | else { |
4d61ec05 |
813 | if (ckWARN_d(WARN_INPLACE)) { |
6af84f9f |
814 | int eno = errno; |
815 | if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0 |
816 | && !S_ISREG(PL_statbuf.st_mode)) |
817 | { |
4d61ec05 |
818 | Perl_warner(aTHX_ WARN_INPLACE, |
819 | "Can't do inplace edit: %s is not a regular file", |
9a7dcd9c |
820 | PL_oldname); |
6af84f9f |
821 | } |
4d61ec05 |
822 | else |
9a7dcd9c |
823 | Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s", |
6af84f9f |
824 | PL_oldname, Strerror(eno)); |
4d61ec05 |
825 | } |
826 | } |
a687059c |
827 | } |
18708f5a |
828 | if (io && (IoFLAGS(io) & IOf_ARGV)) |
829 | IoFLAGS(io) |= IOf_START; |
3280af22 |
830 | if (PL_inplace) { |
831 | (void)do_close(PL_argvoutgv,FALSE); |
7a1c5554 |
832 | if (io && (IoFLAGS(io) & IOf_ARGV) |
833 | && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0) |
834 | { |
18708f5a |
835 | GV *oldout = (GV*)av_pop(PL_argvout_stack); |
836 | setdefout(oldout); |
837 | SvREFCNT_dec(oldout); |
838 | return Nullfp; |
839 | } |
4633a7c4 |
840 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c |
841 | } |
842 | return Nullfp; |
843 | } |
844 | |
fe14fcc3 |
845 | #ifdef HAS_PIPE |
afd9f252 |
846 | void |
864dbfa3 |
847 | Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv) |
afd9f252 |
848 | { |
79072805 |
849 | register IO *rstio; |
850 | register IO *wstio; |
afd9f252 |
851 | int fd[2]; |
852 | |
79072805 |
853 | if (!rgv) |
afd9f252 |
854 | goto badexit; |
79072805 |
855 | if (!wgv) |
afd9f252 |
856 | goto badexit; |
857 | |
a0d0e21e |
858 | rstio = GvIOn(rgv); |
859 | wstio = GvIOn(wgv); |
afd9f252 |
860 | |
a0d0e21e |
861 | if (IoIFP(rstio)) |
79072805 |
862 | do_close(rgv,FALSE); |
a0d0e21e |
863 | if (IoIFP(wstio)) |
79072805 |
864 | do_close(wgv,FALSE); |
afd9f252 |
865 | |
3028581b |
866 | if (PerlProc_pipe(fd) < 0) |
afd9f252 |
867 | goto badexit; |
760ac839 |
868 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); |
869 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); |
8990e307 |
870 | IoIFP(wstio) = IoOFP(wstio); |
50952442 |
871 | IoTYPE(rstio) = IoTYPE_RDONLY; |
872 | IoTYPE(wstio) = IoTYPE_WRONLY; |
8990e307 |
873 | if (!IoIFP(rstio) || !IoOFP(wstio)) { |
760ac839 |
874 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
3028581b |
875 | else PerlLIO_close(fd[0]); |
760ac839 |
876 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
3028581b |
877 | else PerlLIO_close(fd[1]); |
fe14fcc3 |
878 | goto badexit; |
879 | } |
afd9f252 |
880 | |
3280af22 |
881 | sv_setsv(sv,&PL_sv_yes); |
afd9f252 |
882 | return; |
883 | |
884 | badexit: |
3280af22 |
885 | sv_setsv(sv,&PL_sv_undef); |
afd9f252 |
886 | return; |
887 | } |
b1248f16 |
888 | #endif |
afd9f252 |
889 | |
517844ec |
890 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c |
891 | bool |
864dbfa3 |
892 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c |
893 | { |
1193dd27 |
894 | bool retval; |
895 | IO *io; |
a687059c |
896 | |
79072805 |
897 | if (!gv) |
3280af22 |
898 | gv = PL_argvgv; |
a0d0e21e |
899 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
1d2dff63 |
900 | if (not_implicit) |
901 | SETERRNO(EBADF,SS$_IVCHAN); |
c2ab57d4 |
902 | return FALSE; |
99b89507 |
903 | } |
79072805 |
904 | io = GvIO(gv); |
905 | if (!io) { /* never opened */ |
1d2dff63 |
906 | if (not_implicit) { |
2dd78f96 |
907 | if (ckWARN(WARN_UNOPENED)) /* no check for closed here */ |
908 | report_evil_fh(gv, io, PL_op->op_type); |
1d2dff63 |
909 | SETERRNO(EBADF,SS$_IVCHAN); |
910 | } |
a687059c |
911 | return FALSE; |
912 | } |
f2b5be74 |
913 | retval = io_close(io, not_implicit); |
517844ec |
914 | if (not_implicit) { |
1193dd27 |
915 | IoLINES(io) = 0; |
916 | IoPAGE(io) = 0; |
917 | IoLINES_LEFT(io) = IoPAGE_LEN(io); |
918 | } |
50952442 |
919 | IoTYPE(io) = IoTYPE_CLOSED; |
1193dd27 |
920 | return retval; |
921 | } |
922 | |
923 | bool |
f2b5be74 |
924 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
1193dd27 |
925 | { |
926 | bool retval = FALSE; |
927 | int status; |
928 | |
8990e307 |
929 | if (IoIFP(io)) { |
50952442 |
930 | if (IoTYPE(io) == IoTYPE_PIPE) { |
3028581b |
931 | status = PerlProc_pclose(IoIFP(io)); |
f2b5be74 |
932 | if (not_implicit) { |
933 | STATUS_NATIVE_SET(status); |
934 | retval = (STATUS_POSIX == 0); |
935 | } |
936 | else { |
937 | retval = (status != -1); |
938 | } |
a687059c |
939 | } |
50952442 |
940 | else if (IoTYPE(io) == IoTYPE_STD) |
a687059c |
941 | retval = TRUE; |
942 | else { |
8990e307 |
943 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 |
944 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
945 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
946 | } |
947 | else |
760ac839 |
948 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c |
949 | } |
8990e307 |
950 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 |
951 | } |
f2b5be74 |
952 | else if (not_implicit) { |
20408e3c |
953 | SETERRNO(EBADF,SS$_IVCHAN); |
954 | } |
1193dd27 |
955 | |
a687059c |
956 | return retval; |
957 | } |
958 | |
959 | bool |
864dbfa3 |
960 | Perl_do_eof(pTHX_ GV *gv) |
a687059c |
961 | { |
79072805 |
962 | register IO *io; |
a687059c |
963 | int ch; |
964 | |
79072805 |
965 | io = GvIO(gv); |
a687059c |
966 | |
79072805 |
967 | if (!io) |
a687059c |
968 | return TRUE; |
a00b5bd3 |
969 | else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY)) |
af8c498a |
970 | { |
2dd78f96 |
971 | /* integrate to report_evil_fh()? */ |
a1d180c4 |
972 | char *name = NULL; |
2dd78f96 |
973 | if (isGV(gv)) { |
974 | SV* sv = sv_newmortal(); |
975 | gv_efullname4(sv, gv, Nullch, FALSE); |
976 | name = SvPV_nolen(sv); |
977 | } |
978 | if (name && *name) |
979 | Perl_warner(aTHX_ WARN_IO, |
980 | "Filehandle %s opened only for output", name); |
981 | else |
982 | Perl_warner(aTHX_ WARN_IO, |
983 | "Filehandle opened only for output"); |
af8c498a |
984 | } |
a687059c |
985 | |
8990e307 |
986 | while (IoIFP(io)) { |
a687059c |
987 | |
760ac839 |
988 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
a20bf0c3 |
989 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
760ac839 |
990 | return FALSE; /* this is the most usual case */ |
991 | } |
a687059c |
992 | |
760ac839 |
993 | ch = PerlIO_getc(IoIFP(io)); |
a687059c |
994 | if (ch != EOF) { |
760ac839 |
995 | (void)PerlIO_ungetc(IoIFP(io),ch); |
a687059c |
996 | return FALSE; |
997 | } |
fab3f3a7 |
998 | |
760ac839 |
999 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
a20bf0c3 |
1000 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
1001 | PerlIO_set_cnt(IoIFP(io),-1); |
760ac839 |
1002 | } |
533c011a |
1003 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
3280af22 |
1004 | if (!nextargv(PL_argvgv)) /* get another fp handy */ |
a687059c |
1005 | return TRUE; |
1006 | } |
1007 | else |
1008 | return TRUE; /* normal fp, definitely end of file */ |
1009 | } |
1010 | return TRUE; |
1011 | } |
1012 | |
5ff3f7a4 |
1013 | Off_t |
864dbfa3 |
1014 | Perl_do_tell(pTHX_ GV *gv) |
a687059c |
1015 | { |
9c5ffd7c |
1016 | register IO *io = 0; |
96e4d5b1 |
1017 | register PerlIO *fp; |
a687059c |
1018 | |
96e4d5b1 |
1019 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
1020 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 |
1021 | if (PerlIO_eof(fp)) |
1022 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
1023 | #endif |
8903cb82 |
1024 | return PerlIO_tell(fp); |
96e4d5b1 |
1025 | } |
411caa50 |
1026 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1027 | report_evil_fh(gv, io, PL_op->op_type); |
748a9306 |
1028 | SETERRNO(EBADF,RMS$_IFI); |
5ff3f7a4 |
1029 | return (Off_t)-1; |
a687059c |
1030 | } |
1031 | |
1032 | bool |
864dbfa3 |
1033 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c |
1034 | { |
9c5ffd7c |
1035 | register IO *io = 0; |
137443ea |
1036 | register PerlIO *fp; |
a687059c |
1037 | |
137443ea |
1038 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
1039 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea |
1040 | if (PerlIO_eof(fp)) |
1041 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
1042 | #endif |
8903cb82 |
1043 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea |
1044 | } |
411caa50 |
1045 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1046 | report_evil_fh(gv, io, PL_op->op_type); |
748a9306 |
1047 | SETERRNO(EBADF,RMS$_IFI); |
a687059c |
1048 | return FALSE; |
1049 | } |
1050 | |
97cc44eb |
1051 | Off_t |
864dbfa3 |
1052 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 |
1053 | { |
9c5ffd7c |
1054 | register IO *io = 0; |
8903cb82 |
1055 | register PerlIO *fp; |
1056 | |
1057 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) |
3028581b |
1058 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
411caa50 |
1059 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1060 | report_evil_fh(gv, io, PL_op->op_type); |
8903cb82 |
1061 | SETERRNO(EBADF,RMS$_IFI); |
d9b3e12d |
1062 | return (Off_t)-1; |
8903cb82 |
1063 | } |
1064 | |
6ff81951 |
1065 | int |
16fe6d59 |
1066 | Perl_mode_from_discipline(pTHX_ SV *discp) |
1067 | { |
1068 | int mode = O_BINARY; |
1069 | if (discp) { |
1070 | STRLEN len; |
1071 | char *s = SvPV(discp,len); |
1072 | while (*s) { |
1073 | if (*s == ':') { |
1074 | switch (s[1]) { |
1075 | case 'r': |
1076 | if (len > 3 && strnEQ(s+1, "raw", 3) |
1077 | && (!s[4] || s[4] == ':' || isSPACE(s[4]))) |
1078 | { |
1079 | mode = O_BINARY; |
1080 | s += 4; |
1081 | len -= 4; |
1082 | break; |
1083 | } |
1084 | /* FALL THROUGH */ |
1085 | case 'c': |
1086 | if (len > 4 && strnEQ(s+1, "crlf", 4) |
1087 | && (!s[5] || s[5] == ':' || isSPACE(s[5]))) |
1088 | { |
1089 | mode = O_TEXT; |
1090 | s += 5; |
1091 | len -= 5; |
1092 | break; |
1093 | } |
1094 | /* FALL THROUGH */ |
1095 | default: |
1096 | goto fail_discipline; |
1097 | } |
1098 | } |
1099 | else if (isSPACE(*s)) { |
1100 | ++s; |
1101 | --len; |
1102 | } |
1103 | else { |
1104 | char *end; |
1105 | fail_discipline: |
1106 | end = strchr(s+1, ':'); |
1107 | if (!end) |
1108 | end = s+len; |
60382766 |
1109 | #ifndef PERLIO_LAYERS |
16fe6d59 |
1110 | Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s); |
60382766 |
1111 | #else |
1112 | s = end; |
1113 | #endif |
16fe6d59 |
1114 | } |
1115 | } |
1116 | } |
1117 | return mode; |
1118 | } |
1119 | |
1120 | int |
1121 | Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode) |
6ff81951 |
1122 | { |
60382766 |
1123 | /* The old body of this is now in non-LAYER part of perlio.c |
1124 | * This is a stub for any XS code which might have been calling it. |
1125 | */ |
1126 | char *name = (O_BINARY != O_TEXT && !(mode & O_BINARY)) ? ":crlf" : ":raw"; |
1127 | return PerlIO_binmode(aTHX_ fp, iotype, mode, name); |
6ff81951 |
1128 | } |
1129 | |
a0d0e21e |
1130 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 |
1131 | /* code courtesy of William Kucharski */ |
fe14fcc3 |
1132 | #define HAS_CHSIZE |
6eb13c3b |
1133 | |
517844ec |
1134 | I32 my_chsize(fd, length) |
79072805 |
1135 | I32 fd; /* file descriptor */ |
85e6fe83 |
1136 | Off_t length; /* length to set file to */ |
6eb13c3b |
1137 | { |
6eb13c3b |
1138 | struct flock fl; |
1139 | struct stat filebuf; |
1140 | |
3028581b |
1141 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b |
1142 | return -1; |
1143 | |
1144 | if (filebuf.st_size < length) { |
1145 | |
1146 | /* extend file length */ |
1147 | |
3028581b |
1148 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b |
1149 | return -1; |
1150 | |
1151 | /* write a "0" byte */ |
1152 | |
3028581b |
1153 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b |
1154 | return -1; |
1155 | } |
1156 | else { |
1157 | /* truncate length */ |
1158 | |
1159 | fl.l_whence = 0; |
1160 | fl.l_len = 0; |
1161 | fl.l_start = length; |
a0d0e21e |
1162 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b |
1163 | |
1164 | /* |
a0d0e21e |
1165 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b |
1166 | * fcntl(2), which truncates the file so that it ends at the |
1167 | * position indicated by fl.l_start. |
1168 | * |
1169 | * Will minor miracles never cease? |
1170 | */ |
1171 | |
a0d0e21e |
1172 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b |
1173 | return -1; |
1174 | |
1175 | } |
1176 | |
1177 | return 0; |
1178 | } |
a0d0e21e |
1179 | #endif /* F_FREESP */ |
ff8e2863 |
1180 | |
a687059c |
1181 | bool |
864dbfa3 |
1182 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
a687059c |
1183 | { |
1184 | register char *tmps; |
463ee0b2 |
1185 | STRLEN len; |
a687059c |
1186 | |
79072805 |
1187 | /* assuming fp is checked earlier */ |
1188 | if (!sv) |
1189 | return TRUE; |
3280af22 |
1190 | if (PL_ofmt) { |
8990e307 |
1191 | if (SvGMAGICAL(sv)) |
79072805 |
1192 | mg_get(sv); |
463ee0b2 |
1193 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
65202027 |
1194 | PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv)); |
760ac839 |
1195 | return !PerlIO_error(fp); |
79072805 |
1196 | } |
463ee0b2 |
1197 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 |
1198 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
3280af22 |
1199 | PerlIO_printf(fp, PL_ofmt, SvNVX(sv)); |
760ac839 |
1200 | return !PerlIO_error(fp); |
79072805 |
1201 | } |
a687059c |
1202 | } |
79072805 |
1203 | switch (SvTYPE(sv)) { |
1204 | case SVt_NULL: |
411caa50 |
1205 | if (ckWARN(WARN_UNINITIALIZED)) |
1206 | report_uninit(); |
ff8e2863 |
1207 | return TRUE; |
79072805 |
1208 | case SVt_IV: |
a0d0e21e |
1209 | if (SvIOK(sv)) { |
1210 | if (SvGMAGICAL(sv)) |
1211 | mg_get(sv); |
cf2093f6 |
1212 | if (SvIsUV(sv)) |
57def98f |
1213 | PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 |
1214 | else |
57def98f |
1215 | PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv)); |
760ac839 |
1216 | return !PerlIO_error(fp); |
a0d0e21e |
1217 | } |
1218 | /* FALL THROUGH */ |
79072805 |
1219 | default: |
7d59b7e4 |
1220 | if (PerlIO_isutf8(fp)) { |
f9a63242 |
1221 | if (!SvUTF8(sv)) |
1222 | sv_utf8_upgrade(sv = sv_mortalcopy(sv)); |
7d59b7e4 |
1223 | } |
ae798467 |
1224 | else if (DO_UTF8(sv)) { |
2da16753 |
1225 | if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE) |
1226 | && ckWARN(WARN_UTF8)) |
1227 | { |
ae798467 |
1228 | Perl_warner(aTHX_ WARN_UTF8, "Wide character in print"); |
1229 | } |
1230 | } |
f9a63242 |
1231 | tmps = SvPV(sv, len); |
79072805 |
1232 | break; |
ff8e2863 |
1233 | } |
94e4c244 |
1234 | /* To detect whether the process is about to overstep its |
1235 | * filesize limit we would need getrlimit(). We could then |
1236 | * also transparently raise the limit with setrlimit() -- |
1237 | * but only until the system hard limit/the filesystem limit, |
c5dd3cdd |
1238 | * at which we would get EPERM. Note that when using buffered |
1239 | * io the write failure can be delayed until the flush/close. --jhi */ |
a21ac455 |
1240 | if (len && (PerlIO_write(fp,tmps,len) == 0)) |
a687059c |
1241 | return FALSE; |
760ac839 |
1242 | return !PerlIO_error(fp); |
a687059c |
1243 | } |
1244 | |
79072805 |
1245 | I32 |
cea2e8a9 |
1246 | Perl_my_stat(pTHX) |
a687059c |
1247 | { |
39644a26 |
1248 | dSP; |
79072805 |
1249 | IO *io; |
2dd78f96 |
1250 | GV* gv; |
79072805 |
1251 | |
533c011a |
1252 | if (PL_op->op_flags & OPf_REF) { |
924508f0 |
1253 | EXTEND(SP,1); |
2dd78f96 |
1254 | gv = cGVOP_gv; |
748a9306 |
1255 | do_fstat: |
2dd78f96 |
1256 | io = GvIO(gv); |
8990e307 |
1257 | if (io && IoIFP(io)) { |
2dd78f96 |
1258 | PL_statgv = gv; |
3280af22 |
1259 | sv_setpv(PL_statname,""); |
1260 | PL_laststype = OP_STAT; |
1261 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); |
a687059c |
1262 | } |
1263 | else { |
2dd78f96 |
1264 | if (gv == PL_defgv) |
3280af22 |
1265 | return PL_laststatval; |
2dd78f96 |
1266 | if (ckWARN2(WARN_UNOPENED,WARN_CLOSED)) |
1267 | report_evil_fh(gv, io, PL_op->op_type); |
3280af22 |
1268 | PL_statgv = Nullgv; |
1269 | sv_setpv(PL_statname,""); |
1270 | return (PL_laststatval = -1); |
a687059c |
1271 | } |
1272 | } |
1273 | else { |
748a9306 |
1274 | SV* sv = POPs; |
4b74e3fb |
1275 | char *s; |
2d8e6c8d |
1276 | STRLEN n_a; |
79072805 |
1277 | PUTBACK; |
748a9306 |
1278 | if (SvTYPE(sv) == SVt_PVGV) { |
2dd78f96 |
1279 | gv = (GV*)sv; |
748a9306 |
1280 | goto do_fstat; |
1281 | } |
1282 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { |
2dd78f96 |
1283 | gv = (GV*)SvRV(sv); |
748a9306 |
1284 | goto do_fstat; |
1285 | } |
1286 | |
2d8e6c8d |
1287 | s = SvPV(sv, n_a); |
3280af22 |
1288 | PL_statgv = Nullgv; |
1289 | sv_setpv(PL_statname, s); |
1290 | PL_laststype = OP_STAT; |
1291 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); |
599cee73 |
1292 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
cea2e8a9 |
1293 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat"); |
3280af22 |
1294 | return PL_laststatval; |
a687059c |
1295 | } |
1296 | } |
1297 | |
79072805 |
1298 | I32 |
cea2e8a9 |
1299 | Perl_my_lstat(pTHX) |
c623bd54 |
1300 | { |
39644a26 |
1301 | dSP; |
79072805 |
1302 | SV *sv; |
2d8e6c8d |
1303 | STRLEN n_a; |
533c011a |
1304 | if (PL_op->op_flags & OPf_REF) { |
924508f0 |
1305 | EXTEND(SP,1); |
638eceb6 |
1306 | if (cGVOP_gv == PL_defgv) { |
3280af22 |
1307 | if (PL_laststype != OP_LSTAT) |
cea2e8a9 |
1308 | Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat"); |
3280af22 |
1309 | return PL_laststatval; |
fe14fcc3 |
1310 | } |
cea2e8a9 |
1311 | Perl_croak(aTHX_ "You can't use -l on a filehandle"); |
fe14fcc3 |
1312 | } |
c623bd54 |
1313 | |
3280af22 |
1314 | PL_laststype = OP_LSTAT; |
1315 | PL_statgv = Nullgv; |
79072805 |
1316 | sv = POPs; |
1317 | PUTBACK; |
2d8e6c8d |
1318 | sv_setpv(PL_statname,SvPV(sv, n_a)); |
2d8e6c8d |
1319 | PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache); |
2d8e6c8d |
1320 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n')) |
cea2e8a9 |
1321 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat"); |
3280af22 |
1322 | return PL_laststatval; |
c623bd54 |
1323 | } |
1324 | |
a687059c |
1325 | bool |
864dbfa3 |
1326 | Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp) |
a687059c |
1327 | { |
d5a9bfb0 |
1328 | return do_aexec5(really, mark, sp, 0, 0); |
1329 | } |
1330 | |
1331 | bool |
2aa1486d |
1332 | Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp, |
1333 | int fd, int do_report) |
d5a9bfb0 |
1334 | { |
cd39f2b6 |
1335 | #ifdef MACOS_TRADITIONAL |
1336 | Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system"); |
1337 | #else |
a687059c |
1338 | register char **a; |
9c5ffd7c |
1339 | char *tmps = Nullch; |
2d8e6c8d |
1340 | STRLEN n_a; |
a687059c |
1341 | |
79072805 |
1342 | if (sp > mark) { |
3280af22 |
1343 | New(401,PL_Argv, sp - mark + 1, char*); |
1344 | a = PL_Argv; |
79072805 |
1345 | while (++mark <= sp) { |
1346 | if (*mark) |
2d8e6c8d |
1347 | *a++ = SvPVx(*mark, n_a); |
a687059c |
1348 | else |
1349 | *a++ = ""; |
1350 | } |
1351 | *a = Nullch; |
91b2752f |
1352 | if (really) |
1353 | tmps = SvPV(really, n_a); |
1354 | if ((!really && *PL_Argv[0] != '/') || |
1355 | (really && *tmps != '/')) /* will execvp use PATH? */ |
79072805 |
1356 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
91b2752f |
1357 | if (really && *tmps) |
b4748376 |
1358 | PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv)); |
a687059c |
1359 | else |
b4748376 |
1360 | PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv)); |
599cee73 |
1361 | if (ckWARN(WARN_EXEC)) |
a1d180c4 |
1362 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
91b2752f |
1363 | (really ? tmps : PL_Argv[0]), Strerror(errno)); |
d5a9bfb0 |
1364 | if (do_report) { |
1365 | int e = errno; |
1366 | |
1367 | PerlLIO_write(fd, (void*)&e, sizeof(int)); |
1368 | PerlLIO_close(fd); |
1369 | } |
a687059c |
1370 | } |
bee1dbe2 |
1371 | do_execfree(); |
cd39f2b6 |
1372 | #endif |
a687059c |
1373 | return FALSE; |
1374 | } |
1375 | |
fe14fcc3 |
1376 | void |
864dbfa3 |
1377 | Perl_do_execfree(pTHX) |
ff8e2863 |
1378 | { |
3280af22 |
1379 | if (PL_Argv) { |
1380 | Safefree(PL_Argv); |
1381 | PL_Argv = Null(char **); |
ff8e2863 |
1382 | } |
3280af22 |
1383 | if (PL_Cmd) { |
1384 | Safefree(PL_Cmd); |
1385 | PL_Cmd = Nullch; |
ff8e2863 |
1386 | } |
1387 | } |
1388 | |
cd39f2b6 |
1389 | #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) |
760ac839 |
1390 | |
a687059c |
1391 | bool |
864dbfa3 |
1392 | Perl_do_exec(pTHX_ char *cmd) |
a687059c |
1393 | { |
e446cec8 |
1394 | return do_exec3(cmd,0,0); |
1395 | } |
1396 | |
1397 | bool |
864dbfa3 |
1398 | Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report) |
e446cec8 |
1399 | { |
a687059c |
1400 | register char **a; |
1401 | register char *s; |
a687059c |
1402 | |
748a9306 |
1403 | while (*cmd && isSPACE(*cmd)) |
1404 | cmd++; |
1405 | |
a687059c |
1406 | /* save an extra exec if possible */ |
1407 | |
bf38876a |
1408 | #ifdef CSH |
d05c1ba0 |
1409 | { |
1410 | char flags[10]; |
1411 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && |
1412 | strnEQ(cmd+PL_cshlen," -c",3)) { |
1413 | strcpy(flags,"-c"); |
1414 | s = cmd+PL_cshlen+3; |
1415 | if (*s == 'f') { |
1416 | s++; |
1417 | strcat(flags,"f"); |
1418 | } |
1419 | if (*s == ' ') |
1420 | s++; |
1421 | if (*s++ == '\'') { |
1422 | char *ncmd = s; |
1423 | |
1424 | while (*s) |
1425 | s++; |
1426 | if (s[-1] == '\n') |
1427 | *--s = '\0'; |
1428 | if (s[-1] == '\'') { |
1429 | *--s = '\0'; |
1430 | PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0); |
1431 | *s = '\''; |
1432 | return FALSE; |
1433 | } |
1434 | } |
a687059c |
1435 | } |
1436 | } |
bf38876a |
1437 | #endif /* CSH */ |
a687059c |
1438 | |
1439 | /* see if there are shell metacharacters in it */ |
1440 | |
748a9306 |
1441 | if (*cmd == '.' && isSPACE(cmd[1])) |
1442 | goto doshell; |
1443 | |
1444 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) |
1445 | goto doshell; |
1446 | |
c170e444 |
1447 | for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */ |
63f2c1e1 |
1448 | if (*s == '=') |
1449 | goto doshell; |
748a9306 |
1450 | |
a687059c |
1451 | for (s = cmd; *s; s++) { |
d05c1ba0 |
1452 | if (*s != ' ' && !isALPHA(*s) && |
1453 | strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
a687059c |
1454 | if (*s == '\n' && !s[1]) { |
1455 | *s = '\0'; |
1456 | break; |
1457 | } |
603a98b0 |
1458 | /* handle the 2>&1 construct at the end */ |
1459 | if (*s == '>' && s[1] == '&' && s[2] == '1' |
1460 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) |
1461 | && (!s[3] || isSPACE(s[3]))) |
1462 | { |
1463 | char *t = s + 3; |
1464 | |
1465 | while (*t && isSPACE(*t)) |
1466 | ++t; |
1467 | if (!*t && (dup2(1,2) != -1)) { |
1468 | s[-2] = '\0'; |
1469 | break; |
1470 | } |
1471 | } |
a687059c |
1472 | doshell: |
3280af22 |
1473 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0); |
a687059c |
1474 | return FALSE; |
1475 | } |
1476 | } |
748a9306 |
1477 | |
3280af22 |
1478 | New(402,PL_Argv, (s - cmd) / 2 + 2, char*); |
1479 | PL_Cmd = savepvn(cmd, s-cmd); |
1480 | a = PL_Argv; |
1481 | for (s = PL_Cmd; *s;) { |
99b89507 |
1482 | while (*s && isSPACE(*s)) s++; |
a687059c |
1483 | if (*s) |
1484 | *(a++) = s; |
99b89507 |
1485 | while (*s && !isSPACE(*s)) s++; |
a687059c |
1486 | if (*s) |
1487 | *s++ = '\0'; |
1488 | } |
1489 | *a = Nullch; |
3280af22 |
1490 | if (PL_Argv[0]) { |
1491 | PerlProc_execvp(PL_Argv[0],PL_Argv); |
b1248f16 |
1492 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 |
1493 | do_execfree(); |
a687059c |
1494 | goto doshell; |
b1248f16 |
1495 | } |
d008e5eb |
1496 | { |
e446cec8 |
1497 | int e = errno; |
1498 | |
d008e5eb |
1499 | if (ckWARN(WARN_EXEC)) |
a1d180c4 |
1500 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
d008e5eb |
1501 | PL_Argv[0], Strerror(errno)); |
e446cec8 |
1502 | if (do_report) { |
1503 | PerlLIO_write(fd, (void*)&e, sizeof(int)); |
1504 | PerlLIO_close(fd); |
1505 | } |
d008e5eb |
1506 | } |
a687059c |
1507 | } |
ff8e2863 |
1508 | do_execfree(); |
a687059c |
1509 | return FALSE; |
1510 | } |
1511 | |
6890e559 |
1512 | #endif /* OS2 || WIN32 */ |
760ac839 |
1513 | |
79072805 |
1514 | I32 |
864dbfa3 |
1515 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
a687059c |
1516 | { |
79072805 |
1517 | register I32 val; |
1518 | register I32 val2; |
1519 | register I32 tot = 0; |
20408e3c |
1520 | char *what; |
a687059c |
1521 | char *s; |
79072805 |
1522 | SV **oldmark = mark; |
2d8e6c8d |
1523 | STRLEN n_a; |
a687059c |
1524 | |
20408e3c |
1525 | #define APPLY_TAINT_PROPER() \ |
3280af22 |
1526 | STMT_START { \ |
17406bd6 |
1527 | if (PL_tainted) { TAINT_PROPER(what); } \ |
873ef191 |
1528 | } STMT_END |
20408e3c |
1529 | |
1530 | /* This is a first heuristic; it doesn't catch tainting magic. */ |
3280af22 |
1531 | if (PL_tainting) { |
463ee0b2 |
1532 | while (++mark <= sp) { |
bbce6d69 |
1533 | if (SvTAINTED(*mark)) { |
1534 | TAINT; |
1535 | break; |
1536 | } |
463ee0b2 |
1537 | } |
1538 | mark = oldmark; |
1539 | } |
a687059c |
1540 | switch (type) { |
79072805 |
1541 | case OP_CHMOD: |
20408e3c |
1542 | what = "chmod"; |
1543 | APPLY_TAINT_PROPER(); |
79072805 |
1544 | if (++mark <= sp) { |
463ee0b2 |
1545 | val = SvIVx(*mark); |
20408e3c |
1546 | APPLY_TAINT_PROPER(); |
1547 | tot = sp - mark; |
79072805 |
1548 | while (++mark <= sp) { |
2d8e6c8d |
1549 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1550 | APPLY_TAINT_PROPER(); |
1551 | if (PerlLIO_chmod(name, val)) |
a687059c |
1552 | tot--; |
1553 | } |
1554 | } |
1555 | break; |
fe14fcc3 |
1556 | #ifdef HAS_CHOWN |
79072805 |
1557 | case OP_CHOWN: |
20408e3c |
1558 | what = "chown"; |
1559 | APPLY_TAINT_PROPER(); |
79072805 |
1560 | if (sp - mark > 2) { |
463ee0b2 |
1561 | val = SvIVx(*++mark); |
1562 | val2 = SvIVx(*++mark); |
20408e3c |
1563 | APPLY_TAINT_PROPER(); |
a0d0e21e |
1564 | tot = sp - mark; |
79072805 |
1565 | while (++mark <= sp) { |
2d8e6c8d |
1566 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1567 | APPLY_TAINT_PROPER(); |
36660982 |
1568 | if (PerlLIO_chown(name, val, val2)) |
a687059c |
1569 | tot--; |
1570 | } |
1571 | } |
1572 | break; |
b1248f16 |
1573 | #endif |
a1d180c4 |
1574 | /* |
dd64f1c3 |
1575 | XXX Should we make lchown() directly available from perl? |
1576 | For now, we'll let Configure test for HAS_LCHOWN, but do |
1577 | nothing in the core. |
1578 | --AD 5/1998 |
1579 | */ |
fe14fcc3 |
1580 | #ifdef HAS_KILL |
79072805 |
1581 | case OP_KILL: |
20408e3c |
1582 | what = "kill"; |
1583 | APPLY_TAINT_PROPER(); |
55497cff |
1584 | if (mark == sp) |
1585 | break; |
2d8e6c8d |
1586 | s = SvPVx(*++mark, n_a); |
79072805 |
1587 | if (isUPPER(*s)) { |
1588 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') |
1589 | s += 3; |
1590 | if (!(val = whichsig(s))) |
cea2e8a9 |
1591 | Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s); |
79072805 |
1592 | } |
1593 | else |
463ee0b2 |
1594 | val = SvIVx(*mark); |
20408e3c |
1595 | APPLY_TAINT_PROPER(); |
1596 | tot = sp - mark; |
3595fcef |
1597 | #ifdef VMS |
1598 | /* kill() doesn't do process groups (job trees?) under VMS */ |
1599 | if (val < 0) val = -val; |
1600 | if (val == SIGKILL) { |
1601 | # include <starlet.h> |
1602 | /* Use native sys$delprc() to insure that target process is |
1603 | * deleted; supervisor-mode images don't pay attention to |
1604 | * CRTL's emulation of Unix-style signals and kill() |
1605 | */ |
1606 | while (++mark <= sp) { |
1607 | I32 proc = SvIVx(*mark); |
1608 | register unsigned long int __vmssts; |
20408e3c |
1609 | APPLY_TAINT_PROPER(); |
3595fcef |
1610 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
1611 | tot--; |
1612 | switch (__vmssts) { |
1613 | case SS$_NONEXPR: |
1614 | case SS$_NOSUCHNODE: |
1615 | SETERRNO(ESRCH,__vmssts); |
1616 | break; |
1617 | case SS$_NOPRIV: |
1618 | SETERRNO(EPERM,__vmssts); |
1619 | break; |
1620 | default: |
1621 | SETERRNO(EVMSERR,__vmssts); |
1622 | } |
1623 | } |
1624 | } |
1625 | break; |
1626 | } |
1627 | #endif |
79072805 |
1628 | if (val < 0) { |
1629 | val = -val; |
1630 | while (++mark <= sp) { |
463ee0b2 |
1631 | I32 proc = SvIVx(*mark); |
20408e3c |
1632 | APPLY_TAINT_PROPER(); |
fe14fcc3 |
1633 | #ifdef HAS_KILLPG |
3028581b |
1634 | if (PerlProc_killpg(proc,val)) /* BSD */ |
a687059c |
1635 | #else |
3028581b |
1636 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
a687059c |
1637 | #endif |
79072805 |
1638 | tot--; |
a687059c |
1639 | } |
79072805 |
1640 | } |
1641 | else { |
1642 | while (++mark <= sp) { |
20408e3c |
1643 | I32 proc = SvIVx(*mark); |
1644 | APPLY_TAINT_PROPER(); |
1645 | if (PerlProc_kill(proc, val)) |
79072805 |
1646 | tot--; |
a687059c |
1647 | } |
1648 | } |
1649 | break; |
b1248f16 |
1650 | #endif |
79072805 |
1651 | case OP_UNLINK: |
20408e3c |
1652 | what = "unlink"; |
1653 | APPLY_TAINT_PROPER(); |
79072805 |
1654 | tot = sp - mark; |
1655 | while (++mark <= sp) { |
2d8e6c8d |
1656 | s = SvPVx(*mark, n_a); |
20408e3c |
1657 | APPLY_TAINT_PROPER(); |
3280af22 |
1658 | if (PL_euid || PL_unsafe) { |
a687059c |
1659 | if (UNLINK(s)) |
1660 | tot--; |
1661 | } |
1662 | else { /* don't let root wipe out directories without -U */ |
3280af22 |
1663 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
a687059c |
1664 | tot--; |
1665 | else { |
1666 | if (UNLINK(s)) |
1667 | tot--; |
1668 | } |
1669 | } |
1670 | } |
1671 | break; |
a0d0e21e |
1672 | #ifdef HAS_UTIME |
79072805 |
1673 | case OP_UTIME: |
20408e3c |
1674 | what = "utime"; |
1675 | APPLY_TAINT_PROPER(); |
79072805 |
1676 | if (sp - mark > 2) { |
748a9306 |
1677 | #if defined(I_UTIME) || defined(VMS) |
663a0e37 |
1678 | struct utimbuf utbuf; |
1679 | #else |
a687059c |
1680 | struct { |
dd2821f6 |
1681 | Time_t actime; |
1682 | Time_t modtime; |
a687059c |
1683 | } utbuf; |
663a0e37 |
1684 | #endif |
a687059c |
1685 | |
c6f7b413 |
1686 | SV* accessed = *++mark; |
1687 | SV* modified = *++mark; |
1688 | void * utbufp = &utbuf; |
1689 | |
1690 | /* be like C, and if both times are undefined, let the C |
1691 | library figure out what to do. This usually means |
1692 | "current time" */ |
1693 | |
1694 | if ( accessed == &PL_sv_undef && modified == &PL_sv_undef ) |
1695 | utbufp = NULL; |
1696 | |
afd9f252 |
1697 | Zero(&utbuf, sizeof utbuf, char); |
517844ec |
1698 | #ifdef BIG_TIME |
c6f7b413 |
1699 | utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */ |
1700 | utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */ |
517844ec |
1701 | #else |
c6f7b413 |
1702 | utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */ |
1703 | utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */ |
517844ec |
1704 | #endif |
20408e3c |
1705 | APPLY_TAINT_PROPER(); |
79072805 |
1706 | tot = sp - mark; |
1707 | while (++mark <= sp) { |
2d8e6c8d |
1708 | char *name = SvPVx(*mark, n_a); |
20408e3c |
1709 | APPLY_TAINT_PROPER(); |
c6f7b413 |
1710 | if (PerlLIO_utime(name, utbufp)) |
a687059c |
1711 | tot--; |
1712 | } |
a687059c |
1713 | } |
1714 | else |
79072805 |
1715 | tot = 0; |
a687059c |
1716 | break; |
a0d0e21e |
1717 | #endif |
a687059c |
1718 | } |
1719 | return tot; |
20408e3c |
1720 | |
20408e3c |
1721 | #undef APPLY_TAINT_PROPER |
a687059c |
1722 | } |
1723 | |
1724 | /* Do the permissions allow some operation? Assumes statcache already set. */ |
a0d0e21e |
1725 | #ifndef VMS /* VMS' cando is in vms.c */ |
7f4774ae |
1726 | bool |
1727 | Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp) |
1728 | /* Note: we use `effective' both for uids and gids. |
1729 | * Here we are betting on Uid_t being equal or wider than Gid_t. */ |
a687059c |
1730 | { |
bee1dbe2 |
1731 | #ifdef DOSISH |
fe14fcc3 |
1732 | /* [Comments and code from Len Reed] |
1733 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write |
1734 | * to write-protected files. The execute permission bit is set |
1735 | * by the Miscrosoft C library stat() function for the following: |
1736 | * .exe files |
1737 | * .com files |
1738 | * .bat files |
1739 | * directories |
1740 | * All files and directories are readable. |
1741 | * Directories and special files, e.g. "CON", cannot be |
1742 | * write-protected. |
1743 | * [Comment by Tom Dinger -- a directory can have the write-protect |
1744 | * bit set in the file system, but DOS permits changes to |
1745 | * the directory anyway. In addition, all bets are off |
1746 | * here for networked software, such as Novell and |
1747 | * Sun's PC-NFS.] |
1748 | */ |
1749 | |
bee1dbe2 |
1750 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
1751 | * too so it will actually look into the files for magic numbers |
1752 | */ |
7f4774ae |
1753 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
fe14fcc3 |
1754 | |
55497cff |
1755 | #else /* ! DOSISH */ |
3280af22 |
1756 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
7f4774ae |
1757 | if (mode == S_IXUSR) { |
c623bd54 |
1758 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c |
1759 | return TRUE; |
1760 | } |
1761 | else |
1762 | return TRUE; /* root reads and writes anything */ |
1763 | return FALSE; |
1764 | } |
3280af22 |
1765 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
7f4774ae |
1766 | if (statbufp->st_mode & mode) |
a687059c |
1767 | return TRUE; /* ok as "user" */ |
1768 | } |
d8eceb89 |
1769 | else if (ingroup(statbufp->st_gid,effective)) { |
7f4774ae |
1770 | if (statbufp->st_mode & mode >> 3) |
a687059c |
1771 | return TRUE; /* ok as "group" */ |
1772 | } |
7f4774ae |
1773 | else if (statbufp->st_mode & mode >> 6) |
a687059c |
1774 | return TRUE; /* ok as "other" */ |
1775 | return FALSE; |
55497cff |
1776 | #endif /* ! DOSISH */ |
a687059c |
1777 | } |
a0d0e21e |
1778 | #endif /* ! VMS */ |
a687059c |
1779 | |
d8eceb89 |
1780 | bool |
1781 | Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective) |
a687059c |
1782 | { |
cd39f2b6 |
1783 | #ifdef MACOS_TRADITIONAL |
1784 | /* This is simply not correct for AppleShare, but fix it yerself. */ |
1785 | return TRUE; |
1786 | #else |
3280af22 |
1787 | if (testgid == (effective ? PL_egid : PL_gid)) |
a687059c |
1788 | return TRUE; |
fe14fcc3 |
1789 | #ifdef HAS_GETGROUPS |
a687059c |
1790 | #ifndef NGROUPS |
1791 | #define NGROUPS 32 |
1792 | #endif |
1793 | { |
a0d0e21e |
1794 | Groups_t gary[NGROUPS]; |
79072805 |
1795 | I32 anum; |
a687059c |
1796 | |
1797 | anum = getgroups(NGROUPS,gary); |
1798 | while (--anum >= 0) |
1799 | if (gary[anum] == testgid) |
1800 | return TRUE; |
1801 | } |
1802 | #endif |
1803 | return FALSE; |
cd39f2b6 |
1804 | #endif |
a687059c |
1805 | } |
c2ab57d4 |
1806 | |
fe14fcc3 |
1807 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 |
1808 | |
79072805 |
1809 | I32 |
864dbfa3 |
1810 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1811 | { |
c2ab57d4 |
1812 | key_t key; |
79072805 |
1813 | I32 n, flags; |
c2ab57d4 |
1814 | |
463ee0b2 |
1815 | key = (key_t)SvNVx(*++mark); |
1816 | n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); |
1817 | flags = SvIVx(*++mark); |
748a9306 |
1818 | SETERRNO(0,0); |
c2ab57d4 |
1819 | switch (optype) |
1820 | { |
fe14fcc3 |
1821 | #ifdef HAS_MSG |
79072805 |
1822 | case OP_MSGGET: |
c2ab57d4 |
1823 | return msgget(key, flags); |
e5d73d77 |
1824 | #endif |
fe14fcc3 |
1825 | #ifdef HAS_SEM |
79072805 |
1826 | case OP_SEMGET: |
c2ab57d4 |
1827 | return semget(key, n, flags); |
e5d73d77 |
1828 | #endif |
fe14fcc3 |
1829 | #ifdef HAS_SHM |
79072805 |
1830 | case OP_SHMGET: |
c2ab57d4 |
1831 | return shmget(key, n, flags); |
e5d73d77 |
1832 | #endif |
fe14fcc3 |
1833 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 |
1834 | default: |
cea2e8a9 |
1835 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 |
1836 | #endif |
c2ab57d4 |
1837 | } |
1838 | return -1; /* should never happen */ |
1839 | } |
1840 | |
79072805 |
1841 | I32 |
864dbfa3 |
1842 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
1843 | { |
79072805 |
1844 | SV *astr; |
c2ab57d4 |
1845 | char *a; |
a0d0e21e |
1846 | I32 id, n, cmd, infosize, getinfo; |
1847 | I32 ret = -1; |
c2ab57d4 |
1848 | |
463ee0b2 |
1849 | id = SvIVx(*++mark); |
1850 | n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; |
1851 | cmd = SvIVx(*++mark); |
79072805 |
1852 | astr = *++mark; |
c2ab57d4 |
1853 | infosize = 0; |
1854 | getinfo = (cmd == IPC_STAT); |
1855 | |
1856 | switch (optype) |
1857 | { |
fe14fcc3 |
1858 | #ifdef HAS_MSG |
79072805 |
1859 | case OP_MSGCTL: |
c2ab57d4 |
1860 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1861 | infosize = sizeof(struct msqid_ds); |
1862 | break; |
e5d73d77 |
1863 | #endif |
fe14fcc3 |
1864 | #ifdef HAS_SHM |
79072805 |
1865 | case OP_SHMCTL: |
c2ab57d4 |
1866 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1867 | infosize = sizeof(struct shmid_ds); |
1868 | break; |
e5d73d77 |
1869 | #endif |
fe14fcc3 |
1870 | #ifdef HAS_SEM |
79072805 |
1871 | case OP_SEMCTL: |
39398f3f |
1872 | #ifdef Semctl |
c2ab57d4 |
1873 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1874 | infosize = sizeof(struct semid_ds); |
1875 | else if (cmd == GETALL || cmd == SETALL) |
1876 | { |
8e591e46 |
1877 | struct semid_ds semds; |
bd89102f |
1878 | union semun semun; |
e6f0bdd6 |
1879 | #ifdef EXTRA_F_IN_SEMUN_BUF |
1880 | semun.buff = &semds; |
1881 | #else |
84902520 |
1882 | semun.buf = &semds; |
e6f0bdd6 |
1883 | #endif |
c2ab57d4 |
1884 | getinfo = (cmd == GETALL); |
9b89d93d |
1885 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
1886 | return -1; |
6e21c824 |
1887 | infosize = semds.sem_nsems * sizeof(short); |
1888 | /* "short" is technically wrong but much more portable |
1889 | than guessing about u_?short(_t)? */ |
c2ab57d4 |
1890 | } |
39398f3f |
1891 | #else |
cea2e8a9 |
1892 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f |
1893 | #endif |
c2ab57d4 |
1894 | break; |
e5d73d77 |
1895 | #endif |
fe14fcc3 |
1896 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 |
1897 | default: |
cea2e8a9 |
1898 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 |
1899 | #endif |
c2ab57d4 |
1900 | } |
1901 | |
1902 | if (infosize) |
1903 | { |
a0d0e21e |
1904 | STRLEN len; |
c2ab57d4 |
1905 | if (getinfo) |
1906 | { |
a0d0e21e |
1907 | SvPV_force(astr, len); |
1908 | a = SvGROW(astr, infosize+1); |
c2ab57d4 |
1909 | } |
1910 | else |
1911 | { |
463ee0b2 |
1912 | a = SvPV(astr, len); |
1913 | if (len != infosize) |
cea2e8a9 |
1914 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 |
1915 | PL_op_desc[optype], |
1916 | (unsigned long)len, |
1917 | (long)infosize); |
c2ab57d4 |
1918 | } |
1919 | } |
1920 | else |
1921 | { |
c030ccd9 |
1922 | IV i = SvIV(astr); |
56431972 |
1923 | a = INT2PTR(char *,i); /* ouch */ |
c2ab57d4 |
1924 | } |
748a9306 |
1925 | SETERRNO(0,0); |
c2ab57d4 |
1926 | switch (optype) |
1927 | { |
fe14fcc3 |
1928 | #ifdef HAS_MSG |
79072805 |
1929 | case OP_MSGCTL: |
bee1dbe2 |
1930 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 |
1931 | break; |
e5d73d77 |
1932 | #endif |
fe14fcc3 |
1933 | #ifdef HAS_SEM |
bd89102f |
1934 | case OP_SEMCTL: { |
39398f3f |
1935 | #ifdef Semctl |
bd89102f |
1936 | union semun unsemds; |
1937 | |
e6f0bdd6 |
1938 | #ifdef EXTRA_F_IN_SEMUN_BUF |
1939 | unsemds.buff = (struct semid_ds *)a; |
1940 | #else |
bd89102f |
1941 | unsemds.buf = (struct semid_ds *)a; |
e6f0bdd6 |
1942 | #endif |
bd89102f |
1943 | ret = Semctl(id, n, cmd, unsemds); |
39398f3f |
1944 | #else |
cea2e8a9 |
1945 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f |
1946 | #endif |
bd89102f |
1947 | } |
c2ab57d4 |
1948 | break; |
e5d73d77 |
1949 | #endif |
fe14fcc3 |
1950 | #ifdef HAS_SHM |
79072805 |
1951 | case OP_SHMCTL: |
bee1dbe2 |
1952 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 |
1953 | break; |
e5d73d77 |
1954 | #endif |
c2ab57d4 |
1955 | } |
1956 | if (getinfo && ret >= 0) { |
79072805 |
1957 | SvCUR_set(astr, infosize); |
1958 | *SvEND(astr) = '\0'; |
a0d0e21e |
1959 | SvSETMAGIC(astr); |
c2ab57d4 |
1960 | } |
1961 | return ret; |
1962 | } |
1963 | |
79072805 |
1964 | I32 |
864dbfa3 |
1965 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
1966 | { |
fe14fcc3 |
1967 | #ifdef HAS_MSG |
79072805 |
1968 | SV *mstr; |
c2ab57d4 |
1969 | char *mbuf; |
79072805 |
1970 | I32 id, msize, flags; |
463ee0b2 |
1971 | STRLEN len; |
c2ab57d4 |
1972 | |
463ee0b2 |
1973 | id = SvIVx(*++mark); |
79072805 |
1974 | mstr = *++mark; |
463ee0b2 |
1975 | flags = SvIVx(*++mark); |
1976 | mbuf = SvPV(mstr, len); |
1977 | if ((msize = len - sizeof(long)) < 0) |
cea2e8a9 |
1978 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 |
1979 | SETERRNO(0,0); |
bee1dbe2 |
1980 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 |
1981 | #else |
cea2e8a9 |
1982 | Perl_croak(aTHX_ "msgsnd not implemented"); |
e5d73d77 |
1983 | #endif |
c2ab57d4 |
1984 | } |
1985 | |
79072805 |
1986 | I32 |
864dbfa3 |
1987 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
1988 | { |
fe14fcc3 |
1989 | #ifdef HAS_MSG |
79072805 |
1990 | SV *mstr; |
c2ab57d4 |
1991 | char *mbuf; |
1992 | long mtype; |
79072805 |
1993 | I32 id, msize, flags, ret; |
463ee0b2 |
1994 | STRLEN len; |
79072805 |
1995 | |
463ee0b2 |
1996 | id = SvIVx(*++mark); |
79072805 |
1997 | mstr = *++mark; |
c2e66d9e |
1998 | /* suppress warning when reading into undef var --jhi */ |
1999 | if (! SvOK(mstr)) |
2000 | sv_setpvn(mstr, "", 0); |
463ee0b2 |
2001 | msize = SvIVx(*++mark); |
2002 | mtype = (long)SvIVx(*++mark); |
2003 | flags = SvIVx(*++mark); |
a0d0e21e |
2004 | SvPV_force(mstr, len); |
2005 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); |
a1d180c4 |
2006 | |
748a9306 |
2007 | SETERRNO(0,0); |
bee1dbe2 |
2008 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 |
2009 | if (ret >= 0) { |
79072805 |
2010 | SvCUR_set(mstr, sizeof(long)+ret); |
2011 | *SvEND(mstr) = '\0'; |
41d6edb2 |
2012 | #ifndef INCOMPLETE_TAINTS |
2013 | /* who knows who has been playing with this message? */ |
2014 | SvTAINTED_on(mstr); |
2015 | #endif |
c2ab57d4 |
2016 | } |
2017 | return ret; |
e5d73d77 |
2018 | #else |
cea2e8a9 |
2019 | Perl_croak(aTHX_ "msgrcv not implemented"); |
e5d73d77 |
2020 | #endif |
c2ab57d4 |
2021 | } |
2022 | |
79072805 |
2023 | I32 |
864dbfa3 |
2024 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 |
2025 | { |
fe14fcc3 |
2026 | #ifdef HAS_SEM |
79072805 |
2027 | SV *opstr; |
c2ab57d4 |
2028 | char *opbuf; |
463ee0b2 |
2029 | I32 id; |
2030 | STRLEN opsize; |
c2ab57d4 |
2031 | |
463ee0b2 |
2032 | id = SvIVx(*++mark); |
79072805 |
2033 | opstr = *++mark; |
463ee0b2 |
2034 | opbuf = SvPV(opstr, opsize); |
248ff010 |
2035 | if (opsize < 3 * SHORTSIZE |
2036 | || (opsize % (3 * SHORTSIZE))) { |
748a9306 |
2037 | SETERRNO(EINVAL,LIB$_INVARG); |
c2ab57d4 |
2038 | return -1; |
2039 | } |
748a9306 |
2040 | SETERRNO(0,0); |
248ff010 |
2041 | /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */ |
2042 | { |
2043 | int nsops = opsize / (3 * sizeof (short)); |
2044 | int i = nsops; |
2045 | short *ops = (short *) opbuf; |
2046 | short *o = ops; |
2047 | struct sembuf *temps, *t; |
2048 | I32 result; |
2049 | |
2050 | New (0, temps, nsops, struct sembuf); |
2051 | t = temps; |
2052 | while (i--) { |
2053 | t->sem_num = *o++; |
2054 | t->sem_op = *o++; |
2055 | t->sem_flg = *o++; |
2056 | t++; |
2057 | } |
2058 | result = semop(id, temps, nsops); |
2059 | t = temps; |
2060 | o = ops; |
2061 | i = nsops; |
2062 | while (i--) { |
2063 | *o++ = t->sem_num; |
2064 | *o++ = t->sem_op; |
2065 | *o++ = t->sem_flg; |
2066 | t++; |
2067 | } |
2068 | Safefree(temps); |
2069 | return result; |
2070 | } |
e5d73d77 |
2071 | #else |
cea2e8a9 |
2072 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 |
2073 | #endif |
c2ab57d4 |
2074 | } |
2075 | |
79072805 |
2076 | I32 |
864dbfa3 |
2077 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 |
2078 | { |
fe14fcc3 |
2079 | #ifdef HAS_SHM |
79072805 |
2080 | SV *mstr; |
c2ab57d4 |
2081 | char *mbuf, *shm; |
79072805 |
2082 | I32 id, mpos, msize; |
463ee0b2 |
2083 | STRLEN len; |
c2ab57d4 |
2084 | struct shmid_ds shmds; |
c2ab57d4 |
2085 | |
463ee0b2 |
2086 | id = SvIVx(*++mark); |
79072805 |
2087 | mstr = *++mark; |
463ee0b2 |
2088 | mpos = SvIVx(*++mark); |
2089 | msize = SvIVx(*++mark); |
748a9306 |
2090 | SETERRNO(0,0); |
c2ab57d4 |
2091 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
2092 | return -1; |
2093 | if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { |
748a9306 |
2094 | SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 |
2095 | return -1; |
2096 | } |
8ac85365 |
2097 | shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 |
2098 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
2099 | return -1; |
79072805 |
2100 | if (optype == OP_SHMREAD) { |
9f538c04 |
2101 | /* suppress warning when reading into undef var (tchrist 3/Mar/00) */ |
2102 | if (! SvOK(mstr)) |
2103 | sv_setpvn(mstr, "", 0); |
a0d0e21e |
2104 | SvPV_force(mstr, len); |
2105 | mbuf = SvGROW(mstr, msize+1); |
2106 | |
bee1dbe2 |
2107 | Copy(shm + mpos, mbuf, msize, char); |
79072805 |
2108 | SvCUR_set(mstr, msize); |
2109 | *SvEND(mstr) = '\0'; |
a0d0e21e |
2110 | SvSETMAGIC(mstr); |
d929ce6f |
2111 | #ifndef INCOMPLETE_TAINTS |
2112 | /* who knows who has been playing with this shared memory? */ |
2113 | SvTAINTED_on(mstr); |
2114 | #endif |
c2ab57d4 |
2115 | } |
2116 | else { |
79072805 |
2117 | I32 n; |
c2ab57d4 |
2118 | |
a0d0e21e |
2119 | mbuf = SvPV(mstr, len); |
463ee0b2 |
2120 | if ((n = len) > msize) |
c2ab57d4 |
2121 | n = msize; |
bee1dbe2 |
2122 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 |
2123 | if (n < msize) |
bee1dbe2 |
2124 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 |
2125 | } |
2126 | return shmdt(shm); |
e5d73d77 |
2127 | #else |
cea2e8a9 |
2128 | Perl_croak(aTHX_ "shm I/O not implemented"); |
e5d73d77 |
2129 | #endif |
c2ab57d4 |
2130 | } |
2131 | |
fe14fcc3 |
2132 | #endif /* SYSV IPC */ |
4e35701f |
2133 | |
0d44d22b |
2134 | /* |
2135 | =for apidoc start_glob |
2136 | |
2137 | Function called by C<do_readline> to spawn a glob (or do the glob inside |
2138 | perl on VMS). This code used to be inline, but now perl uses C<File::Glob> |
2139 | this glob starter is only used by miniperl during the build proccess. |
2140 | Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up. |
fab3f3a7 |
2141 | |
0d44d22b |
2142 | =cut |
2143 | */ |
2144 | |
2145 | PerlIO * |
2146 | Perl_start_glob (pTHX_ SV *tmpglob, IO *io) |
2147 | { |
2148 | SV *tmpcmd = NEWSV(55, 0); |
2149 | PerlIO *fp; |
2150 | ENTER; |
2151 | SAVEFREESV(tmpcmd); |
2152 | #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */ |
2153 | /* since spawning off a process is a real performance hit */ |
2154 | { |
2155 | #include <descrip.h> |
2156 | #include <lib$routines.h> |
2157 | #include <nam.h> |
2158 | #include <rmsdef.h> |
2159 | char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'}; |
2160 | char vmsspec[NAM$C_MAXRSS+1]; |
2161 | char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp; |
0d44d22b |
2162 | $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;"); |
2163 | PerlIO *tmpfp; |
2164 | STRLEN i; |
2165 | struct dsc$descriptor_s wilddsc |
2166 | = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0}; |
2167 | struct dsc$descriptor_vs rsdsc |
2168 | = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt}; |
2169 | unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0; |
2170 | |
2171 | /* We could find out if there's an explicit dev/dir or version |
2172 | by peeking into lib$find_file's internal context at |
2173 | ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb |
2174 | but that's unsupported, so I don't want to do it now and |
2175 | have it bite someone in the future. */ |
0d44d22b |
2176 | cp = SvPV(tmpglob,i); |
2177 | for (; i; i--) { |
2178 | if (cp[i] == ';') hasver = 1; |
2179 | if (cp[i] == '.') { |
2180 | if (sts) hasver = 1; |
2181 | else sts = 1; |
2182 | } |
2183 | if (cp[i] == '/') { |
2184 | hasdir = isunix = 1; |
2185 | break; |
2186 | } |
2187 | if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') { |
2188 | hasdir = 1; |
2189 | break; |
2190 | } |
2191 | } |
a15cef0c |
2192 | if ((tmpfp = PerlIO_tmpfile()) != NULL) { |
0d44d22b |
2193 | Stat_t st; |
2194 | if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode)) |
2195 | ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL); |
2196 | else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL); |
2197 | if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer); |
2198 | while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt, |
2199 | &dfltdsc,NULL,NULL,NULL))&1)) { |
2200 | end = rstr + (unsigned long int) *rslt; |
2201 | if (!hasver) while (*end != ';') end--; |
2202 | *(end++) = '\n'; *end = '\0'; |
2203 | for (cp = rstr; *cp; cp++) *cp = _tolower(*cp); |
2204 | if (hasdir) { |
2205 | if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1); |
2206 | begin = rstr; |
2207 | } |
2208 | else { |
2209 | begin = end; |
2210 | while (*(--begin) != ']' && *begin != '>') ; |
2211 | ++begin; |
2212 | } |
2213 | ok = (PerlIO_puts(tmpfp,begin) != EOF); |
2214 | } |
2215 | if (cxt) (void)lib$find_file_end(&cxt); |
2216 | if (ok && sts != RMS$_NMF && |
2217 | sts != RMS$_DNF && sts != RMS$_FNF) ok = 0; |
2218 | if (!ok) { |
2219 | if (!(sts & 1)) { |
2220 | SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts); |
2221 | } |
2222 | PerlIO_close(tmpfp); |
2223 | fp = NULL; |
2224 | } |
2225 | else { |
2226 | PerlIO_rewind(tmpfp); |
2227 | IoTYPE(io) = IoTYPE_RDONLY; |
2228 | IoIFP(io) = fp = tmpfp; |
2229 | IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */ |
2230 | } |
2231 | } |
2232 | } |
2233 | #else /* !VMS */ |
2234 | #ifdef MACOS_TRADITIONAL |
2235 | sv_setpv(tmpcmd, "glob "); |
2236 | sv_catsv(tmpcmd, tmpglob); |
2237 | sv_catpv(tmpcmd, " |"); |
2238 | #else |
2239 | #ifdef DOSISH |
2240 | #ifdef OS2 |
2241 | sv_setpv(tmpcmd, "for a in "); |
2242 | sv_catsv(tmpcmd, tmpglob); |
2243 | sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |"); |
2244 | #else |
2245 | #ifdef DJGPP |
2246 | sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */ |
2247 | sv_catsv(tmpcmd, tmpglob); |
2248 | #else |
2249 | sv_setpv(tmpcmd, "perlglob "); |
2250 | sv_catsv(tmpcmd, tmpglob); |
2251 | sv_catpv(tmpcmd, " |"); |
2252 | #endif /* !DJGPP */ |
2253 | #endif /* !OS2 */ |
2254 | #else /* !DOSISH */ |
2255 | #if defined(CSH) |
2256 | sv_setpvn(tmpcmd, PL_cshname, PL_cshlen); |
2257 | sv_catpv(tmpcmd, " -cf 'set nonomatch; glob "); |
2258 | sv_catsv(tmpcmd, tmpglob); |
2259 | sv_catpv(tmpcmd, "' 2>/dev/null |"); |
2260 | #else |
2261 | sv_setpv(tmpcmd, "echo "); |
2262 | sv_catsv(tmpcmd, tmpglob); |
2263 | #if 'z' - 'a' == 25 |
2264 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|"); |
2265 | #else |
2266 | sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|"); |
2267 | #endif |
2268 | #endif /* !CSH */ |
2269 | #endif /* !DOSISH */ |
2270 | #endif /* MACOS_TRADITIONAL */ |
2271 | (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd), |
2272 | FALSE, O_RDONLY, 0, Nullfp); |
2273 | fp = IoIFP(io); |
2274 | #endif /* !VMS */ |
2275 | LEAVE; |
2276 | return fp; |
2277 | } |