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