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