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