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