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