Move the init_tm() and mini_mktime() up from POSIX.xs to util.c
[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 }
ae798467 1207 else if (DO_UTF8(sv)) {
1208 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)) {
1209 Perl_warner(aTHX_ WARN_UTF8, "Wide character in print");
1210 }
1211 }
f9a63242 1212 tmps = SvPV(sv, len);
79072805 1213 break;
ff8e2863 1214 }
94e4c244 1215 /* To detect whether the process is about to overstep its
1216 * filesize limit we would need getrlimit(). We could then
1217 * also transparently raise the limit with setrlimit() --
1218 * but only until the system hard limit/the filesystem limit,
c5dd3cdd 1219 * at which we would get EPERM. Note that when using buffered
1220 * io the write failure can be delayed until the flush/close. --jhi */
a21ac455 1221 if (len && (PerlIO_write(fp,tmps,len) == 0))
a687059c 1222 return FALSE;
760ac839 1223 return !PerlIO_error(fp);
a687059c 1224}
1225
79072805 1226I32
cea2e8a9 1227Perl_my_stat(pTHX)
a687059c 1228{
39644a26 1229 dSP;
79072805 1230 IO *io;
2dd78f96 1231 GV* gv;
79072805 1232
533c011a 1233 if (PL_op->op_flags & OPf_REF) {
924508f0 1234 EXTEND(SP,1);
2dd78f96 1235 gv = cGVOP_gv;
748a9306 1236 do_fstat:
2dd78f96 1237 io = GvIO(gv);
8990e307 1238 if (io && IoIFP(io)) {
2dd78f96 1239 PL_statgv = gv;
3280af22 1240 sv_setpv(PL_statname,"");
1241 PL_laststype = OP_STAT;
1242 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 1243 }
1244 else {
2dd78f96 1245 if (gv == PL_defgv)
3280af22 1246 return PL_laststatval;
2dd78f96 1247 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1248 report_evil_fh(gv, io, PL_op->op_type);
3280af22 1249 PL_statgv = Nullgv;
1250 sv_setpv(PL_statname,"");
1251 return (PL_laststatval = -1);
a687059c 1252 }
1253 }
1254 else {
748a9306 1255 SV* sv = POPs;
4b74e3fb 1256 char *s;
2d8e6c8d 1257 STRLEN n_a;
79072805 1258 PUTBACK;
748a9306 1259 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 1260 gv = (GV*)sv;
748a9306 1261 goto do_fstat;
1262 }
1263 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 1264 gv = (GV*)SvRV(sv);
748a9306 1265 goto do_fstat;
1266 }
1267
2d8e6c8d 1268 s = SvPV(sv, n_a);
3280af22 1269 PL_statgv = Nullgv;
1270 sv_setpv(PL_statname, s);
1271 PL_laststype = OP_STAT;
1272 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1273 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
cea2e8a9 1274 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 1275 return PL_laststatval;
a687059c 1276 }
1277}
1278
79072805 1279I32
cea2e8a9 1280Perl_my_lstat(pTHX)
c623bd54 1281{
39644a26 1282 dSP;
79072805 1283 SV *sv;
2d8e6c8d 1284 STRLEN n_a;
533c011a 1285 if (PL_op->op_flags & OPf_REF) {
924508f0 1286 EXTEND(SP,1);
638eceb6 1287 if (cGVOP_gv == PL_defgv) {
3280af22 1288 if (PL_laststype != OP_LSTAT)
cea2e8a9 1289 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1290 return PL_laststatval;
fe14fcc3 1291 }
cea2e8a9 1292 Perl_croak(aTHX_ "You can't use -l on a filehandle");
fe14fcc3 1293 }
c623bd54 1294
3280af22 1295 PL_laststype = OP_LSTAT;
1296 PL_statgv = Nullgv;
79072805 1297 sv = POPs;
1298 PUTBACK;
2d8e6c8d 1299 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1300 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1301 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
cea2e8a9 1302 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1303 return PL_laststatval;
c623bd54 1304}
1305
a687059c 1306bool
864dbfa3 1307Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1308{
d5a9bfb0 1309 return do_aexec5(really, mark, sp, 0, 0);
1310}
1311
1312bool
2aa1486d 1313Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1314 int fd, int do_report)
d5a9bfb0 1315{
cd39f2b6 1316#ifdef MACOS_TRADITIONAL
1317 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1318#else
a687059c 1319 register char **a;
a687059c 1320 char *tmps;
2d8e6c8d 1321 STRLEN n_a;
a687059c 1322
79072805 1323 if (sp > mark) {
3280af22 1324 New(401,PL_Argv, sp - mark + 1, char*);
1325 a = PL_Argv;
79072805 1326 while (++mark <= sp) {
1327 if (*mark)
2d8e6c8d 1328 *a++ = SvPVx(*mark, n_a);
a687059c 1329 else
1330 *a++ = "";
1331 }
1332 *a = Nullch;
91b2752f 1333 if (really)
1334 tmps = SvPV(really, n_a);
1335 if ((!really && *PL_Argv[0] != '/') ||
1336 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1337 TAINT_ENV(); /* testing IFS here is overkill, probably */
91b2752f 1338 if (really && *tmps)
b4748376 1339 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1340 else
b4748376 1341 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
599cee73 1342 if (ckWARN(WARN_EXEC))
a1d180c4 1343 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
91b2752f 1344 (really ? tmps : PL_Argv[0]), Strerror(errno));
d5a9bfb0 1345 if (do_report) {
1346 int e = errno;
1347
1348 PerlLIO_write(fd, (void*)&e, sizeof(int));
1349 PerlLIO_close(fd);
1350 }
a687059c 1351 }
bee1dbe2 1352 do_execfree();
cd39f2b6 1353#endif
a687059c 1354 return FALSE;
1355}
1356
fe14fcc3 1357void
864dbfa3 1358Perl_do_execfree(pTHX)
ff8e2863 1359{
3280af22 1360 if (PL_Argv) {
1361 Safefree(PL_Argv);
1362 PL_Argv = Null(char **);
ff8e2863 1363 }
3280af22 1364 if (PL_Cmd) {
1365 Safefree(PL_Cmd);
1366 PL_Cmd = Nullch;
ff8e2863 1367 }
1368}
1369
cd39f2b6 1370#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1371
a687059c 1372bool
864dbfa3 1373Perl_do_exec(pTHX_ char *cmd)
a687059c 1374{
e446cec8 1375 return do_exec3(cmd,0,0);
1376}
1377
1378bool
864dbfa3 1379Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1380{
a687059c 1381 register char **a;
1382 register char *s;
a687059c 1383 char flags[10];
1384
748a9306 1385 while (*cmd && isSPACE(*cmd))
1386 cmd++;
1387
a687059c 1388 /* save an extra exec if possible */
1389
bf38876a 1390#ifdef CSH
3280af22 1391 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1392 strcpy(flags,"-c");
3280af22 1393 s = cmd+PL_cshlen+3;
a687059c 1394 if (*s == 'f') {
1395 s++;
1396 strcat(flags,"f");
1397 }
1398 if (*s == ' ')
1399 s++;
1400 if (*s++ == '\'') {
1401 char *ncmd = s;
1402
1403 while (*s)
1404 s++;
1405 if (s[-1] == '\n')
1406 *--s = '\0';
1407 if (s[-1] == '\'') {
1408 *--s = '\0';
3280af22 1409 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c 1410 *s = '\'';
1411 return FALSE;
1412 }
1413 }
1414 }
bf38876a 1415#endif /* CSH */
a687059c 1416
1417 /* see if there are shell metacharacters in it */
1418
748a9306 1419 if (*cmd == '.' && isSPACE(cmd[1]))
1420 goto doshell;
1421
1422 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1423 goto doshell;
1424
c170e444 1425 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1426 if (*s == '=')
1427 goto doshell;
748a9306 1428
a687059c 1429 for (s = cmd; *s; s++) {
93a17b20 1430 if (*s != ' ' && !isALPHA(*s) && 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
afd9f252 1663 Zero(&utbuf, sizeof utbuf, char);
517844ec 1664#ifdef BIG_TIME
dd2821f6 1665 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1666 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1667#else
dd2821f6 1668 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1669 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1670#endif
20408e3c 1671 APPLY_TAINT_PROPER();
79072805 1672 tot = sp - mark;
1673 while (++mark <= sp) {
2d8e6c8d 1674 char *name = SvPVx(*mark, n_a);
20408e3c 1675 APPLY_TAINT_PROPER();
1676 if (PerlLIO_utime(name, &utbuf))
a687059c 1677 tot--;
1678 }
a687059c 1679 }
1680 else
79072805 1681 tot = 0;
a687059c 1682 break;
a0d0e21e 1683#endif
a687059c 1684 }
1685 return tot;
20408e3c 1686
20408e3c 1687#undef APPLY_TAINT_PROPER
a687059c 1688}
1689
1690/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1691#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1692bool
1693Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1694/* Note: we use `effective' both for uids and gids.
1695 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1696{
bee1dbe2 1697#ifdef DOSISH
fe14fcc3 1698 /* [Comments and code from Len Reed]
1699 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1700 * to write-protected files. The execute permission bit is set
1701 * by the Miscrosoft C library stat() function for the following:
1702 * .exe files
1703 * .com files
1704 * .bat files
1705 * directories
1706 * All files and directories are readable.
1707 * Directories and special files, e.g. "CON", cannot be
1708 * write-protected.
1709 * [Comment by Tom Dinger -- a directory can have the write-protect
1710 * bit set in the file system, but DOS permits changes to
1711 * the directory anyway. In addition, all bets are off
1712 * here for networked software, such as Novell and
1713 * Sun's PC-NFS.]
1714 */
1715
bee1dbe2 1716 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1717 * too so it will actually look into the files for magic numbers
1718 */
7f4774ae 1719 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1720
55497cff 1721#else /* ! DOSISH */
3280af22 1722 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1723 if (mode == S_IXUSR) {
c623bd54 1724 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1725 return TRUE;
1726 }
1727 else
1728 return TRUE; /* root reads and writes anything */
1729 return FALSE;
1730 }
3280af22 1731 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1732 if (statbufp->st_mode & mode)
a687059c 1733 return TRUE; /* ok as "user" */
1734 }
d8eceb89 1735 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1736 if (statbufp->st_mode & mode >> 3)
a687059c 1737 return TRUE; /* ok as "group" */
1738 }
7f4774ae 1739 else if (statbufp->st_mode & mode >> 6)
a687059c 1740 return TRUE; /* ok as "other" */
1741 return FALSE;
55497cff 1742#endif /* ! DOSISH */
a687059c 1743}
a0d0e21e 1744#endif /* ! VMS */
a687059c 1745
d8eceb89 1746bool
1747Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1748{
cd39f2b6 1749#ifdef MACOS_TRADITIONAL
1750 /* This is simply not correct for AppleShare, but fix it yerself. */
1751 return TRUE;
1752#else
3280af22 1753 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1754 return TRUE;
fe14fcc3 1755#ifdef HAS_GETGROUPS
a687059c 1756#ifndef NGROUPS
1757#define NGROUPS 32
1758#endif
1759 {
a0d0e21e 1760 Groups_t gary[NGROUPS];
79072805 1761 I32 anum;
a687059c 1762
1763 anum = getgroups(NGROUPS,gary);
1764 while (--anum >= 0)
1765 if (gary[anum] == testgid)
1766 return TRUE;
1767 }
1768#endif
1769 return FALSE;
cd39f2b6 1770#endif
a687059c 1771}
c2ab57d4 1772
fe14fcc3 1773#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1774
79072805 1775I32
864dbfa3 1776Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1777{
c2ab57d4 1778 key_t key;
79072805 1779 I32 n, flags;
c2ab57d4 1780
463ee0b2 1781 key = (key_t)SvNVx(*++mark);
1782 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1783 flags = SvIVx(*++mark);
748a9306 1784 SETERRNO(0,0);
c2ab57d4 1785 switch (optype)
1786 {
fe14fcc3 1787#ifdef HAS_MSG
79072805 1788 case OP_MSGGET:
c2ab57d4 1789 return msgget(key, flags);
e5d73d77 1790#endif
fe14fcc3 1791#ifdef HAS_SEM
79072805 1792 case OP_SEMGET:
c2ab57d4 1793 return semget(key, n, flags);
e5d73d77 1794#endif
fe14fcc3 1795#ifdef HAS_SHM
79072805 1796 case OP_SHMGET:
c2ab57d4 1797 return shmget(key, n, flags);
e5d73d77 1798#endif
fe14fcc3 1799#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1800 default:
cea2e8a9 1801 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1802#endif
c2ab57d4 1803 }
1804 return -1; /* should never happen */
1805}
1806
79072805 1807I32
864dbfa3 1808Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1809{
79072805 1810 SV *astr;
c2ab57d4 1811 char *a;
a0d0e21e 1812 I32 id, n, cmd, infosize, getinfo;
1813 I32 ret = -1;
c2ab57d4 1814
463ee0b2 1815 id = SvIVx(*++mark);
1816 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1817 cmd = SvIVx(*++mark);
79072805 1818 astr = *++mark;
c2ab57d4 1819 infosize = 0;
1820 getinfo = (cmd == IPC_STAT);
1821
1822 switch (optype)
1823 {
fe14fcc3 1824#ifdef HAS_MSG
79072805 1825 case OP_MSGCTL:
c2ab57d4 1826 if (cmd == IPC_STAT || cmd == IPC_SET)
1827 infosize = sizeof(struct msqid_ds);
1828 break;
e5d73d77 1829#endif
fe14fcc3 1830#ifdef HAS_SHM
79072805 1831 case OP_SHMCTL:
c2ab57d4 1832 if (cmd == IPC_STAT || cmd == IPC_SET)
1833 infosize = sizeof(struct shmid_ds);
1834 break;
e5d73d77 1835#endif
fe14fcc3 1836#ifdef HAS_SEM
79072805 1837 case OP_SEMCTL:
39398f3f 1838#ifdef Semctl
c2ab57d4 1839 if (cmd == IPC_STAT || cmd == IPC_SET)
1840 infosize = sizeof(struct semid_ds);
1841 else if (cmd == GETALL || cmd == SETALL)
1842 {
8e591e46 1843 struct semid_ds semds;
bd89102f 1844 union semun semun;
e6f0bdd6 1845#ifdef EXTRA_F_IN_SEMUN_BUF
1846 semun.buff = &semds;
1847#else
84902520 1848 semun.buf = &semds;
e6f0bdd6 1849#endif
c2ab57d4 1850 getinfo = (cmd == GETALL);
9b89d93d 1851 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1852 return -1;
6e21c824 1853 infosize = semds.sem_nsems * sizeof(short);
1854 /* "short" is technically wrong but much more portable
1855 than guessing about u_?short(_t)? */
c2ab57d4 1856 }
39398f3f 1857#else
cea2e8a9 1858 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1859#endif
c2ab57d4 1860 break;
e5d73d77 1861#endif
fe14fcc3 1862#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1863 default:
cea2e8a9 1864 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1865#endif
c2ab57d4 1866 }
1867
1868 if (infosize)
1869 {
a0d0e21e 1870 STRLEN len;
c2ab57d4 1871 if (getinfo)
1872 {
a0d0e21e 1873 SvPV_force(astr, len);
1874 a = SvGROW(astr, infosize+1);
c2ab57d4 1875 }
1876 else
1877 {
463ee0b2 1878 a = SvPV(astr, len);
1879 if (len != infosize)
cea2e8a9 1880 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1881 PL_op_desc[optype],
1882 (unsigned long)len,
1883 (long)infosize);
c2ab57d4 1884 }
1885 }
1886 else
1887 {
c030ccd9 1888 IV i = SvIV(astr);
56431972 1889 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 1890 }
748a9306 1891 SETERRNO(0,0);
c2ab57d4 1892 switch (optype)
1893 {
fe14fcc3 1894#ifdef HAS_MSG
79072805 1895 case OP_MSGCTL:
bee1dbe2 1896 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1897 break;
e5d73d77 1898#endif
fe14fcc3 1899#ifdef HAS_SEM
bd89102f 1900 case OP_SEMCTL: {
39398f3f 1901#ifdef Semctl
bd89102f 1902 union semun unsemds;
1903
e6f0bdd6 1904#ifdef EXTRA_F_IN_SEMUN_BUF
1905 unsemds.buff = (struct semid_ds *)a;
1906#else
bd89102f 1907 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 1908#endif
bd89102f 1909 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1910#else
cea2e8a9 1911 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1912#endif
bd89102f 1913 }
c2ab57d4 1914 break;
e5d73d77 1915#endif
fe14fcc3 1916#ifdef HAS_SHM
79072805 1917 case OP_SHMCTL:
bee1dbe2 1918 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1919 break;
e5d73d77 1920#endif
c2ab57d4 1921 }
1922 if (getinfo && ret >= 0) {
79072805 1923 SvCUR_set(astr, infosize);
1924 *SvEND(astr) = '\0';
a0d0e21e 1925 SvSETMAGIC(astr);
c2ab57d4 1926 }
1927 return ret;
1928}
1929
79072805 1930I32
864dbfa3 1931Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 1932{
fe14fcc3 1933#ifdef HAS_MSG
79072805 1934 SV *mstr;
c2ab57d4 1935 char *mbuf;
79072805 1936 I32 id, msize, flags;
463ee0b2 1937 STRLEN len;
c2ab57d4 1938
463ee0b2 1939 id = SvIVx(*++mark);
79072805 1940 mstr = *++mark;
463ee0b2 1941 flags = SvIVx(*++mark);
1942 mbuf = SvPV(mstr, len);
1943 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 1944 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 1945 SETERRNO(0,0);
bee1dbe2 1946 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1947#else
cea2e8a9 1948 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 1949#endif
c2ab57d4 1950}
1951
79072805 1952I32
864dbfa3 1953Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 1954{
fe14fcc3 1955#ifdef HAS_MSG
79072805 1956 SV *mstr;
c2ab57d4 1957 char *mbuf;
1958 long mtype;
79072805 1959 I32 id, msize, flags, ret;
463ee0b2 1960 STRLEN len;
79072805 1961
463ee0b2 1962 id = SvIVx(*++mark);
79072805 1963 mstr = *++mark;
c2e66d9e 1964 /* suppress warning when reading into undef var --jhi */
1965 if (! SvOK(mstr))
1966 sv_setpvn(mstr, "", 0);
463ee0b2 1967 msize = SvIVx(*++mark);
1968 mtype = (long)SvIVx(*++mark);
1969 flags = SvIVx(*++mark);
a0d0e21e 1970 SvPV_force(mstr, len);
1971 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 1972
748a9306 1973 SETERRNO(0,0);
bee1dbe2 1974 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1975 if (ret >= 0) {
79072805 1976 SvCUR_set(mstr, sizeof(long)+ret);
1977 *SvEND(mstr) = '\0';
41d6edb2 1978#ifndef INCOMPLETE_TAINTS
1979 /* who knows who has been playing with this message? */
1980 SvTAINTED_on(mstr);
1981#endif
c2ab57d4 1982 }
1983 return ret;
e5d73d77 1984#else
cea2e8a9 1985 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 1986#endif
c2ab57d4 1987}
1988
79072805 1989I32
864dbfa3 1990Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 1991{
fe14fcc3 1992#ifdef HAS_SEM
79072805 1993 SV *opstr;
c2ab57d4 1994 char *opbuf;
463ee0b2 1995 I32 id;
1996 STRLEN opsize;
c2ab57d4 1997
463ee0b2 1998 id = SvIVx(*++mark);
79072805 1999 opstr = *++mark;
463ee0b2 2000 opbuf = SvPV(opstr, opsize);
c2ab57d4 2001 if (opsize < sizeof(struct sembuf)
2002 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 2003 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 2004 return -1;
2005 }
748a9306 2006 SETERRNO(0,0);
6e21c824 2007 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 2008#else
cea2e8a9 2009 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2010#endif
c2ab57d4 2011}
2012
79072805 2013I32
864dbfa3 2014Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2015{
fe14fcc3 2016#ifdef HAS_SHM
79072805 2017 SV *mstr;
c2ab57d4 2018 char *mbuf, *shm;
79072805 2019 I32 id, mpos, msize;
463ee0b2 2020 STRLEN len;
c2ab57d4 2021 struct shmid_ds shmds;
c2ab57d4 2022
463ee0b2 2023 id = SvIVx(*++mark);
79072805 2024 mstr = *++mark;
463ee0b2 2025 mpos = SvIVx(*++mark);
2026 msize = SvIVx(*++mark);
748a9306 2027 SETERRNO(0,0);
c2ab57d4 2028 if (shmctl(id, IPC_STAT, &shmds) == -1)
2029 return -1;
2030 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 2031 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 2032 return -1;
2033 }
8ac85365 2034 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 2035 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2036 return -1;
79072805 2037 if (optype == OP_SHMREAD) {
9f538c04 2038 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2039 if (! SvOK(mstr))
2040 sv_setpvn(mstr, "", 0);
a0d0e21e 2041 SvPV_force(mstr, len);
2042 mbuf = SvGROW(mstr, msize+1);
2043
bee1dbe2 2044 Copy(shm + mpos, mbuf, msize, char);
79072805 2045 SvCUR_set(mstr, msize);
2046 *SvEND(mstr) = '\0';
a0d0e21e 2047 SvSETMAGIC(mstr);
d929ce6f 2048#ifndef INCOMPLETE_TAINTS
2049 /* who knows who has been playing with this shared memory? */
2050 SvTAINTED_on(mstr);
2051#endif
c2ab57d4 2052 }
2053 else {
79072805 2054 I32 n;
c2ab57d4 2055
a0d0e21e 2056 mbuf = SvPV(mstr, len);
463ee0b2 2057 if ((n = len) > msize)
c2ab57d4 2058 n = msize;
bee1dbe2 2059 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2060 if (n < msize)
bee1dbe2 2061 memzero(shm + mpos + n, msize - n);
c2ab57d4 2062 }
2063 return shmdt(shm);
e5d73d77 2064#else
cea2e8a9 2065 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2066#endif
c2ab57d4 2067}
2068
fe14fcc3 2069#endif /* SYSV IPC */
4e35701f 2070
0d44d22b 2071/*
2072=for apidoc start_glob
2073
2074Function called by C<do_readline> to spawn a glob (or do the glob inside
2075perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
2076this glob starter is only used by miniperl during the build proccess.
2077Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
fab3f3a7 2078
0d44d22b 2079=cut
2080*/
2081
2082PerlIO *
2083Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2084{
2085 SV *tmpcmd = NEWSV(55, 0);
2086 PerlIO *fp;
2087 ENTER;
2088 SAVEFREESV(tmpcmd);
2089#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2090 /* since spawning off a process is a real performance hit */
2091 {
2092#include <descrip.h>
2093#include <lib$routines.h>
2094#include <nam.h>
2095#include <rmsdef.h>
2096 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2097 char vmsspec[NAM$C_MAXRSS+1];
2098 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
2099 char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
2100 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2101 PerlIO *tmpfp;
2102 STRLEN i;
2103 struct dsc$descriptor_s wilddsc
2104 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2105 struct dsc$descriptor_vs rsdsc
2106 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2107 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2108
2109 /* We could find out if there's an explicit dev/dir or version
2110 by peeking into lib$find_file's internal context at
2111 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2112 but that's unsupported, so I don't want to do it now and
2113 have it bite someone in the future. */
2114 strcat(tmpfnam,PerlLIO_tmpnam(NULL));
2115 cp = SvPV(tmpglob,i);
2116 for (; i; i--) {
2117 if (cp[i] == ';') hasver = 1;
2118 if (cp[i] == '.') {
2119 if (sts) hasver = 1;
2120 else sts = 1;
2121 }
2122 if (cp[i] == '/') {
2123 hasdir = isunix = 1;
2124 break;
2125 }
2126 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2127 hasdir = 1;
2128 break;
2129 }
2130 }
2131 if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
2132 Stat_t st;
2133 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2134 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2135 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2136 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
2137 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2138 &dfltdsc,NULL,NULL,NULL))&1)) {
2139 end = rstr + (unsigned long int) *rslt;
2140 if (!hasver) while (*end != ';') end--;
2141 *(end++) = '\n'; *end = '\0';
2142 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2143 if (hasdir) {
2144 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2145 begin = rstr;
2146 }
2147 else {
2148 begin = end;
2149 while (*(--begin) != ']' && *begin != '>') ;
2150 ++begin;
2151 }
2152 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2153 }
2154 if (cxt) (void)lib$find_file_end(&cxt);
2155 if (ok && sts != RMS$_NMF &&
2156 sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
2157 if (!ok) {
2158 if (!(sts & 1)) {
2159 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2160 }
2161 PerlIO_close(tmpfp);
2162 fp = NULL;
2163 }
2164 else {
2165 PerlIO_rewind(tmpfp);
2166 IoTYPE(io) = IoTYPE_RDONLY;
2167 IoIFP(io) = fp = tmpfp;
2168 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2169 }
2170 }
2171 }
2172#else /* !VMS */
2173#ifdef MACOS_TRADITIONAL
2174 sv_setpv(tmpcmd, "glob ");
2175 sv_catsv(tmpcmd, tmpglob);
2176 sv_catpv(tmpcmd, " |");
2177#else
2178#ifdef DOSISH
2179#ifdef OS2
2180 sv_setpv(tmpcmd, "for a in ");
2181 sv_catsv(tmpcmd, tmpglob);
2182 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2183#else
2184#ifdef DJGPP
2185 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2186 sv_catsv(tmpcmd, tmpglob);
2187#else
2188 sv_setpv(tmpcmd, "perlglob ");
2189 sv_catsv(tmpcmd, tmpglob);
2190 sv_catpv(tmpcmd, " |");
2191#endif /* !DJGPP */
2192#endif /* !OS2 */
2193#else /* !DOSISH */
2194#if defined(CSH)
2195 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2196 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2197 sv_catsv(tmpcmd, tmpglob);
2198 sv_catpv(tmpcmd, "' 2>/dev/null |");
2199#else
2200 sv_setpv(tmpcmd, "echo ");
2201 sv_catsv(tmpcmd, tmpglob);
2202#if 'z' - 'a' == 25
2203 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2204#else
2205 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2206#endif
2207#endif /* !CSH */
2208#endif /* !DOSISH */
2209#endif /* MACOS_TRADITIONAL */
2210 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2211 FALSE, O_RDONLY, 0, Nullfp);
2212 fp = IoIFP(io);
2213#endif /* !VMS */
2214 LEAVE;
2215 return fp;
2216}