Bad $? assumptions.
[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 */
6ce75a77 1110 char *name = ":raw";
35990314 1111#ifdef PERLIO_USING_CRLF
8b24e160 1112 if (!(mode & O_BINARY))
6ce75a77 1113 name = ":crlf";
1114#endif
60382766 1115 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
6ff81951 1116}
1117
a0d0e21e 1118#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
c2ab57d4 1119 /* code courtesy of William Kucharski */
fe14fcc3 1120#define HAS_CHSIZE
6eb13c3b 1121
517844ec 1122I32 my_chsize(fd, length)
79072805 1123I32 fd; /* file descriptor */
85e6fe83 1124Off_t length; /* length to set file to */
6eb13c3b 1125{
6eb13c3b 1126 struct flock fl;
1127 struct stat filebuf;
1128
3028581b 1129 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b 1130 return -1;
1131
1132 if (filebuf.st_size < length) {
1133
1134 /* extend file length */
1135
3028581b 1136 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b 1137 return -1;
1138
1139 /* write a "0" byte */
1140
3028581b 1141 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b 1142 return -1;
1143 }
1144 else {
1145 /* truncate length */
1146
1147 fl.l_whence = 0;
1148 fl.l_len = 0;
1149 fl.l_start = length;
a0d0e21e 1150 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b 1151
1152 /*
a0d0e21e 1153 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b 1154 * fcntl(2), which truncates the file so that it ends at the
1155 * position indicated by fl.l_start.
1156 *
1157 * Will minor miracles never cease?
1158 */
1159
a0d0e21e 1160 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b 1161 return -1;
1162
1163 }
1164
1165 return 0;
1166}
a0d0e21e 1167#endif /* F_FREESP */
ff8e2863 1168
a687059c 1169bool
864dbfa3 1170Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
a687059c 1171{
1172 register char *tmps;
463ee0b2 1173 STRLEN len;
a687059c 1174
79072805 1175 /* assuming fp is checked earlier */
1176 if (!sv)
1177 return TRUE;
3280af22 1178 if (PL_ofmt) {
8990e307 1179 if (SvGMAGICAL(sv))
79072805 1180 mg_get(sv);
463ee0b2 1181 if (SvIOK(sv) && SvIVX(sv) != 0) {
65202027 1182 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
760ac839 1183 return !PerlIO_error(fp);
79072805 1184 }
463ee0b2 1185 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
79072805 1186 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
3280af22 1187 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
760ac839 1188 return !PerlIO_error(fp);
79072805 1189 }
a687059c 1190 }
79072805 1191 switch (SvTYPE(sv)) {
1192 case SVt_NULL:
411caa50 1193 if (ckWARN(WARN_UNINITIALIZED))
1194 report_uninit();
ff8e2863 1195 return TRUE;
79072805 1196 case SVt_IV:
a0d0e21e 1197 if (SvIOK(sv)) {
1198 if (SvGMAGICAL(sv))
1199 mg_get(sv);
cf2093f6 1200 if (SvIsUV(sv))
57def98f 1201 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
cf2093f6 1202 else
57def98f 1203 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
760ac839 1204 return !PerlIO_error(fp);
a0d0e21e 1205 }
1206 /* FALL THROUGH */
79072805 1207 default:
7d59b7e4 1208 if (PerlIO_isutf8(fp)) {
f9a63242 1209 if (!SvUTF8(sv))
1210 sv_utf8_upgrade(sv = sv_mortalcopy(sv));
7d59b7e4 1211 }
ae798467 1212 else if (DO_UTF8(sv)) {
2da16753 1213 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
1214 && ckWARN(WARN_UTF8))
1215 {
ae798467 1216 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1217 }
1218 }
f9a63242 1219 tmps = SvPV(sv, len);
79072805 1220 break;
ff8e2863 1221 }
94e4c244 1222 /* To detect whether the process is about to overstep its
1223 * filesize limit we would need getrlimit(). We could then
1224 * also transparently raise the limit with setrlimit() --
1225 * but only until the system hard limit/the filesystem limit,
c5dd3cdd 1226 * at which we would get EPERM. Note that when using buffered
1227 * io the write failure can be delayed until the flush/close. --jhi */
a21ac455 1228 if (len && (PerlIO_write(fp,tmps,len) == 0))
a687059c 1229 return FALSE;
760ac839 1230 return !PerlIO_error(fp);
a687059c 1231}
1232
79072805 1233I32
cea2e8a9 1234Perl_my_stat(pTHX)
a687059c 1235{
39644a26 1236 dSP;
79072805 1237 IO *io;
2dd78f96 1238 GV* gv;
79072805 1239
533c011a 1240 if (PL_op->op_flags & OPf_REF) {
924508f0 1241 EXTEND(SP,1);
2dd78f96 1242 gv = cGVOP_gv;
748a9306 1243 do_fstat:
2dd78f96 1244 io = GvIO(gv);
8990e307 1245 if (io && IoIFP(io)) {
2dd78f96 1246 PL_statgv = gv;
3280af22 1247 sv_setpv(PL_statname,"");
1248 PL_laststype = OP_STAT;
1249 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 1250 }
1251 else {
2dd78f96 1252 if (gv == PL_defgv)
3280af22 1253 return PL_laststatval;
2dd78f96 1254 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1255 report_evil_fh(gv, io, PL_op->op_type);
3280af22 1256 PL_statgv = Nullgv;
1257 sv_setpv(PL_statname,"");
1258 return (PL_laststatval = -1);
a687059c 1259 }
1260 }
1261 else {
748a9306 1262 SV* sv = POPs;
4b74e3fb 1263 char *s;
2d8e6c8d 1264 STRLEN n_a;
79072805 1265 PUTBACK;
748a9306 1266 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 1267 gv = (GV*)sv;
748a9306 1268 goto do_fstat;
1269 }
1270 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 1271 gv = (GV*)SvRV(sv);
748a9306 1272 goto do_fstat;
1273 }
1274
2d8e6c8d 1275 s = SvPV(sv, n_a);
3280af22 1276 PL_statgv = Nullgv;
1277 sv_setpv(PL_statname, s);
1278 PL_laststype = OP_STAT;
1279 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1280 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
cea2e8a9 1281 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 1282 return PL_laststatval;
a687059c 1283 }
1284}
1285
79072805 1286I32
cea2e8a9 1287Perl_my_lstat(pTHX)
c623bd54 1288{
39644a26 1289 dSP;
79072805 1290 SV *sv;
2d8e6c8d 1291 STRLEN n_a;
533c011a 1292 if (PL_op->op_flags & OPf_REF) {
924508f0 1293 EXTEND(SP,1);
638eceb6 1294 if (cGVOP_gv == PL_defgv) {
3280af22 1295 if (PL_laststype != OP_LSTAT)
cea2e8a9 1296 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1297 return PL_laststatval;
fe14fcc3 1298 }
cea2e8a9 1299 Perl_croak(aTHX_ "You can't use -l on a filehandle");
fe14fcc3 1300 }
c623bd54 1301
3280af22 1302 PL_laststype = OP_LSTAT;
1303 PL_statgv = Nullgv;
79072805 1304 sv = POPs;
1305 PUTBACK;
2d8e6c8d 1306 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1307 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1308 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
cea2e8a9 1309 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1310 return PL_laststatval;
c623bd54 1311}
1312
a687059c 1313bool
864dbfa3 1314Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1315{
d5a9bfb0 1316 return do_aexec5(really, mark, sp, 0, 0);
1317}
1318
1319bool
2aa1486d 1320Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1321 int fd, int do_report)
d5a9bfb0 1322{
cd39f2b6 1323#ifdef MACOS_TRADITIONAL
1324 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1325#else
a687059c 1326 register char **a;
9c5ffd7c 1327 char *tmps = Nullch;
2d8e6c8d 1328 STRLEN n_a;
a687059c 1329
79072805 1330 if (sp > mark) {
3280af22 1331 New(401,PL_Argv, sp - mark + 1, char*);
1332 a = PL_Argv;
79072805 1333 while (++mark <= sp) {
1334 if (*mark)
2d8e6c8d 1335 *a++ = SvPVx(*mark, n_a);
a687059c 1336 else
1337 *a++ = "";
1338 }
1339 *a = Nullch;
91b2752f 1340 if (really)
1341 tmps = SvPV(really, n_a);
1342 if ((!really && *PL_Argv[0] != '/') ||
1343 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1344 TAINT_ENV(); /* testing IFS here is overkill, probably */
91b2752f 1345 if (really && *tmps)
b4748376 1346 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1347 else
b4748376 1348 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
599cee73 1349 if (ckWARN(WARN_EXEC))
a1d180c4 1350 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
91b2752f 1351 (really ? tmps : PL_Argv[0]), Strerror(errno));
d5a9bfb0 1352 if (do_report) {
1353 int e = errno;
1354
1355 PerlLIO_write(fd, (void*)&e, sizeof(int));
1356 PerlLIO_close(fd);
1357 }
a687059c 1358 }
bee1dbe2 1359 do_execfree();
cd39f2b6 1360#endif
a687059c 1361 return FALSE;
1362}
1363
fe14fcc3 1364void
864dbfa3 1365Perl_do_execfree(pTHX)
ff8e2863 1366{
3280af22 1367 if (PL_Argv) {
1368 Safefree(PL_Argv);
1369 PL_Argv = Null(char **);
ff8e2863 1370 }
3280af22 1371 if (PL_Cmd) {
1372 Safefree(PL_Cmd);
1373 PL_Cmd = Nullch;
ff8e2863 1374 }
1375}
1376
cd39f2b6 1377#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1378
a687059c 1379bool
864dbfa3 1380Perl_do_exec(pTHX_ char *cmd)
a687059c 1381{
e446cec8 1382 return do_exec3(cmd,0,0);
1383}
1384
1385bool
864dbfa3 1386Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1387{
a687059c 1388 register char **a;
1389 register char *s;
a687059c 1390
748a9306 1391 while (*cmd && isSPACE(*cmd))
1392 cmd++;
1393
a687059c 1394 /* save an extra exec if possible */
1395
bf38876a 1396#ifdef CSH
d05c1ba0 1397 {
1398 char flags[10];
1399 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1400 strnEQ(cmd+PL_cshlen," -c",3)) {
1401 strcpy(flags,"-c");
1402 s = cmd+PL_cshlen+3;
1403 if (*s == 'f') {
1404 s++;
1405 strcat(flags,"f");
1406 }
1407 if (*s == ' ')
1408 s++;
1409 if (*s++ == '\'') {
1410 char *ncmd = s;
1411
1412 while (*s)
1413 s++;
1414 if (s[-1] == '\n')
1415 *--s = '\0';
1416 if (s[-1] == '\'') {
1417 *--s = '\0';
1418 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1419 *s = '\'';
1420 return FALSE;
1421 }
1422 }
a687059c 1423 }
1424 }
bf38876a 1425#endif /* CSH */
a687059c 1426
1427 /* see if there are shell metacharacters in it */
1428
748a9306 1429 if (*cmd == '.' && isSPACE(cmd[1]))
1430 goto doshell;
1431
1432 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1433 goto doshell;
1434
c170e444 1435 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1436 if (*s == '=')
1437 goto doshell;
748a9306 1438
a687059c 1439 for (s = cmd; *s; s++) {
d05c1ba0 1440 if (*s != ' ' && !isALPHA(*s) &&
1441 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1442 if (*s == '\n' && !s[1]) {
1443 *s = '\0';
1444 break;
1445 }
603a98b0 1446 /* handle the 2>&1 construct at the end */
1447 if (*s == '>' && s[1] == '&' && s[2] == '1'
1448 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1449 && (!s[3] || isSPACE(s[3])))
1450 {
1451 char *t = s + 3;
1452
1453 while (*t && isSPACE(*t))
1454 ++t;
1455 if (!*t && (dup2(1,2) != -1)) {
1456 s[-2] = '\0';
1457 break;
1458 }
1459 }
a687059c 1460 doshell:
3280af22 1461 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1462 return FALSE;
1463 }
1464 }
748a9306 1465
3280af22 1466 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1467 PL_Cmd = savepvn(cmd, s-cmd);
1468 a = PL_Argv;
1469 for (s = PL_Cmd; *s;) {
99b89507 1470 while (*s && isSPACE(*s)) s++;
a687059c 1471 if (*s)
1472 *(a++) = s;
99b89507 1473 while (*s && !isSPACE(*s)) s++;
a687059c 1474 if (*s)
1475 *s++ = '\0';
1476 }
1477 *a = Nullch;
3280af22 1478 if (PL_Argv[0]) {
1479 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1480 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1481 do_execfree();
a687059c 1482 goto doshell;
b1248f16 1483 }
d008e5eb 1484 {
e446cec8 1485 int e = errno;
1486
d008e5eb 1487 if (ckWARN(WARN_EXEC))
a1d180c4 1488 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
d008e5eb 1489 PL_Argv[0], Strerror(errno));
e446cec8 1490 if (do_report) {
1491 PerlLIO_write(fd, (void*)&e, sizeof(int));
1492 PerlLIO_close(fd);
1493 }
d008e5eb 1494 }
a687059c 1495 }
ff8e2863 1496 do_execfree();
a687059c 1497 return FALSE;
1498}
1499
6890e559 1500#endif /* OS2 || WIN32 */
760ac839 1501
79072805 1502I32
864dbfa3 1503Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1504{
79072805 1505 register I32 val;
1506 register I32 val2;
1507 register I32 tot = 0;
20408e3c 1508 char *what;
a687059c 1509 char *s;
79072805 1510 SV **oldmark = mark;
2d8e6c8d 1511 STRLEN n_a;
a687059c 1512
20408e3c 1513#define APPLY_TAINT_PROPER() \
3280af22 1514 STMT_START { \
17406bd6 1515 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1516 } STMT_END
20408e3c 1517
1518 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1519 if (PL_tainting) {
463ee0b2 1520 while (++mark <= sp) {
bbce6d69 1521 if (SvTAINTED(*mark)) {
1522 TAINT;
1523 break;
1524 }
463ee0b2 1525 }
1526 mark = oldmark;
1527 }
a687059c 1528 switch (type) {
79072805 1529 case OP_CHMOD:
20408e3c 1530 what = "chmod";
1531 APPLY_TAINT_PROPER();
79072805 1532 if (++mark <= sp) {
463ee0b2 1533 val = SvIVx(*mark);
20408e3c 1534 APPLY_TAINT_PROPER();
1535 tot = sp - mark;
79072805 1536 while (++mark <= sp) {
2d8e6c8d 1537 char *name = SvPVx(*mark, n_a);
20408e3c 1538 APPLY_TAINT_PROPER();
1539 if (PerlLIO_chmod(name, val))
a687059c 1540 tot--;
1541 }
1542 }
1543 break;
fe14fcc3 1544#ifdef HAS_CHOWN
79072805 1545 case OP_CHOWN:
20408e3c 1546 what = "chown";
1547 APPLY_TAINT_PROPER();
79072805 1548 if (sp - mark > 2) {
463ee0b2 1549 val = SvIVx(*++mark);
1550 val2 = SvIVx(*++mark);
20408e3c 1551 APPLY_TAINT_PROPER();
a0d0e21e 1552 tot = sp - mark;
79072805 1553 while (++mark <= sp) {
2d8e6c8d 1554 char *name = SvPVx(*mark, n_a);
20408e3c 1555 APPLY_TAINT_PROPER();
36660982 1556 if (PerlLIO_chown(name, val, val2))
a687059c 1557 tot--;
1558 }
1559 }
1560 break;
b1248f16 1561#endif
a1d180c4 1562/*
dd64f1c3 1563XXX Should we make lchown() directly available from perl?
1564For now, we'll let Configure test for HAS_LCHOWN, but do
1565nothing in the core.
1566 --AD 5/1998
1567*/
fe14fcc3 1568#ifdef HAS_KILL
79072805 1569 case OP_KILL:
20408e3c 1570 what = "kill";
1571 APPLY_TAINT_PROPER();
55497cff 1572 if (mark == sp)
1573 break;
2d8e6c8d 1574 s = SvPVx(*++mark, n_a);
79072805 1575 if (isUPPER(*s)) {
1576 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1577 s += 3;
1578 if (!(val = whichsig(s)))
cea2e8a9 1579 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805 1580 }
1581 else
463ee0b2 1582 val = SvIVx(*mark);
20408e3c 1583 APPLY_TAINT_PROPER();
1584 tot = sp - mark;
3595fcef 1585#ifdef VMS
1586 /* kill() doesn't do process groups (job trees?) under VMS */
1587 if (val < 0) val = -val;
1588 if (val == SIGKILL) {
1589# include <starlet.h>
1590 /* Use native sys$delprc() to insure that target process is
1591 * deleted; supervisor-mode images don't pay attention to
1592 * CRTL's emulation of Unix-style signals and kill()
1593 */
1594 while (++mark <= sp) {
1595 I32 proc = SvIVx(*mark);
1596 register unsigned long int __vmssts;
20408e3c 1597 APPLY_TAINT_PROPER();
3595fcef 1598 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1599 tot--;
1600 switch (__vmssts) {
1601 case SS$_NONEXPR:
1602 case SS$_NOSUCHNODE:
1603 SETERRNO(ESRCH,__vmssts);
1604 break;
1605 case SS$_NOPRIV:
1606 SETERRNO(EPERM,__vmssts);
1607 break;
1608 default:
1609 SETERRNO(EVMSERR,__vmssts);
1610 }
1611 }
1612 }
1613 break;
1614 }
1615#endif
79072805 1616 if (val < 0) {
1617 val = -val;
1618 while (++mark <= sp) {
463ee0b2 1619 I32 proc = SvIVx(*mark);
20408e3c 1620 APPLY_TAINT_PROPER();
fe14fcc3 1621#ifdef HAS_KILLPG
3028581b 1622 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1623#else
3028581b 1624 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1625#endif
79072805 1626 tot--;
a687059c 1627 }
79072805 1628 }
1629 else {
1630 while (++mark <= sp) {
20408e3c 1631 I32 proc = SvIVx(*mark);
1632 APPLY_TAINT_PROPER();
1633 if (PerlProc_kill(proc, val))
79072805 1634 tot--;
a687059c 1635 }
1636 }
1637 break;
b1248f16 1638#endif
79072805 1639 case OP_UNLINK:
20408e3c 1640 what = "unlink";
1641 APPLY_TAINT_PROPER();
79072805 1642 tot = sp - mark;
1643 while (++mark <= sp) {
2d8e6c8d 1644 s = SvPVx(*mark, n_a);
20408e3c 1645 APPLY_TAINT_PROPER();
3280af22 1646 if (PL_euid || PL_unsafe) {
a687059c 1647 if (UNLINK(s))
1648 tot--;
1649 }
1650 else { /* don't let root wipe out directories without -U */
3280af22 1651 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1652 tot--;
1653 else {
1654 if (UNLINK(s))
1655 tot--;
1656 }
1657 }
1658 }
1659 break;
a0d0e21e 1660#ifdef HAS_UTIME
79072805 1661 case OP_UTIME:
20408e3c 1662 what = "utime";
1663 APPLY_TAINT_PROPER();
79072805 1664 if (sp - mark > 2) {
748a9306 1665#if defined(I_UTIME) || defined(VMS)
663a0e37 1666 struct utimbuf utbuf;
1667#else
a687059c 1668 struct {
dd2821f6 1669 Time_t actime;
1670 Time_t modtime;
a687059c 1671 } utbuf;
663a0e37 1672#endif
a687059c 1673
c6f7b413 1674 SV* accessed = *++mark;
1675 SV* modified = *++mark;
1676 void * utbufp = &utbuf;
1677
1678 /* be like C, and if both times are undefined, let the C
1679 library figure out what to do. This usually means
1680 "current time" */
1681
1682 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1683 utbufp = NULL;
06c7082d 1684
afd9f252 1685 Zero(&utbuf, sizeof utbuf, char);
517844ec 1686#ifdef BIG_TIME
c6f7b413 1687 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1688 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
517844ec 1689#else
c6f7b413 1690 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1691 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
517844ec 1692#endif
20408e3c 1693 APPLY_TAINT_PROPER();
79072805 1694 tot = sp - mark;
1695 while (++mark <= sp) {
2d8e6c8d 1696 char *name = SvPVx(*mark, n_a);
20408e3c 1697 APPLY_TAINT_PROPER();
c6f7b413 1698 if (PerlLIO_utime(name, utbufp))
a687059c 1699 tot--;
1700 }
a687059c 1701 }
1702 else
79072805 1703 tot = 0;
a687059c 1704 break;
a0d0e21e 1705#endif
a687059c 1706 }
1707 return tot;
20408e3c 1708
20408e3c 1709#undef APPLY_TAINT_PROPER
a687059c 1710}
1711
1712/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1713#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1714bool
1715Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1716/* Note: we use `effective' both for uids and gids.
1717 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1718{
bee1dbe2 1719#ifdef DOSISH
fe14fcc3 1720 /* [Comments and code from Len Reed]
1721 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1722 * to write-protected files. The execute permission bit is set
1723 * by the Miscrosoft C library stat() function for the following:
1724 * .exe files
1725 * .com files
1726 * .bat files
1727 * directories
1728 * All files and directories are readable.
1729 * Directories and special files, e.g. "CON", cannot be
1730 * write-protected.
1731 * [Comment by Tom Dinger -- a directory can have the write-protect
1732 * bit set in the file system, but DOS permits changes to
1733 * the directory anyway. In addition, all bets are off
1734 * here for networked software, such as Novell and
1735 * Sun's PC-NFS.]
1736 */
1737
bee1dbe2 1738 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1739 * too so it will actually look into the files for magic numbers
1740 */
7f4774ae 1741 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1742
55497cff 1743#else /* ! DOSISH */
3280af22 1744 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1745 if (mode == S_IXUSR) {
c623bd54 1746 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1747 return TRUE;
1748 }
1749 else
1750 return TRUE; /* root reads and writes anything */
1751 return FALSE;
1752 }
3280af22 1753 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1754 if (statbufp->st_mode & mode)
a687059c 1755 return TRUE; /* ok as "user" */
1756 }
d8eceb89 1757 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1758 if (statbufp->st_mode & mode >> 3)
a687059c 1759 return TRUE; /* ok as "group" */
1760 }
7f4774ae 1761 else if (statbufp->st_mode & mode >> 6)
a687059c 1762 return TRUE; /* ok as "other" */
1763 return FALSE;
55497cff 1764#endif /* ! DOSISH */
a687059c 1765}
a0d0e21e 1766#endif /* ! VMS */
a687059c 1767
d8eceb89 1768bool
1769Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1770{
cd39f2b6 1771#ifdef MACOS_TRADITIONAL
1772 /* This is simply not correct for AppleShare, but fix it yerself. */
1773 return TRUE;
1774#else
3280af22 1775 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1776 return TRUE;
fe14fcc3 1777#ifdef HAS_GETGROUPS
a687059c 1778#ifndef NGROUPS
1779#define NGROUPS 32
1780#endif
1781 {
a0d0e21e 1782 Groups_t gary[NGROUPS];
79072805 1783 I32 anum;
a687059c 1784
1785 anum = getgroups(NGROUPS,gary);
1786 while (--anum >= 0)
1787 if (gary[anum] == testgid)
1788 return TRUE;
1789 }
1790#endif
1791 return FALSE;
cd39f2b6 1792#endif
a687059c 1793}
c2ab57d4 1794
fe14fcc3 1795#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1796
79072805 1797I32
864dbfa3 1798Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1799{
c2ab57d4 1800 key_t key;
79072805 1801 I32 n, flags;
c2ab57d4 1802
463ee0b2 1803 key = (key_t)SvNVx(*++mark);
1804 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1805 flags = SvIVx(*++mark);
748a9306 1806 SETERRNO(0,0);
c2ab57d4 1807 switch (optype)
1808 {
fe14fcc3 1809#ifdef HAS_MSG
79072805 1810 case OP_MSGGET:
c2ab57d4 1811 return msgget(key, flags);
e5d73d77 1812#endif
fe14fcc3 1813#ifdef HAS_SEM
79072805 1814 case OP_SEMGET:
c2ab57d4 1815 return semget(key, n, flags);
e5d73d77 1816#endif
fe14fcc3 1817#ifdef HAS_SHM
79072805 1818 case OP_SHMGET:
c2ab57d4 1819 return shmget(key, n, flags);
e5d73d77 1820#endif
fe14fcc3 1821#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1822 default:
cea2e8a9 1823 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1824#endif
c2ab57d4 1825 }
1826 return -1; /* should never happen */
1827}
1828
79072805 1829I32
864dbfa3 1830Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1831{
79072805 1832 SV *astr;
c2ab57d4 1833 char *a;
a0d0e21e 1834 I32 id, n, cmd, infosize, getinfo;
1835 I32 ret = -1;
c2ab57d4 1836
463ee0b2 1837 id = SvIVx(*++mark);
1838 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1839 cmd = SvIVx(*++mark);
79072805 1840 astr = *++mark;
c2ab57d4 1841 infosize = 0;
1842 getinfo = (cmd == IPC_STAT);
1843
1844 switch (optype)
1845 {
fe14fcc3 1846#ifdef HAS_MSG
79072805 1847 case OP_MSGCTL:
c2ab57d4 1848 if (cmd == IPC_STAT || cmd == IPC_SET)
1849 infosize = sizeof(struct msqid_ds);
1850 break;
e5d73d77 1851#endif
fe14fcc3 1852#ifdef HAS_SHM
79072805 1853 case OP_SHMCTL:
c2ab57d4 1854 if (cmd == IPC_STAT || cmd == IPC_SET)
1855 infosize = sizeof(struct shmid_ds);
1856 break;
e5d73d77 1857#endif
fe14fcc3 1858#ifdef HAS_SEM
79072805 1859 case OP_SEMCTL:
39398f3f 1860#ifdef Semctl
c2ab57d4 1861 if (cmd == IPC_STAT || cmd == IPC_SET)
1862 infosize = sizeof(struct semid_ds);
1863 else if (cmd == GETALL || cmd == SETALL)
1864 {
8e591e46 1865 struct semid_ds semds;
bd89102f 1866 union semun semun;
e6f0bdd6 1867#ifdef EXTRA_F_IN_SEMUN_BUF
1868 semun.buff = &semds;
1869#else
84902520 1870 semun.buf = &semds;
e6f0bdd6 1871#endif
c2ab57d4 1872 getinfo = (cmd == GETALL);
9b89d93d 1873 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1874 return -1;
6e21c824 1875 infosize = semds.sem_nsems * sizeof(short);
1876 /* "short" is technically wrong but much more portable
1877 than guessing about u_?short(_t)? */
c2ab57d4 1878 }
39398f3f 1879#else
cea2e8a9 1880 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1881#endif
c2ab57d4 1882 break;
e5d73d77 1883#endif
fe14fcc3 1884#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1885 default:
cea2e8a9 1886 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1887#endif
c2ab57d4 1888 }
1889
1890 if (infosize)
1891 {
a0d0e21e 1892 STRLEN len;
c2ab57d4 1893 if (getinfo)
1894 {
a0d0e21e 1895 SvPV_force(astr, len);
1896 a = SvGROW(astr, infosize+1);
c2ab57d4 1897 }
1898 else
1899 {
463ee0b2 1900 a = SvPV(astr, len);
1901 if (len != infosize)
cea2e8a9 1902 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1903 PL_op_desc[optype],
1904 (unsigned long)len,
1905 (long)infosize);
c2ab57d4 1906 }
1907 }
1908 else
1909 {
c030ccd9 1910 IV i = SvIV(astr);
56431972 1911 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 1912 }
748a9306 1913 SETERRNO(0,0);
c2ab57d4 1914 switch (optype)
1915 {
fe14fcc3 1916#ifdef HAS_MSG
79072805 1917 case OP_MSGCTL:
bee1dbe2 1918 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1919 break;
e5d73d77 1920#endif
fe14fcc3 1921#ifdef HAS_SEM
bd89102f 1922 case OP_SEMCTL: {
39398f3f 1923#ifdef Semctl
bd89102f 1924 union semun unsemds;
1925
e6f0bdd6 1926#ifdef EXTRA_F_IN_SEMUN_BUF
1927 unsemds.buff = (struct semid_ds *)a;
1928#else
bd89102f 1929 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 1930#endif
bd89102f 1931 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1932#else
cea2e8a9 1933 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1934#endif
bd89102f 1935 }
c2ab57d4 1936 break;
e5d73d77 1937#endif
fe14fcc3 1938#ifdef HAS_SHM
79072805 1939 case OP_SHMCTL:
bee1dbe2 1940 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1941 break;
e5d73d77 1942#endif
c2ab57d4 1943 }
1944 if (getinfo && ret >= 0) {
79072805 1945 SvCUR_set(astr, infosize);
1946 *SvEND(astr) = '\0';
a0d0e21e 1947 SvSETMAGIC(astr);
c2ab57d4 1948 }
1949 return ret;
1950}
1951
79072805 1952I32
864dbfa3 1953Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 1954{
fe14fcc3 1955#ifdef HAS_MSG
79072805 1956 SV *mstr;
c2ab57d4 1957 char *mbuf;
79072805 1958 I32 id, msize, flags;
463ee0b2 1959 STRLEN len;
c2ab57d4 1960
463ee0b2 1961 id = SvIVx(*++mark);
79072805 1962 mstr = *++mark;
463ee0b2 1963 flags = SvIVx(*++mark);
1964 mbuf = SvPV(mstr, len);
1965 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 1966 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 1967 SETERRNO(0,0);
bee1dbe2 1968 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1969#else
cea2e8a9 1970 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 1971#endif
c2ab57d4 1972}
1973
79072805 1974I32
864dbfa3 1975Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 1976{
fe14fcc3 1977#ifdef HAS_MSG
79072805 1978 SV *mstr;
c2ab57d4 1979 char *mbuf;
1980 long mtype;
79072805 1981 I32 id, msize, flags, ret;
463ee0b2 1982 STRLEN len;
79072805 1983
463ee0b2 1984 id = SvIVx(*++mark);
79072805 1985 mstr = *++mark;
c2e66d9e 1986 /* suppress warning when reading into undef var --jhi */
1987 if (! SvOK(mstr))
1988 sv_setpvn(mstr, "", 0);
463ee0b2 1989 msize = SvIVx(*++mark);
1990 mtype = (long)SvIVx(*++mark);
1991 flags = SvIVx(*++mark);
a0d0e21e 1992 SvPV_force(mstr, len);
1993 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 1994
748a9306 1995 SETERRNO(0,0);
bee1dbe2 1996 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1997 if (ret >= 0) {
79072805 1998 SvCUR_set(mstr, sizeof(long)+ret);
1999 *SvEND(mstr) = '\0';
41d6edb2 2000#ifndef INCOMPLETE_TAINTS
2001 /* who knows who has been playing with this message? */
2002 SvTAINTED_on(mstr);
2003#endif
c2ab57d4 2004 }
2005 return ret;
e5d73d77 2006#else
cea2e8a9 2007 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 2008#endif
c2ab57d4 2009}
2010
79072805 2011I32
864dbfa3 2012Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 2013{
fe14fcc3 2014#ifdef HAS_SEM
79072805 2015 SV *opstr;
c2ab57d4 2016 char *opbuf;
463ee0b2 2017 I32 id;
2018 STRLEN opsize;
c2ab57d4 2019
463ee0b2 2020 id = SvIVx(*++mark);
79072805 2021 opstr = *++mark;
463ee0b2 2022 opbuf = SvPV(opstr, opsize);
248ff010 2023 if (opsize < 3 * SHORTSIZE
2024 || (opsize % (3 * SHORTSIZE))) {
748a9306 2025 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 2026 return -1;
2027 }
748a9306 2028 SETERRNO(0,0);
248ff010 2029 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2030 {
2031 int nsops = opsize / (3 * sizeof (short));
2032 int i = nsops;
2033 short *ops = (short *) opbuf;
2034 short *o = ops;
2035 struct sembuf *temps, *t;
2036 I32 result;
2037
2038 New (0, temps, nsops, struct sembuf);
2039 t = temps;
2040 while (i--) {
2041 t->sem_num = *o++;
2042 t->sem_op = *o++;
2043 t->sem_flg = *o++;
2044 t++;
2045 }
2046 result = semop(id, temps, nsops);
2047 t = temps;
2048 o = ops;
2049 i = nsops;
2050 while (i--) {
2051 *o++ = t->sem_num;
2052 *o++ = t->sem_op;
2053 *o++ = t->sem_flg;
2054 t++;
2055 }
2056 Safefree(temps);
2057 return result;
2058 }
e5d73d77 2059#else
cea2e8a9 2060 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2061#endif
c2ab57d4 2062}
2063
79072805 2064I32
864dbfa3 2065Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2066{
fe14fcc3 2067#ifdef HAS_SHM
79072805 2068 SV *mstr;
c2ab57d4 2069 char *mbuf, *shm;
79072805 2070 I32 id, mpos, msize;
463ee0b2 2071 STRLEN len;
c2ab57d4 2072 struct shmid_ds shmds;
c2ab57d4 2073
463ee0b2 2074 id = SvIVx(*++mark);
79072805 2075 mstr = *++mark;
463ee0b2 2076 mpos = SvIVx(*++mark);
2077 msize = SvIVx(*++mark);
748a9306 2078 SETERRNO(0,0);
c2ab57d4 2079 if (shmctl(id, IPC_STAT, &shmds) == -1)
2080 return -1;
2081 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 2082 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 2083 return -1;
2084 }
8ac85365 2085 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 2086 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2087 return -1;
79072805 2088 if (optype == OP_SHMREAD) {
9f538c04 2089 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2090 if (! SvOK(mstr))
2091 sv_setpvn(mstr, "", 0);
a0d0e21e 2092 SvPV_force(mstr, len);
2093 mbuf = SvGROW(mstr, msize+1);
2094
bee1dbe2 2095 Copy(shm + mpos, mbuf, msize, char);
79072805 2096 SvCUR_set(mstr, msize);
2097 *SvEND(mstr) = '\0';
a0d0e21e 2098 SvSETMAGIC(mstr);
d929ce6f 2099#ifndef INCOMPLETE_TAINTS
2100 /* who knows who has been playing with this shared memory? */
2101 SvTAINTED_on(mstr);
2102#endif
c2ab57d4 2103 }
2104 else {
79072805 2105 I32 n;
c2ab57d4 2106
a0d0e21e 2107 mbuf = SvPV(mstr, len);
463ee0b2 2108 if ((n = len) > msize)
c2ab57d4 2109 n = msize;
bee1dbe2 2110 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2111 if (n < msize)
bee1dbe2 2112 memzero(shm + mpos + n, msize - n);
c2ab57d4 2113 }
2114 return shmdt(shm);
e5d73d77 2115#else
cea2e8a9 2116 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2117#endif
c2ab57d4 2118}
2119
fe14fcc3 2120#endif /* SYSV IPC */
4e35701f 2121
0d44d22b 2122/*
2123=for apidoc start_glob
2124
2125Function called by C<do_readline> to spawn a glob (or do the glob inside
2126perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
210b36aa 2127this glob starter is only used by miniperl during the build process.
0d44d22b 2128Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
fab3f3a7 2129
0d44d22b 2130=cut
2131*/
2132
2133PerlIO *
2134Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2135{
2136 SV *tmpcmd = NEWSV(55, 0);
2137 PerlIO *fp;
2138 ENTER;
2139 SAVEFREESV(tmpcmd);
2140#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2141 /* since spawning off a process is a real performance hit */
2142 {
2143#include <descrip.h>
2144#include <lib$routines.h>
2145#include <nam.h>
2146#include <rmsdef.h>
2147 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2148 char vmsspec[NAM$C_MAXRSS+1];
2149 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
0d44d22b 2150 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2151 PerlIO *tmpfp;
2152 STRLEN i;
2153 struct dsc$descriptor_s wilddsc
2154 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2155 struct dsc$descriptor_vs rsdsc
2156 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2157 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2158
2159 /* We could find out if there's an explicit dev/dir or version
2160 by peeking into lib$find_file's internal context at
2161 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2162 but that's unsupported, so I don't want to do it now and
2163 have it bite someone in the future. */
0d44d22b 2164 cp = SvPV(tmpglob,i);
2165 for (; i; i--) {
2166 if (cp[i] == ';') hasver = 1;
2167 if (cp[i] == '.') {
2168 if (sts) hasver = 1;
2169 else sts = 1;
2170 }
2171 if (cp[i] == '/') {
2172 hasdir = isunix = 1;
2173 break;
2174 }
2175 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2176 hasdir = 1;
2177 break;
2178 }
2179 }
a15cef0c 2180 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
0d44d22b 2181 Stat_t st;
2182 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2183 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2184 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2185 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
a3987cb8 2186 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2187 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
0d44d22b 2188 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2189 &dfltdsc,NULL,NULL,NULL))&1)) {
2190 end = rstr + (unsigned long int) *rslt;
2191 if (!hasver) while (*end != ';') end--;
2192 *(end++) = '\n'; *end = '\0';
2193 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2194 if (hasdir) {
2195 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2196 begin = rstr;
2197 }
2198 else {
2199 begin = end;
2200 while (*(--begin) != ']' && *begin != '>') ;
2201 ++begin;
2202 }
2203 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2204 }
2205 if (cxt) (void)lib$find_file_end(&cxt);
2206 if (ok && sts != RMS$_NMF &&
2207 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2208 if (!ok) {
2209 if (!(sts & 1)) {
2210 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2211 }
2212 PerlIO_close(tmpfp);
2213 fp = NULL;
2214 }
2215 else {
2216 PerlIO_rewind(tmpfp);
2217 IoTYPE(io) = IoTYPE_RDONLY;
2218 IoIFP(io) = fp = tmpfp;
2219 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2220 }
2221 }
2222 }
2223#else /* !VMS */
2224#ifdef MACOS_TRADITIONAL
2225 sv_setpv(tmpcmd, "glob ");
2226 sv_catsv(tmpcmd, tmpglob);
2227 sv_catpv(tmpcmd, " |");
2228#else
2229#ifdef DOSISH
2230#ifdef OS2
2231 sv_setpv(tmpcmd, "for a in ");
2232 sv_catsv(tmpcmd, tmpglob);
2233 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2234#else
2235#ifdef DJGPP
2236 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2237 sv_catsv(tmpcmd, tmpglob);
2238#else
2239 sv_setpv(tmpcmd, "perlglob ");
2240 sv_catsv(tmpcmd, tmpglob);
2241 sv_catpv(tmpcmd, " |");
2242#endif /* !DJGPP */
2243#endif /* !OS2 */
2244#else /* !DOSISH */
2245#if defined(CSH)
2246 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2247 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2248 sv_catsv(tmpcmd, tmpglob);
2249 sv_catpv(tmpcmd, "' 2>/dev/null |");
2250#else
2251 sv_setpv(tmpcmd, "echo ");
2252 sv_catsv(tmpcmd, tmpglob);
2253#if 'z' - 'a' == 25
2254 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2255#else
2256 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2257#endif
2258#endif /* !CSH */
2259#endif /* !DOSISH */
2260#endif /* MACOS_TRADITIONAL */
2261 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2262 FALSE, O_RDONLY, 0, Nullfp);
2263 fp = IoIFP(io);
2264#endif /* !VMS */
2265 LEAVE;
2266 return fp;
2267}