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