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