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