The *right* way to make the csighandler visible.
[p5sagit/p5-mst-13.2.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
4bb101f2 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 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 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 16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_DOIO_C
a687059c 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 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 56Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
57 int rawmode, int rawperm, PerlIO *supplied_fp)
58{
a567e93b 59 return do_openn(gv, name, len, as_raw, rawmode, rawperm,
60 supplied_fp, (SV **) NULL, 0);
6170680b 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 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 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 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 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 108 if (IoTYPE(io) == IoTYPE_STD) {
109 /* This is a clone of one of STD* handles */
c2ab57d4 110 result = 0;
ee518936 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 117 savefd = fd;
118 result = 0;
6e21c824 119 }
50952442 120 else if (IoTYPE(io) == IoTYPE_PIPE)
3028581b 121 result = PerlProc_pclose(IoIFP(io));
8990e307 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 126 }
127 else
760ac839 128 result = PerlIO_close(IoIFP(io));
a687059c 129 }
a687059c 130 else
760ac839 131 result = PerlIO_close(IoIFP(io));
ee518936 132 if (result == EOF && fd > PL_maxsysfd) {
133 /* Why is this not Perl_warn*() call ? */
bf49b057 134 PerlIO_printf(Perl_error_log,
6170680b 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 144 int appendtrunc =
145 0
d1da7611 146#ifdef O_APPEND /* Not fully portable. */
3dccc55c 147 |O_APPEND
d1da7611 148#endif
149#ifdef O_TRUNC /* Not fully portable. */
3dccc55c 150 |O_TRUNC
d1da7611 151#endif
3dccc55c 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 186#endif
187
06c7082d 188 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
ee518936 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 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 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 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 223 goto say_false;
224 }
225#endif /* USE_STDIO */
f6c77cf1 226 name = SvOK(*svp) ? SvPV(*svp, l) : "";
faecd977 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 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 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 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 275 if (out_raw)
276 strcat(mode, "b");
277 else if (out_crlf)
278 strcat(mode, "t");
4a7d1889 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 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 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 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 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 324 if (num_svs > 1) {
325 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
326 }
54edfe98 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 334 fd = atoi(type);
335 }
c07a80fd 336 else {
337 IO* thatio;
e620cd72 338 if (num_svs) {
339 thatio = sv_2io(*svp);
340 }
341 else {
342 GV *thatgv;
e620cd72 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 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 358#ifdef USE_SFIO
359 /* sfio fails to clear error on next
360 sfwrite, contrary to documentation.
361 -- Nick Clark */
ecdeb87c 362 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
363 PerlIO_clearerr(that_fp);
2c534a3f 364#endif
7211d486 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 369 PerlIO_flush(that_fp);
370 fd = PerlIO_fileno(that_fp);
0759c907 371 /* When dup()ing STDIN, STDOUT or STDERR
372 * explicitly set appropriate access mode */
f4e789af 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 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 388 if (that_fp) {
389 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
390 }
391 else {
c07a80fd 392 if (dodup)
ecdeb87c 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 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 411 if (num_svs > 1) {
412 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
413 }
c07a80fd 414 }
415 else {
ee518936 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 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 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 443 if (num_svs > 1) {
444 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
445 }
a687059c 446 }
ee518936 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 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 461 }
462 else {
b931b1d9 463 *--tend = '\0';
464 while (tend > type && isSPACE(tend[-1]))
465 *--tend = '\0';
6170680b 466 /*SUPPRESS 530*/
467 for (; isSPACE(*type); type++) ;
468 name = type;
b931b1d9 469 len = tend-type;
6170680b 470 }
4a7d1889 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 475 errno = EPIPE;
476 goto say_false;
477 }
6170680b 478 if (strNE(name,"-") || num_svs)
79072805 479 TAINT_ENV();
480 TAINT_PROPER("piped open");
a1d180c4 481 mode[0] = 'r';
482 if (in_raw)
483 strcat(mode, "b");
484 else if (in_crlf)
485 strcat(mode, "t");
4a7d1889 486 if (num_svs > 1) {
487 fp = PerlProc_popen_list(mode,num_svs,svp);
488 }
e620cd72 489 else {
4a7d1889 490 fp = PerlProc_popen(name,mode);
491 }
50952442 492 IoTYPE(io) = IoTYPE_PIPE;
1771866f 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 501 }
502 else {
6170680b 503 if (num_svs)
504 goto unknown_desr;
505 name = type;
50952442 506 IoTYPE(io) = IoTYPE_RDONLY;
99b89507 507 /*SUPPRESS 530*/
508 for (; isSPACE(*name); name++) ;
88b61e10 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 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 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 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 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 545 "Filehandle STDIN reopened as %s only for output",
546 GvENAME(gv));
a00b5bd3 547 }
548 }
549
e99cca91 550 fd = PerlIO_fileno(fp);
e934609f 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 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 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 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 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 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 598 }
599 }
6e60e805 600 if (savefd != fd) {
e934609f 601 /* Still a small can-of-worms here if (say) PerlIO::scalar
ecdeb87c 602 is assigned to (say) STDOUT - for now let dup2() fail
603 and provide the error
604 */
bd4a5668 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 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 618 }
619#endif
d0e2cf63 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 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 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 648 PerlIO_close(fp);
649 PerlLIO_dup2(dupfd,ofd);
03e631df 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 656 else
657 PerlIO_close(fp);
6e21c824 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 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 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 685 }
686 else
8990e307 687 IoOFP(io) = fp;
bf38876a 688 }
a687059c 689 return TRUE;
6e21c824 690
691say_false:
8990e307 692 IoIFP(io) = saveifp;
693 IoOFP(io) = saveofp;
694 IoTYPE(io) = savetype;
6e21c824 695 return FALSE;
a687059c 696}
697
760ac839 698PerlIO *
864dbfa3 699Perl_nextargv(pTHX_ register GV *gv)
a687059c 700{
79072805 701 register SV *sv;
99b89507 702#ifndef FLEXFILENAMES
c623bd54 703 int filedev;
704 int fileino;
99b89507 705#endif
761237fe 706 Uid_t fileuid;
707 Gid_t filegid;
18708f5a 708 IO *io = GvIOp(gv);
fe14fcc3 709
3280af22 710 if (!PL_argvoutgv)
711 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
18708f5a 712 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
713 IoFLAGS(io) &= ~IOf_START;
7a1c5554 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 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 726#endif
727 }
3280af22 728 PL_filemode = 0;
5c501b37 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 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 746 filedev = PL_statbuf.st_dev;
747 fileino = PL_statbuf.st_ino;
99b89507 748#endif
3280af22 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 755 "Can't do inplace edit: %s is not a regular file",
756 PL_oldname );
79072805 757 do_close(gv,FALSE);
c623bd54 758 continue;
759 }
3280af22 760 if (*PL_inplace) {
761 char *star = strchr(PL_inplace, '*');
2d259d92 762 if (star) {
3280af22 763 char *begin = PL_inplace;
2d259d92 764 sv_setpvn(sv, "", 0);
765 do {
766 sv_catpvn(sv, begin, star - begin);
3280af22 767 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92 768 begin = ++star;
769 } while ((star = strchr(begin, '*')));
3d66d7bb 770 if (*begin)
771 sv_catpv(sv,begin);
2d259d92 772 }
773 else {
3280af22 774 sv_catpv(sv,PL_inplace);
2d259d92 775 }
c623bd54 776#ifndef FLEXFILENAMES
5f74f29c 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 783 )
784 {
785 if (ckWARN_d(WARN_INPLACE))
9014280d 786 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
35c1215d 787 "Can't do inplace edit: %"SVf" would not be unique",
788 sv);
79072805 789 do_close(gv,FALSE);
c623bd54 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 798 "Can't rename %s to %"SVf": %s, skipping file",
799 PL_oldname, sv, Strerror(errno) );
79072805 800 do_close(gv,FALSE);
c623bd54 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 814 "Can't rename %s to %"SVf": %s, skipping file",
815 PL_oldname, sv, Strerror(errno) );
79072805 816 do_close(gv,FALSE);
c623bd54 817 continue;
818 }
b28d0864 819 (void)UNLINK(PL_oldname);
a687059c 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 828 "Can't remove %s: %s, skipping file",
829 PL_oldname, Strerror(errno) );
79072805 830 do_close(gv,FALSE);
fe14fcc3 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 837 }
838
3280af22 839 sv_setpvn(sv,">",!PL_inplace);
840 sv_catpvn(sv,PL_oldname,oldlen);
748a9306 841 SETERRNO(0,0); /* in case sprintf set errno */
4119ab01 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 854 continue;
855 }
3280af22 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 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 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 892 }
893 }
a687059c 894 }
18708f5a 895 if (io && (IoFLAGS(io) & IOf_ARGV))
896 IoFLAGS(io) |= IOf_START;
3280af22 897 if (PL_inplace) {
898 (void)do_close(PL_argvoutgv,FALSE);
7a1c5554 899 if (io && (IoFLAGS(io) & IOf_ARGV)
900 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
901 {
18708f5a 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 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 916 register IO *rstio;
917 register IO *wstio;
afd9f252 918 int fd[2];
919
79072805 920 if (!rgv)
afd9f252 921 goto badexit;
79072805 922 if (!wgv)
afd9f252 923 goto badexit;
924
a0d0e21e 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 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 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 946 goto badexit;
947 }
afd9f252 948
3280af22 949 sv_setsv(sv,&PL_sv_yes);
afd9f252 950 return;
951
952badexit:
3280af22 953 sv_setsv(sv,&PL_sv_undef);
afd9f252 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 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 972 io = GvIO(gv);
973 if (!io) { /* never opened */
1d2dff63 974 if (not_implicit) {
2dd78f96 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 979 return FALSE;
980 }
f2b5be74 981 retval = io_close(io, not_implicit);
517844ec 982 if (not_implicit) {
1193dd27 983 IoLINES(io) = 0;
984 IoPAGE(io) = 0;
985 IoLINES_LEFT(io) = IoPAGE_LEN(io);
986 }
50952442 987 IoTYPE(io) = IoTYPE_CLOSED;
1193dd27 988 return retval;
989}
990
991bool
f2b5be74 992Perl_io_close(pTHX_ IO *io, bool not_implicit)
1193dd27 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 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 1009 retval = TRUE;
1010 else {
8990e307 1011 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
760ac839 1012 retval = (PerlIO_close(IoOFP(io)) != EOF);
1013 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 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 1024 return retval;
1025}
1026
1027bool
864dbfa3 1028Perl_do_eof(pTHX_ GV *gv)
a687059c 1029{
79072805 1030 register IO *io;
a687059c 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 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 1053 return FALSE;
1054 }
505fdf5d 1055 errno = saverrno;
fab3f3a7 1056
760ac839 1057 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3 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 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 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 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 1103 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1104 report_evil_fh(gv, io, PL_op->op_type);
93189314 1105 SETERRNO(EBADF,RMS_IFI);
a687059c 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 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 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 1169#else
1170 s = end;
1171#endif
16fe6d59 1172 }
1173 }
1174 }
1175 return mode;
1176}
1177
1178int
1179Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
6ff81951 1180{
60382766 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 1187 name = ":crlf";
1188#endif
60382766 1189 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
6ff81951 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 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 1211 return -1;
1212
1213 /* write a "0" byte */
1214
3028581b 1215 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b 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 1225
1226 /*
a0d0e21e 1227 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b 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 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 1245{
1246 register char *tmps;
463ee0b2 1247 STRLEN len;
a687059c 1248
79072805 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 1265 switch (SvTYPE(sv)) {
1266 case SVt_NULL:
411caa50 1267 if (ckWARN(WARN_UNINITIALIZED))
1268 report_uninit();
ff8e2863 1269 return TRUE;
79072805 1270 case SVt_IV:
a0d0e21e 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 1279 }
1280 /* FALL THROUGH */
79072805 1281 default:
7d59b7e4 1282 if (PerlIO_isutf8(fp)) {
f9a63242 1283 if (!SvUTF8(sv))
88632417 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 1292 }
1293 }
f9a63242 1294 tmps = SvPV(sv, len);
79072805 1295 break;
ff8e2863 1296 }
94e4c244 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 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 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 1322 sv_setpv(PL_statname,"");
1323 PL_laststype = OP_STAT;
1324 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 1325 }
1326 else {
2dd78f96 1327 if (gv == PL_defgv)
3280af22 1328 return PL_laststatval;
2dd78f96 1329 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1330 report_evil_fh(gv, io, PL_op->op_type);
3280af22 1331 PL_statgv = Nullgv;
1332 sv_setpv(PL_statname,"");
1333 return (PL_laststatval = -1);
a687059c 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 1343 goto do_fstat;
1344 }
1345 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 1346 gv = (GV*)SvRV(sv);
748a9306 1347 goto do_fstat;
1348 }
1349
4ecd490c 1350 s = SvPV(sv, len);
3280af22 1351 PL_statgv = Nullgv;
4ecd490c 1352 sv_setpvn(PL_statname, s, len);
1353 s = SvPVX(PL_statname); /* s now NUL-terminated */
3280af22 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 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 1377 GvENAME(cGVOP_gv));
1378 return (PL_laststatval = -1);
1379 }
fe14fcc3 1380 }
c623bd54 1381
3280af22 1382 PL_laststype = OP_LSTAT;
1383 PL_statgv = Nullgv;
79072805 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 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 1396}
1397
622913ab 1398#ifndef OS2
a687059c 1399bool
864dbfa3 1400Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1401{
d5a9bfb0 1402 return do_aexec5(really, mark, sp, 0, 0);
1403}
622913ab 1404#endif
d5a9bfb0 1405
1406bool
2aa1486d 1407Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1408 int fd, int do_report)
d5a9bfb0 1409{
cd39f2b6 1410#ifdef MACOS_TRADITIONAL
1411 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1412#else
a687059c 1413 register char **a;
9c5ffd7c 1414 char *tmps = Nullch;
2d8e6c8d 1415 STRLEN n_a;
a687059c 1416
79072805 1417 if (sp > mark) {
3280af22 1418 New(401,PL_Argv, sp - mark + 1, char*);
1419 a = PL_Argv;
79072805 1420 while (++mark <= sp) {
1421 if (*mark)
2d8e6c8d 1422 *a++ = SvPVx(*mark, n_a);
a687059c 1423 else
1424 *a++ = "";
1425 }
1426 *a = Nullch;
91b2752f 1427 if (really)
1428 tmps = SvPV(really, n_a);
1429 if ((!really && *PL_Argv[0] != '/') ||
1430 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1431 TAINT_ENV(); /* testing IFS here is overkill, probably */
91b2752f 1432 if (really && *tmps)
b4748376 1433 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1434 else
b4748376 1435 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
599cee73 1436 if (ckWARN(WARN_EXEC))
9014280d 1437 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
91b2752f 1438 (really ? tmps : PL_Argv[0]), Strerror(errno));
d5a9bfb0 1439 if (do_report) {
1440 int e = errno;
1441
1442 PerlLIO_write(fd, (void*)&e, sizeof(int));
1443 PerlLIO_close(fd);
1444 }
a687059c 1445 }
bee1dbe2 1446 do_execfree();
cd39f2b6 1447#endif
a687059c 1448 return FALSE;
1449}
1450
fe14fcc3 1451void
864dbfa3 1452Perl_do_execfree(pTHX)
ff8e2863 1453{
3280af22 1454 if (PL_Argv) {
1455 Safefree(PL_Argv);
1456 PL_Argv = Null(char **);
ff8e2863 1457 }
3280af22 1458 if (PL_Cmd) {
1459 Safefree(PL_Cmd);
1460 PL_Cmd = Nullch;
ff8e2863 1461 }
1462}
1463
cd39f2b6 1464#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1465
a687059c 1466bool
864dbfa3 1467Perl_do_exec(pTHX_ char *cmd)
a687059c 1468{
e446cec8 1469 return do_exec3(cmd,0,0);
1470}
1471
1472bool
864dbfa3 1473Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1474{
a687059c 1475 register char **a;
1476 register char *s;
a687059c 1477
748a9306 1478 while (*cmd && isSPACE(*cmd))
1479 cmd++;
1480
a687059c 1481 /* save an extra exec if possible */
1482
bf38876a 1483#ifdef CSH
d05c1ba0 1484 {
1485 char flags[10];
1486 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1487 strnEQ(cmd+PL_cshlen," -c",3)) {
1488 strcpy(flags,"-c");
1489 s = cmd+PL_cshlen+3;
1490 if (*s == 'f') {
1491 s++;
1492 strcat(flags,"f");
1493 }
1494 if (*s == ' ')
1495 s++;
1496 if (*s++ == '\'') {
1497 char *ncmd = s;
1498
1499 while (*s)
1500 s++;
1501 if (s[-1] == '\n')
1502 *--s = '\0';
1503 if (s[-1] == '\'') {
1504 *--s = '\0';
1505 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1506 *s = '\'';
1507 return FALSE;
1508 }
1509 }
a687059c 1510 }
1511 }
bf38876a 1512#endif /* CSH */
a687059c 1513
1514 /* see if there are shell metacharacters in it */
1515
748a9306 1516 if (*cmd == '.' && isSPACE(cmd[1]))
1517 goto doshell;
1518
1519 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1520 goto doshell;
1521
c170e444 1522 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1523 if (*s == '=')
1524 goto doshell;
748a9306 1525
a687059c 1526 for (s = cmd; *s; s++) {
d05c1ba0 1527 if (*s != ' ' && !isALPHA(*s) &&
1528 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1529 if (*s == '\n' && !s[1]) {
1530 *s = '\0';
1531 break;
1532 }
603a98b0 1533 /* handle the 2>&1 construct at the end */
1534 if (*s == '>' && s[1] == '&' && s[2] == '1'
1535 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1536 && (!s[3] || isSPACE(s[3])))
1537 {
1538 char *t = s + 3;
1539
1540 while (*t && isSPACE(*t))
1541 ++t;
1542 if (!*t && (dup2(1,2) != -1)) {
1543 s[-2] = '\0';
1544 break;
1545 }
1546 }
a687059c 1547 doshell:
3280af22 1548 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1549 return FALSE;
1550 }
1551 }
748a9306 1552
3280af22 1553 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1554 PL_Cmd = savepvn(cmd, s-cmd);
1555 a = PL_Argv;
1556 for (s = PL_Cmd; *s;) {
99b89507 1557 while (*s && isSPACE(*s)) s++;
a687059c 1558 if (*s)
1559 *(a++) = s;
99b89507 1560 while (*s && !isSPACE(*s)) s++;
a687059c 1561 if (*s)
1562 *s++ = '\0';
1563 }
1564 *a = Nullch;
3280af22 1565 if (PL_Argv[0]) {
1566 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1567 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1568 do_execfree();
a687059c 1569 goto doshell;
b1248f16 1570 }
d008e5eb 1571 {
e446cec8 1572 int e = errno;
1573
d008e5eb 1574 if (ckWARN(WARN_EXEC))
9014280d 1575 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
d008e5eb 1576 PL_Argv[0], Strerror(errno));
e446cec8 1577 if (do_report) {
1578 PerlLIO_write(fd, (void*)&e, sizeof(int));
1579 PerlLIO_close(fd);
1580 }
d008e5eb 1581 }
a687059c 1582 }
ff8e2863 1583 do_execfree();
a687059c 1584 return FALSE;
1585}
1586
6890e559 1587#endif /* OS2 || WIN32 */
760ac839 1588
79072805 1589I32
864dbfa3 1590Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1591{
79072805 1592 register I32 val;
1593 register I32 val2;
1594 register I32 tot = 0;
20408e3c 1595 char *what;
a687059c 1596 char *s;
79072805 1597 SV **oldmark = mark;
2d8e6c8d 1598 STRLEN n_a;
a687059c 1599
20408e3c 1600#define APPLY_TAINT_PROPER() \
3280af22 1601 STMT_START { \
17406bd6 1602 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1603 } STMT_END
20408e3c 1604
1605 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1606 if (PL_tainting) {
463ee0b2 1607 while (++mark <= sp) {
bbce6d69 1608 if (SvTAINTED(*mark)) {
1609 TAINT;
1610 break;
1611 }
463ee0b2 1612 }
1613 mark = oldmark;
1614 }
a687059c 1615 switch (type) {
79072805 1616 case OP_CHMOD:
20408e3c 1617 what = "chmod";
1618 APPLY_TAINT_PROPER();
79072805 1619 if (++mark <= sp) {
463ee0b2 1620 val = SvIVx(*mark);
20408e3c 1621 APPLY_TAINT_PROPER();
1622 tot = sp - mark;
79072805 1623 while (++mark <= sp) {
2d8e6c8d 1624 char *name = SvPVx(*mark, n_a);
20408e3c 1625 APPLY_TAINT_PROPER();
1626 if (PerlLIO_chmod(name, val))
a687059c 1627 tot--;
1628 }
1629 }
1630 break;
fe14fcc3 1631#ifdef HAS_CHOWN
79072805 1632 case OP_CHOWN:
20408e3c 1633 what = "chown";
1634 APPLY_TAINT_PROPER();
79072805 1635 if (sp - mark > 2) {
463ee0b2 1636 val = SvIVx(*++mark);
1637 val2 = SvIVx(*++mark);
20408e3c 1638 APPLY_TAINT_PROPER();
a0d0e21e 1639 tot = sp - mark;
79072805 1640 while (++mark <= sp) {
2d8e6c8d 1641 char *name = SvPVx(*mark, n_a);
20408e3c 1642 APPLY_TAINT_PROPER();
36660982 1643 if (PerlLIO_chown(name, val, val2))
a687059c 1644 tot--;
1645 }
1646 }
1647 break;
b1248f16 1648#endif
a1d180c4 1649/*
dd64f1c3 1650XXX Should we make lchown() directly available from perl?
1651For now, we'll let Configure test for HAS_LCHOWN, but do
1652nothing in the core.
1653 --AD 5/1998
1654*/
fe14fcc3 1655#ifdef HAS_KILL
79072805 1656 case OP_KILL:
20408e3c 1657 what = "kill";
1658 APPLY_TAINT_PROPER();
55497cff 1659 if (mark == sp)
1660 break;
2d8e6c8d 1661 s = SvPVx(*++mark, n_a);
e02bfb16 1662 if (isALPHA(*s)) {
79072805 1663 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1664 s += 3;
e02bfb16 1665 if ((val = whichsig(s)) < 0)
cea2e8a9 1666 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805 1667 }
1668 else
463ee0b2 1669 val = SvIVx(*mark);
20408e3c 1670 APPLY_TAINT_PROPER();
1671 tot = sp - mark;
3595fcef 1672#ifdef VMS
1673 /* kill() doesn't do process groups (job trees?) under VMS */
1674 if (val < 0) val = -val;
1675 if (val == SIGKILL) {
1676# include <starlet.h>
1677 /* Use native sys$delprc() to insure that target process is
1678 * deleted; supervisor-mode images don't pay attention to
1679 * CRTL's emulation of Unix-style signals and kill()
1680 */
1681 while (++mark <= sp) {
1682 I32 proc = SvIVx(*mark);
1683 register unsigned long int __vmssts;
20408e3c 1684 APPLY_TAINT_PROPER();
3595fcef 1685 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1686 tot--;
1687 switch (__vmssts) {
1688 case SS$_NONEXPR:
1689 case SS$_NOSUCHNODE:
1690 SETERRNO(ESRCH,__vmssts);
1691 break;
1692 case SS$_NOPRIV:
1693 SETERRNO(EPERM,__vmssts);
1694 break;
1695 default:
1696 SETERRNO(EVMSERR,__vmssts);
1697 }
1698 }
1699 }
1700 break;
1701 }
1702#endif
79072805 1703 if (val < 0) {
1704 val = -val;
1705 while (++mark <= sp) {
463ee0b2 1706 I32 proc = SvIVx(*mark);
20408e3c 1707 APPLY_TAINT_PROPER();
fe14fcc3 1708#ifdef HAS_KILLPG
3028581b 1709 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1710#else
3028581b 1711 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1712#endif
79072805 1713 tot--;
a687059c 1714 }
79072805 1715 }
1716 else {
1717 while (++mark <= sp) {
20408e3c 1718 I32 proc = SvIVx(*mark);
1719 APPLY_TAINT_PROPER();
1720 if (PerlProc_kill(proc, val))
79072805 1721 tot--;
a687059c 1722 }
1723 }
1724 break;
b1248f16 1725#endif
79072805 1726 case OP_UNLINK:
20408e3c 1727 what = "unlink";
1728 APPLY_TAINT_PROPER();
79072805 1729 tot = sp - mark;
1730 while (++mark <= sp) {
2d8e6c8d 1731 s = SvPVx(*mark, n_a);
20408e3c 1732 APPLY_TAINT_PROPER();
3280af22 1733 if (PL_euid || PL_unsafe) {
a687059c 1734 if (UNLINK(s))
1735 tot--;
1736 }
1737 else { /* don't let root wipe out directories without -U */
3280af22 1738 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1739 tot--;
1740 else {
1741 if (UNLINK(s))
1742 tot--;
1743 }
1744 }
1745 }
1746 break;
a0d0e21e 1747#ifdef HAS_UTIME
79072805 1748 case OP_UTIME:
20408e3c 1749 what = "utime";
1750 APPLY_TAINT_PROPER();
79072805 1751 if (sp - mark > 2) {
748a9306 1752#if defined(I_UTIME) || defined(VMS)
663a0e37 1753 struct utimbuf utbuf;
1754#else
a687059c 1755 struct {
dd2821f6 1756 Time_t actime;
1757 Time_t modtime;
a687059c 1758 } utbuf;
663a0e37 1759#endif
a687059c 1760
c6f7b413 1761 SV* accessed = *++mark;
1762 SV* modified = *++mark;
1763 void * utbufp = &utbuf;
1764
6ec06612 1765 /* Be like C, and if both times are undefined, let the C
1766 * library figure out what to do. This usually means
1767 * "current time". */
c6f7b413 1768
1769 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
6ec06612 1770 utbufp = NULL;
1771 else {
1772 Zero(&utbuf, sizeof utbuf, char);
517844ec 1773#ifdef BIG_TIME
6ec06612 1774 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1775 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
517844ec 1776#else
6ec06612 1777 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1778 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
517844ec 1779#endif
6ec06612 1780 }
1781 APPLY_TAINT_PROPER();
79072805 1782 tot = sp - mark;
1783 while (++mark <= sp) {
2d8e6c8d 1784 char *name = SvPVx(*mark, n_a);
20408e3c 1785 APPLY_TAINT_PROPER();
c6f7b413 1786 if (PerlLIO_utime(name, utbufp))
a687059c 1787 tot--;
1788 }
a687059c 1789 }
1790 else
79072805 1791 tot = 0;
a687059c 1792 break;
a0d0e21e 1793#endif
a687059c 1794 }
1795 return tot;
20408e3c 1796
20408e3c 1797#undef APPLY_TAINT_PROPER
a687059c 1798}
1799
1800/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1801#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1802bool
1803Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1804/* Note: we use `effective' both for uids and gids.
1805 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1806{
bee1dbe2 1807#ifdef DOSISH
fe14fcc3 1808 /* [Comments and code from Len Reed]
1809 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1810 * to write-protected files. The execute permission bit is set
1811 * by the Miscrosoft C library stat() function for the following:
1812 * .exe files
1813 * .com files
1814 * .bat files
1815 * directories
1816 * All files and directories are readable.
1817 * Directories and special files, e.g. "CON", cannot be
1818 * write-protected.
1819 * [Comment by Tom Dinger -- a directory can have the write-protect
1820 * bit set in the file system, but DOS permits changes to
1821 * the directory anyway. In addition, all bets are off
1822 * here for networked software, such as Novell and
1823 * Sun's PC-NFS.]
1824 */
1825
bee1dbe2 1826 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1827 * too so it will actually look into the files for magic numbers
1828 */
7f4774ae 1829 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1830
55497cff 1831#else /* ! DOSISH */
3280af22 1832 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1833 if (mode == S_IXUSR) {
c623bd54 1834 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1835 return TRUE;
1836 }
1837 else
1838 return TRUE; /* root reads and writes anything */
1839 return FALSE;
1840 }
3280af22 1841 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1842 if (statbufp->st_mode & mode)
a687059c 1843 return TRUE; /* ok as "user" */
1844 }
d8eceb89 1845 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1846 if (statbufp->st_mode & mode >> 3)
a687059c 1847 return TRUE; /* ok as "group" */
1848 }
7f4774ae 1849 else if (statbufp->st_mode & mode >> 6)
a687059c 1850 return TRUE; /* ok as "other" */
1851 return FALSE;
55497cff 1852#endif /* ! DOSISH */
a687059c 1853}
a0d0e21e 1854#endif /* ! VMS */
a687059c 1855
d8eceb89 1856bool
1857Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1858{
cd39f2b6 1859#ifdef MACOS_TRADITIONAL
1860 /* This is simply not correct for AppleShare, but fix it yerself. */
1861 return TRUE;
1862#else
3280af22 1863 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1864 return TRUE;
fe14fcc3 1865#ifdef HAS_GETGROUPS
a687059c 1866#ifndef NGROUPS
1867#define NGROUPS 32
1868#endif
1869 {
a0d0e21e 1870 Groups_t gary[NGROUPS];
79072805 1871 I32 anum;
a687059c 1872
1873 anum = getgroups(NGROUPS,gary);
1874 while (--anum >= 0)
1875 if (gary[anum] == testgid)
1876 return TRUE;
1877 }
1878#endif
1879 return FALSE;
cd39f2b6 1880#endif
a687059c 1881}
c2ab57d4 1882
fe14fcc3 1883#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1884
79072805 1885I32
864dbfa3 1886Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1887{
c2ab57d4 1888 key_t key;
79072805 1889 I32 n, flags;
c2ab57d4 1890
463ee0b2 1891 key = (key_t)SvNVx(*++mark);
1892 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1893 flags = SvIVx(*++mark);
748a9306 1894 SETERRNO(0,0);
c2ab57d4 1895 switch (optype)
1896 {
fe14fcc3 1897#ifdef HAS_MSG
79072805 1898 case OP_MSGGET:
c2ab57d4 1899 return msgget(key, flags);
e5d73d77 1900#endif
fe14fcc3 1901#ifdef HAS_SEM
79072805 1902 case OP_SEMGET:
c2ab57d4 1903 return semget(key, n, flags);
e5d73d77 1904#endif
fe14fcc3 1905#ifdef HAS_SHM
79072805 1906 case OP_SHMGET:
c2ab57d4 1907 return shmget(key, n, flags);
e5d73d77 1908#endif
fe14fcc3 1909#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1910 default:
cea2e8a9 1911 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1912#endif
c2ab57d4 1913 }
1914 return -1; /* should never happen */
1915}
1916
79072805 1917I32
864dbfa3 1918Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1919{
79072805 1920 SV *astr;
c2ab57d4 1921 char *a;
a0d0e21e 1922 I32 id, n, cmd, infosize, getinfo;
1923 I32 ret = -1;
c2ab57d4 1924
463ee0b2 1925 id = SvIVx(*++mark);
1926 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1927 cmd = SvIVx(*++mark);
79072805 1928 astr = *++mark;
c2ab57d4 1929 infosize = 0;
1930 getinfo = (cmd == IPC_STAT);
1931
1932 switch (optype)
1933 {
fe14fcc3 1934#ifdef HAS_MSG
79072805 1935 case OP_MSGCTL:
c2ab57d4 1936 if (cmd == IPC_STAT || cmd == IPC_SET)
1937 infosize = sizeof(struct msqid_ds);
1938 break;
e5d73d77 1939#endif
fe14fcc3 1940#ifdef HAS_SHM
79072805 1941 case OP_SHMCTL:
c2ab57d4 1942 if (cmd == IPC_STAT || cmd == IPC_SET)
1943 infosize = sizeof(struct shmid_ds);
1944 break;
e5d73d77 1945#endif
fe14fcc3 1946#ifdef HAS_SEM
79072805 1947 case OP_SEMCTL:
39398f3f 1948#ifdef Semctl
c2ab57d4 1949 if (cmd == IPC_STAT || cmd == IPC_SET)
1950 infosize = sizeof(struct semid_ds);
1951 else if (cmd == GETALL || cmd == SETALL)
1952 {
8e591e46 1953 struct semid_ds semds;
bd89102f 1954 union semun semun;
e6f0bdd6 1955#ifdef EXTRA_F_IN_SEMUN_BUF
1956 semun.buff = &semds;
1957#else
84902520 1958 semun.buf = &semds;
e6f0bdd6 1959#endif
c2ab57d4 1960 getinfo = (cmd == GETALL);
9b89d93d 1961 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1962 return -1;
6e21c824 1963 infosize = semds.sem_nsems * sizeof(short);
1964 /* "short" is technically wrong but much more portable
1965 than guessing about u_?short(_t)? */
c2ab57d4 1966 }
39398f3f 1967#else
cea2e8a9 1968 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1969#endif
c2ab57d4 1970 break;
e5d73d77 1971#endif
fe14fcc3 1972#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1973 default:
cea2e8a9 1974 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1975#endif
c2ab57d4 1976 }
1977
1978 if (infosize)
1979 {
a0d0e21e 1980 STRLEN len;
c2ab57d4 1981 if (getinfo)
1982 {
a0d0e21e 1983 SvPV_force(astr, len);
1984 a = SvGROW(astr, infosize+1);
c2ab57d4 1985 }
1986 else
1987 {
463ee0b2 1988 a = SvPV(astr, len);
1989 if (len != infosize)
cea2e8a9 1990 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1991 PL_op_desc[optype],
1992 (unsigned long)len,
1993 (long)infosize);
c2ab57d4 1994 }
1995 }
1996 else
1997 {
c030ccd9 1998 IV i = SvIV(astr);
56431972 1999 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 2000 }
748a9306 2001 SETERRNO(0,0);
c2ab57d4 2002 switch (optype)
2003 {
fe14fcc3 2004#ifdef HAS_MSG
79072805 2005 case OP_MSGCTL:
bee1dbe2 2006 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 2007 break;
e5d73d77 2008#endif
fe14fcc3 2009#ifdef HAS_SEM
bd89102f 2010 case OP_SEMCTL: {
39398f3f 2011#ifdef Semctl
bd89102f 2012 union semun unsemds;
2013
e6f0bdd6 2014#ifdef EXTRA_F_IN_SEMUN_BUF
2015 unsemds.buff = (struct semid_ds *)a;
2016#else
bd89102f 2017 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 2018#endif
bd89102f 2019 ret = Semctl(id, n, cmd, unsemds);
39398f3f 2020#else
cea2e8a9 2021 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2022#endif
bd89102f 2023 }
c2ab57d4 2024 break;
e5d73d77 2025#endif
fe14fcc3 2026#ifdef HAS_SHM
79072805 2027 case OP_SHMCTL:
bee1dbe2 2028 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 2029 break;
e5d73d77 2030#endif
c2ab57d4 2031 }
2032 if (getinfo && ret >= 0) {
79072805 2033 SvCUR_set(astr, infosize);
2034 *SvEND(astr) = '\0';
a0d0e21e 2035 SvSETMAGIC(astr);
c2ab57d4 2036 }
2037 return ret;
2038}
2039
79072805 2040I32
864dbfa3 2041Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 2042{
fe14fcc3 2043#ifdef HAS_MSG
79072805 2044 SV *mstr;
c2ab57d4 2045 char *mbuf;
79072805 2046 I32 id, msize, flags;
463ee0b2 2047 STRLEN len;
c2ab57d4 2048
463ee0b2 2049 id = SvIVx(*++mark);
79072805 2050 mstr = *++mark;
463ee0b2 2051 flags = SvIVx(*++mark);
2052 mbuf = SvPV(mstr, len);
2053 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 2054 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 2055 SETERRNO(0,0);
bee1dbe2 2056 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 2057#else
cea2e8a9 2058 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 2059#endif
c2ab57d4 2060}
2061
79072805 2062I32
864dbfa3 2063Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 2064{
fe14fcc3 2065#ifdef HAS_MSG
79072805 2066 SV *mstr;
c2ab57d4 2067 char *mbuf;
2068 long mtype;
79072805 2069 I32 id, msize, flags, ret;
463ee0b2 2070 STRLEN len;
79072805 2071
463ee0b2 2072 id = SvIVx(*++mark);
79072805 2073 mstr = *++mark;
c2e66d9e 2074 /* suppress warning when reading into undef var --jhi */
2075 if (! SvOK(mstr))
2076 sv_setpvn(mstr, "", 0);
463ee0b2 2077 msize = SvIVx(*++mark);
2078 mtype = (long)SvIVx(*++mark);
2079 flags = SvIVx(*++mark);
a0d0e21e 2080 SvPV_force(mstr, len);
2081 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 2082
748a9306 2083 SETERRNO(0,0);
bee1dbe2 2084 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 2085 if (ret >= 0) {
79072805 2086 SvCUR_set(mstr, sizeof(long)+ret);
2087 *SvEND(mstr) = '\0';
41d6edb2 2088#ifndef INCOMPLETE_TAINTS
2089 /* who knows who has been playing with this message? */
2090 SvTAINTED_on(mstr);
2091#endif
c2ab57d4 2092 }
2093 return ret;
e5d73d77 2094#else
cea2e8a9 2095 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 2096#endif
c2ab57d4 2097}
2098
79072805 2099I32
864dbfa3 2100Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 2101{
fe14fcc3 2102#ifdef HAS_SEM
79072805 2103 SV *opstr;
c2ab57d4 2104 char *opbuf;
463ee0b2 2105 I32 id;
2106 STRLEN opsize;
c2ab57d4 2107
463ee0b2 2108 id = SvIVx(*++mark);
79072805 2109 opstr = *++mark;
463ee0b2 2110 opbuf = SvPV(opstr, opsize);
248ff010 2111 if (opsize < 3 * SHORTSIZE
2112 || (opsize % (3 * SHORTSIZE))) {
93189314 2113 SETERRNO(EINVAL,LIB_INVARG);
c2ab57d4 2114 return -1;
2115 }
748a9306 2116 SETERRNO(0,0);
248ff010 2117 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2118 {
2119 int nsops = opsize / (3 * sizeof (short));
2120 int i = nsops;
2121 short *ops = (short *) opbuf;
2122 short *o = ops;
2123 struct sembuf *temps, *t;
2124 I32 result;
2125
2126 New (0, temps, nsops, struct sembuf);
2127 t = temps;
2128 while (i--) {
2129 t->sem_num = *o++;
2130 t->sem_op = *o++;
2131 t->sem_flg = *o++;
2132 t++;
2133 }
2134 result = semop(id, temps, nsops);
2135 t = temps;
2136 o = ops;
2137 i = nsops;
2138 while (i--) {
2139 *o++ = t->sem_num;
2140 *o++ = t->sem_op;
2141 *o++ = t->sem_flg;
2142 t++;
2143 }
2144 Safefree(temps);
2145 return result;
2146 }
e5d73d77 2147#else
cea2e8a9 2148 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2149#endif
c2ab57d4 2150}
2151
79072805 2152I32
864dbfa3 2153Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2154{
fe14fcc3 2155#ifdef HAS_SHM
79072805 2156 SV *mstr;
c2ab57d4 2157 char *mbuf, *shm;
79072805 2158 I32 id, mpos, msize;
463ee0b2 2159 STRLEN len;
c2ab57d4 2160 struct shmid_ds shmds;
c2ab57d4 2161
463ee0b2 2162 id = SvIVx(*++mark);
79072805 2163 mstr = *++mark;
463ee0b2 2164 mpos = SvIVx(*++mark);
2165 msize = SvIVx(*++mark);
748a9306 2166 SETERRNO(0,0);
c2ab57d4 2167 if (shmctl(id, IPC_STAT, &shmds) == -1)
2168 return -1;
2169 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
93189314 2170 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
c2ab57d4 2171 return -1;
2172 }
8ac85365 2173 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 2174 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2175 return -1;
79072805 2176 if (optype == OP_SHMREAD) {
9f538c04 2177 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2178 if (! SvOK(mstr))
2179 sv_setpvn(mstr, "", 0);
a0d0e21e 2180 SvPV_force(mstr, len);
2181 mbuf = SvGROW(mstr, msize+1);
2182
bee1dbe2 2183 Copy(shm + mpos, mbuf, msize, char);
79072805 2184 SvCUR_set(mstr, msize);
2185 *SvEND(mstr) = '\0';
a0d0e21e 2186 SvSETMAGIC(mstr);
d929ce6f 2187#ifndef INCOMPLETE_TAINTS
2188 /* who knows who has been playing with this shared memory? */
2189 SvTAINTED_on(mstr);
2190#endif
c2ab57d4 2191 }
2192 else {
79072805 2193 I32 n;
c2ab57d4 2194
a0d0e21e 2195 mbuf = SvPV(mstr, len);
463ee0b2 2196 if ((n = len) > msize)
c2ab57d4 2197 n = msize;
bee1dbe2 2198 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2199 if (n < msize)
bee1dbe2 2200 memzero(shm + mpos + n, msize - n);
c2ab57d4 2201 }
2202 return shmdt(shm);
e5d73d77 2203#else
cea2e8a9 2204 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2205#endif
c2ab57d4 2206}
2207
fe14fcc3 2208#endif /* SYSV IPC */
4e35701f 2209
0d44d22b 2210/*
ccfc67b7 2211=head1 IO Functions
2212
0d44d22b 2213=for apidoc start_glob
2214
2215Function called by C<do_readline> to spawn a glob (or do the glob inside
2216perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
210b36aa 2217this glob starter is only used by miniperl during the build process.
0d44d22b 2218Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
fab3f3a7 2219
0d44d22b 2220=cut
2221*/
2222
2223PerlIO *
2224Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2225{
2226 SV *tmpcmd = NEWSV(55, 0);
2227 PerlIO *fp;
2228 ENTER;
2229 SAVEFREESV(tmpcmd);
2230#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2231 /* since spawning off a process is a real performance hit */
2232 {
2233#include <descrip.h>
2234#include <lib$routines.h>
2235#include <nam.h>
2236#include <rmsdef.h>
2237 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2238 char vmsspec[NAM$C_MAXRSS+1];
2239 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
0d44d22b 2240 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2241 PerlIO *tmpfp;
2242 STRLEN i;
2243 struct dsc$descriptor_s wilddsc
2244 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2245 struct dsc$descriptor_vs rsdsc
2246 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2247 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2248
2249 /* We could find out if there's an explicit dev/dir or version
2250 by peeking into lib$find_file's internal context at
2251 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2252 but that's unsupported, so I don't want to do it now and
2253 have it bite someone in the future. */
0d44d22b 2254 cp = SvPV(tmpglob,i);
2255 for (; i; i--) {
2256 if (cp[i] == ';') hasver = 1;
2257 if (cp[i] == '.') {
2258 if (sts) hasver = 1;
2259 else sts = 1;
2260 }
2261 if (cp[i] == '/') {
2262 hasdir = isunix = 1;
2263 break;
2264 }
2265 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2266 hasdir = 1;
2267 break;
2268 }
2269 }
a15cef0c 2270 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
0d44d22b 2271 Stat_t st;
2272 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2273 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2274 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2275 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
a3987cb8 2276 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2277 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
0d44d22b 2278 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2279 &dfltdsc,NULL,NULL,NULL))&1)) {
2280 end = rstr + (unsigned long int) *rslt;
2281 if (!hasver) while (*end != ';') end--;
2282 *(end++) = '\n'; *end = '\0';
2283 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2284 if (hasdir) {
2285 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2286 begin = rstr;
2287 }
2288 else {
2289 begin = end;
2290 while (*(--begin) != ']' && *begin != '>') ;
2291 ++begin;
2292 }
2293 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2294 }
2295 if (cxt) (void)lib$find_file_end(&cxt);
2296 if (ok && sts != RMS$_NMF &&
93189314 2297 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
0d44d22b 2298 if (!ok) {
2299 if (!(sts & 1)) {
2300 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2301 }
2302 PerlIO_close(tmpfp);
2303 fp = NULL;
2304 }
2305 else {
2306 PerlIO_rewind(tmpfp);
2307 IoTYPE(io) = IoTYPE_RDONLY;
2308 IoIFP(io) = fp = tmpfp;
2309 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2310 }
2311 }
2312 }
2313#else /* !VMS */
2314#ifdef MACOS_TRADITIONAL
2315 sv_setpv(tmpcmd, "glob ");
2316 sv_catsv(tmpcmd, tmpglob);
2317 sv_catpv(tmpcmd, " |");
2318#else
2319#ifdef DOSISH
2320#ifdef OS2
2321 sv_setpv(tmpcmd, "for a in ");
2322 sv_catsv(tmpcmd, tmpglob);
2323 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2324#else
2325#ifdef DJGPP
2326 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2327 sv_catsv(tmpcmd, tmpglob);
2328#else
2329 sv_setpv(tmpcmd, "perlglob ");
2330 sv_catsv(tmpcmd, tmpglob);
2331 sv_catpv(tmpcmd, " |");
2332#endif /* !DJGPP */
2333#endif /* !OS2 */
2334#else /* !DOSISH */
2335#if defined(CSH)
2336 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2337 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2338 sv_catsv(tmpcmd, tmpglob);
2339 sv_catpv(tmpcmd, "' 2>/dev/null |");
2340#else
2341 sv_setpv(tmpcmd, "echo ");
2342 sv_catsv(tmpcmd, tmpglob);
2343#if 'z' - 'a' == 25
2344 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2345#else
2346 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2347#endif
2348#endif /* !CSH */
2349#endif /* !DOSISH */
2350#endif /* MACOS_TRADITIONAL */
2351 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2352 FALSE, O_RDONLY, 0, Nullfp);
2353 fp = IoIFP(io);
2354#endif /* !VMS */
2355 LEAVE;
2356 return fp;
2357}