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