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