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