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