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