This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add gv_fetchpvs, which uses STR_WITH_LEN to call gv_fetchpvn_flags.
[perl5.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 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
166f8a29
DM
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
0c19750d
SP
56#define PERL_MODE_MAX 8
57#define PERL_FLAGS_MAX 10
58
76121258 59#include <signal.h>
76121258 60
a687059c 61bool
2fbb330f 62Perl_do_openn(pTHX_ GV *gv, register const char *oname, I32 len, int as_raw,
a567e93b
NIS
63 int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
64 I32 num_svs)
65{
27da23d5 66 dVAR;
890ce7af 67 register IO * const io = GvIOn(gv);
760ac839
LW
68 PerlIO *saveifp = Nullfp;
69 PerlIO *saveofp = Nullfp;
ee518936 70 int savefd = -1;
9f37169a 71 char savetype = IoTYPE_CLOSED;
c07a80fd 72 int writing = 0;
760ac839 73 PerlIO *fp;
c07a80fd
PP
74 int fd;
75 int result;
3500f679 76 bool was_fdopen = FALSE;
16fe6d59 77 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
b931b1d9 78 char *type = NULL;
0c19750d 79 char mode[PERL_MODE_MAX]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
ee518936 80 SV *namesv;
a687059c 81
b931b1d9 82 Zero(mode,sizeof(mode),char);
3280af22 83 PL_forkprocess = 1; /* assume true if no fork */
c07a80fd 84
b931b1d9 85 /* Collect default raw/crlf info from the op */
16fe6d59 86 if (PL_op && PL_op->op_type == OP_OPEN) {
c2be40b1 87 /* set up IO layers */
b464bac0 88 const U8 flags = PL_op->op_private;
16fe6d59
GS
89 in_raw = (flags & OPpOPEN_IN_RAW);
90 in_crlf = (flags & OPpOPEN_IN_CRLF);
91 out_raw = (flags & OPpOPEN_OUT_RAW);
92 out_crlf = (flags & OPpOPEN_OUT_CRLF);
93 }
94
b931b1d9 95 /* If currently open - close before we re-open */
a0d0e21e 96 if (IoIFP(io)) {
760ac839 97 fd = PerlIO_fileno(IoIFP(io));
ee518936
NIS
98 if (IoTYPE(io) == IoTYPE_STD) {
99 /* This is a clone of one of STD* handles */
c2ab57d4 100 result = 0;
ee518936
NIS
101 }
102 else if (fd >= 0 && fd <= PL_maxsysfd) {
103 /* This is one of the original STD* handles */
104 saveifp = IoIFP(io);
105 saveofp = IoOFP(io);
8990e307 106 savetype = IoTYPE(io);
ee518936
NIS
107 savefd = fd;
108 result = 0;
6e21c824 109 }
50952442 110 else if (IoTYPE(io) == IoTYPE_PIPE)
3028581b 111 result = PerlProc_pclose(IoIFP(io));
8990e307
LW
112 else if (IoIFP(io) != IoOFP(io)) {
113 if (IoOFP(io)) {
760ac839 114 result = PerlIO_close(IoOFP(io));
6170680b 115 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4
LW
116 }
117 else
760ac839 118 result = PerlIO_close(IoIFP(io));
a687059c 119 }
a687059c 120 else
760ac839 121 result = PerlIO_close(IoIFP(io));
ee518936
NIS
122 if (result == EOF && fd > PL_maxsysfd) {
123 /* Why is this not Perl_warn*() call ? */
bf49b057 124 PerlIO_printf(Perl_error_log,
6170680b
IZ
125 "Warning: unable to close filehandle %s properly.\n",
126 GvENAME(gv));
ee518936 127 }
8990e307 128 IoOFP(io) = IoIFP(io) = Nullfp;
a687059c 129 }
c07a80fd
PP
130
131 if (as_raw) {
b931b1d9 132 /* sysopen style args, i.e. integer mode and permissions */
ee518936 133 STRLEN ix = 0;
e1ec3a88 134 const int appendtrunc =
3dccc55c 135 0
d1da7611 136#ifdef O_APPEND /* Not fully portable. */
3dccc55c 137 |O_APPEND
d1da7611
JH
138#endif
139#ifdef O_TRUNC /* Not fully portable. */
3dccc55c 140 |O_TRUNC
d1da7611 141#endif
3dccc55c 142 ;
6867be6d 143 const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
3dccc55c
JH
144 int ismodifying;
145
146 if (num_svs != 0) {
147 Perl_croak(aTHX_ "panic: sysopen with multiple args");
148 }
149 /* It's not always
150
151 O_RDONLY 0
152 O_WRONLY 1
153 O_RDWR 2
154
155 It might be (in OS/390 and Mac OS Classic it is)
156
157 O_WRONLY 1
158 O_RDONLY 2
159 O_RDWR 3
160
161 This means that simple & with O_RDWR would look
162 like O_RDONLY is present. Therefore we have to
163 be more careful.
164 */
165 if ((ismodifying = (rawmode & modifyingmode))) {
166 if ((ismodifying & O_WRONLY) == O_WRONLY ||
167 (ismodifying & O_RDWR) == O_RDWR ||
168 (ismodifying & (O_CREAT|appendtrunc)))
169 TAINT_PROPER("sysopen");
170 }
3b6c1aba 171 mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
b931b1d9 172
09458382 173#if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
b94c04ac 174 rawmode |= O_LARGEFILE; /* Transparently largefiley. */
5ff3f7a4
GS
175#endif
176
06c7082d 177 IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
ee518936 178
42d9b98d 179 namesv = sv_2mortal(newSVpv(oname,0));
ee518936
NIS
180 num_svs = 1;
181 svp = &namesv;
1141d9f8 182 type = Nullch;
b94c04ac 183 fp = PerlIO_openn(aTHX_ type, mode, -1, rawmode, rawperm, NULL, num_svs, svp);
a687059c 184 }
c07a80fd 185 else {
b931b1d9 186 /* Regular (non-sys) open */
2fbb330f 187 char *name;
faecd977 188 STRLEN olen = len;
b931b1d9
NIS
189 char *tend;
190 int dodup = 0;
ecdeb87c 191 PerlIO *that_fp = NULL;
c07a80fd 192
2fbb330f 193 type = savepvn(oname, len);
b931b1d9 194 tend = type+len;
faecd977 195 SAVEFREEPV(type);
eb649f83
AMS
196
197 /* Lose leading and trailing white space */
eb649f83
AMS
198 for (; isSPACE(*type); type++) ;
199 while (tend > type && isSPACE(tend[-1]))
200 *--tend = '\0';
201
6170680b 202 if (num_svs) {
c2be40b1 203 /* New style explicit name, type is just mode and layer info */
9a869a14 204#ifdef USE_STDIO
2fbb330f 205 if (SvROK(*svp) && !strchr(oname,'&')) {
9a869a14
RGS
206 if (ckWARN(WARN_IO))
207 Perl_warner(aTHX_ packWARN(WARN_IO),
208 "Can't open a reference");
93189314 209 SETERRNO(EINVAL, LIB_INVARG);
9a869a14
RGS
210 goto say_false;
211 }
212#endif /* USE_STDIO */
705bfa5f 213 name = SvOK(*svp) ? savesvpv (*svp) : savepvn ("", 0);
faecd977 214 SAVEFREEPV(name);
6170680b 215 }
faecd977 216 else {
faecd977 217 name = type;
b931b1d9 218 len = tend-type;
faecd977 219 }
6170680b 220 IoTYPE(io) = *type;
516a5887 221 if ((*type == IoTYPE_RDWR) && /* scary */
01a8ea99 222 (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
516a5887 223 ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
c2be40b1 224 TAINT_PROPER("open");
6170680b 225 mode[1] = *type++;
c07a80fd 226 writing = 1;
a687059c 227 }
c07a80fd 228
9f37169a 229 if (*type == IoTYPE_PIPE) {
b931b1d9
NIS
230 if (num_svs) {
231 if (type[1] != IoTYPE_STD) {
c2be40b1 232 unknown_open_mode:
b931b1d9
NIS
233 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
234 }
235 type++;
6170680b 236 }
b931b1d9 237 for (type++; isSPACE(*type); type++) ;
faecd977 238 if (!num_svs) {
6170680b 239 name = type;
b931b1d9 240 len = tend-type;
faecd977 241 }
4a7d1889
NIS
242 if (*name == '\0') {
243 /* command is missing 19990114 */
06eaf0bc 244 if (ckWARN(WARN_PIPE))
9014280d 245 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
06eaf0bc
GS
246 errno = EPIPE;
247 goto say_false;
248 }
770526c1 249 if ((*name == '-' && name[1] == '\0') || num_svs)
c07a80fd
PP
250 TAINT_ENV();
251 TAINT_PROPER("piped open");
b931b1d9 252 if (!num_svs && name[len-1] == '|') {
faecd977 253 name[--len] = '\0' ;
599cee73 254 if (ckWARN(WARN_PIPE))
9014280d 255 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
7b8d334a 256 }
a1d180c4 257 mode[0] = 'w';
c07a80fd 258 writing = 1;
0c19750d
SP
259#ifdef HAS_STRLCAT
260 if (out_raw)
261 strlcat(mode, "b", PERL_MODE_MAX);
262 else if (out_crlf)
263 strlcat(mode, "t", PERL_MODE_MAX);
264#else
a1d180c4
NIS
265 if (out_raw)
266 strcat(mode, "b");
267 else if (out_crlf)
268 strcat(mode, "t");
0c19750d 269#endif
4a7d1889
NIS
270 if (num_svs > 1) {
271 fp = PerlProc_popen_list(mode, num_svs, svp);
272 }
273 else {
274 fp = PerlProc_popen(name,mode);
275 }
1771866f
NIS
276 if (num_svs) {
277 if (*type) {
278 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
279 goto say_false;
280 }
281 }
282 }
c2be40b1 283 } /* IoTYPE_PIPE */
9f37169a 284 else if (*type == IoTYPE_WRONLY) {
c07a80fd 285 TAINT_PROPER("open");
6170680b 286 type++;
9f37169a
JH
287 if (*type == IoTYPE_WRONLY) {
288 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
50952442 289 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
6170680b 290 type++;
a0d0e21e 291 }
ee518936 292 else {
c07a80fd 293 mode[0] = 'w';
ee518936 294 }
c07a80fd
PP
295 writing = 1;
296
0c19750d
SP
297#ifdef HAS_STRLCAT
298 if (out_raw)
299 strlcat(mode, "b", PERL_MODE_MAX);
300 else if (out_crlf)
301 strlcat(mode, "t", PERL_MODE_MAX);
302#else
16fe6d59
GS
303 if (out_raw)
304 strcat(mode, "b");
305 else if (out_crlf)
306 strcat(mode, "t");
0c19750d 307#endif
6170680b 308 if (*type == '&') {
c07a80fd 309 duplicity:
ecdeb87c 310 dodup = PERLIO_DUP_FD;
e620cd72
NIS
311 type++;
312 if (*type == '=') {
c07a80fd 313 dodup = 0;
e620cd72 314 type++;
4a7d1889 315 }
ee518936 316 if (!num_svs && !*type && supplied_fp) {
4a7d1889 317 /* "<+&" etc. is used by typemaps */
c07a80fd 318 fp = supplied_fp;
ee518936 319 }
a0d0e21e 320 else {
e620cd72
NIS
321 if (num_svs > 1) {
322 Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
323 }
54edfe98 324 for (; isSPACE(*type); type++) ;
1771866f 325 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
e620cd72 326 fd = SvUV(*svp);
24a7a40d 327 num_svs = 0;
ee518936 328 }
e620cd72 329 else if (isDIGIT(*type)) {
e620cd72
NIS
330 fd = atoi(type);
331 }
c07a80fd 332 else {
e1ec3a88 333 const IO* thatio;
e620cd72
NIS
334 if (num_svs) {
335 thatio = sv_2io(*svp);
336 }
337 else {
338 GV *thatgv;
f776e3cd 339 thatgv = gv_fetchpv(type,0,SVt_PVIO);
e620cd72
NIS
340 thatio = GvIO(thatgv);
341 }
c07a80fd 342 if (!thatio) {
6e21c824 343#ifdef EINVAL
93189314 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.
04d26ece 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)
1141d9f8 383 type = Nullch;
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))) {
b42969c0 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) {
2b2a7552 411 namesv = sv_2mortal(newSVpvn(type,tend - type));
ee518936
NIS
412 num_svs = 1;
413 svp = &namesv;
1141d9f8 414 type = Nullch;
ee518936 415 }
6e60e805 416 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
c07a80fd 417 }
ee518936 418 } /* !& */
7e72d509
JH
419 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
420 goto unknown_open_mode;
c2be40b1 421 } /* IoTYPE_WRONLY */
9f37169a 422 else if (*type == IoTYPE_RDONLY) {
6170680b 423 for (type++; isSPACE(*type); type++) ;
bf38876a 424 mode[0] = 'r';
0c19750d
SP
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");
0c19750d 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) {
2b2a7552 449 namesv = sv_2mortal(newSVpvn(type,tend - type));
ee518936
NIS
450 num_svs = 1;
451 svp = &namesv;
1141d9f8 452 type = Nullch;
ee518936 453 }
6e60e805 454 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
ee518936 455 }
7e72d509
JH
456 if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
457 goto unknown_open_mode;
c2be40b1
JH
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';
a6e20a40
AL
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 }
770526c1 481 if (!(*name == '-' && name[1] == '\0') || num_svs)
79072805
LW
482 TAINT_ENV();
483 TAINT_PROPER("piped open");
a1d180c4 484 mode[0] = 'r';
0c19750d
SP
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");
0c19750d
SP
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 }
c2be40b1 514 else { /* layer(Args) */
6170680b 515 if (num_svs)
c2be40b1 516 goto unknown_open_mode;
6170680b 517 name = type;
50952442 518 IoTYPE(io) = IoTYPE_RDONLY;
a6e20a40
AL
519 for (; isSPACE(*name); name++)
520 ;
88b61e10 521 mode[0] = 'r';
0c19750d
SP
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");
0c19750d
SP
533#endif
534
770526c1 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) {
2b2a7552 541 namesv = sv_2mortal(newSVpvn(type,tend - type));
ee518936
NIS
542 num_svs = 1;
543 svp = &namesv;
1141d9f8 544 type = Nullch;
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) {
ce44635a 551 if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
2fbb330f 552 && strchr(oname, '\n')
ce44635a 553
041457d9 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),
97828cef
RGS
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),
97828cef
RGS
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);
862a34c6 654 SvUPGRADE(sv, SVt_IV);
d0e2cf63 655 pid = SvIVX(sv);
45977657 656 SvIV_set(sv, 0);
d0e2cf63 657 sv = *av_fetch(PL_fdpid,savefd,TRUE);
862a34c6 658 SvUPGRADE(sv, SVt_IV);
45977657 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);
03e631df
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);
03e631df
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) {
0bcc34c2 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;
3b6c1aba
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);
8990e307 706 IoIFP(io) = Nullfp;
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{
97aff369 725 dVAR;
79072805 726 register SV *sv;
99b89507 727#ifndef FLEXFILENAMES
c623bd54
LW
728 int filedev;
729 int fileino;
99b89507 730#endif
761237fe
JB
731 Uid_t fileuid;
732 Gid_t filegid;
2d03de9c 733 IO * const io = GvIOp(gv);
fe14fcc3 734
3280af22 735 if (!PL_argvoutgv)
5c1737d1 736 PL_argvoutgv = gv_fetchpvs("ARGVOUT",TRUE,SVt_PVIO);
18708f5a
GS
737 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
738 IoFLAGS(io) &= ~IOf_START;
7a1c5554
GS
739 if (PL_inplace) {
740 if (!PL_argvout_stack)
741 PL_argvout_stack = newAV();
18708f5a 742 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
7a1c5554 743 }
18708f5a 744 }
3280af22
NIS
745 if (PL_filemode & (S_ISUID|S_ISGID)) {
746 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 747#ifdef HAS_FCHMOD
c797f2d8
DM
748 if (PL_lastfd != -1)
749 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 750#else
b28d0864 751 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3
LW
752#endif
753 }
c797f2d8 754 PL_lastfd = -1;
3280af22 755 PL_filemode = 0;
5c501b37
JH
756 if (!GvAV(gv))
757 return Nullfp;
79072805 758 while (av_len(GvAV(gv)) >= 0) {
85aff577 759 STRLEN oldlen;
79072805 760 sv = av_shift(GvAV(gv));
8990e307 761 SAVEFREESV(sv);
e203899d 762 sv_setsv(GvSVn(gv),sv);
79072805 763 SvSETMAGIC(GvSV(gv));
3280af22 764 PL_oldname = SvPVx(GvSV(gv), oldlen);
9d116dd7 765 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
3280af22 766 if (PL_inplace) {
79072805 767 TAINT_PROPER("inplace open");
3280af22 768 if (oldlen == 1 && *PL_oldname == '-') {
5c1737d1 769 setdefout(gv_fetchpvs("STDOUT",TRUE,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) {
2d03de9c 788 const char *star = strchr(PL_inplace, '*');
2d259d92 789 if (star) {
2d03de9c 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
95a20fc0 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),
35c1215d
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)
95a20fc0 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),
35c1215d
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);
95a20fc0
SP
832 (void)PerlLIO_unlink(SvPVX_const(sv));
833 (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
93524f2b
NC
834 do_open(gv,(char*)SvPVX_const(sv),SvCUR(sv),PL_inplace!=0,
835 O_RDONLY,0,Nullfp);
55497cff 836#endif /* DOSISH */
ff8e2863 837#else
95a20fc0
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),
35c1215d
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
93524f2b
NC
871 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
872 PL_inplace!=0,O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
4119ab01 873#else
93524f2b
NC
874 if (!do_open(PL_argvoutgv,(char*)SvPVX_const(sv),SvCUR(sv),
875 PL_inplace!=0,O_WRONLY|O_CREAT|OPEN_EXCL,0666,
876 Nullfp))
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)) {
4373e329 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);
934 return Nullfp;
935 }
5c1737d1 936 setdefout(gv_fetchpvs("STDOUT",TRUE,SVt_PVIO));
a687059c
LW
937 }
938 return Nullfp;
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{
97aff369 945 dVAR;
1193dd27
IZ
946 bool retval;
947 IO *io;
a687059c 948
79072805 949 if (!gv)
3280af22 950 gv = PL_argvgv;
a0d0e21e 951 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1d2dff63 952 if (not_implicit)
93189314 953 SETERRNO(EBADF,SS_IVCHAN);
c2ab57d4 954 return FALSE;
99b89507 955 }
79072805
LW
956 io = GvIO(gv);
957 if (!io) { /* never opened */
1d2dff63 958 if (not_implicit) {
2dd78f96
JH
959 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
960 report_evil_fh(gv, io, PL_op->op_type);
93189314 961 SETERRNO(EBADF,SS_IVCHAN);
1d2dff63 962 }
a687059c
LW
963 return FALSE;
964 }
f2b5be74 965 retval = io_close(io, not_implicit);
517844ec 966 if (not_implicit) {
1193dd27
IZ
967 IoLINES(io) = 0;
968 IoPAGE(io) = 0;
969 IoLINES_LEFT(io) = IoPAGE_LEN(io);
970 }
50952442 971 IoTYPE(io) = IoTYPE_CLOSED;
1193dd27
IZ
972 return retval;
973}
974
975bool
f2b5be74 976Perl_io_close(pTHX_ IO *io, bool not_implicit)
1193dd27 977{
97aff369 978 dVAR;
1193dd27 979 bool retval = FALSE;
1193dd27 980
8990e307 981 if (IoIFP(io)) {
50952442 982 if (IoTYPE(io) == IoTYPE_PIPE) {
4373e329 983 const int status = PerlProc_pclose(IoIFP(io));
f2b5be74 984 if (not_implicit) {
37038d91 985 STATUS_NATIVE_CHILD_SET(status);
e5218da5 986 retval = (STATUS_UNIX == 0);
f2b5be74
GS
987 }
988 else {
989 retval = (status != -1);
990 }
a687059c 991 }
50952442 992 else if (IoTYPE(io) == IoTYPE_STD)
a687059c
LW
993 retval = TRUE;
994 else {
8990e307 995 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
0bcc34c2 996 const bool prev_err = PerlIO_error(IoOFP(io));
e199e3be 997 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
760ac839 998 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 999 }
e199e3be 1000 else {
0bcc34c2 1001 const bool prev_err = PerlIO_error(IoIFP(io));
e199e3be
RGS
1002 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1003 }
a687059c 1004 }
8990e307 1005 IoOFP(io) = IoIFP(io) = Nullfp;
79072805 1006 }
f2b5be74 1007 else if (not_implicit) {
93189314 1008 SETERRNO(EBADF,SS_IVCHAN);
20408e3c 1009 }
1193dd27 1010
a687059c
LW
1011 return retval;
1012}
1013
1014bool
864dbfa3 1015Perl_do_eof(pTHX_ GV *gv)
a687059c 1016{
97aff369 1017 dVAR;
0bcc34c2 1018 register IO * const io = GvIO(gv);
a687059c 1019
79072805 1020 if (!io)
a687059c 1021 return TRUE;
041457d9 1022 else if ((IoTYPE(io) == IoTYPE_WRONLY) && ckWARN(WARN_IO))
06bcfee8 1023 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
a687059c 1024
8990e307 1025 while (IoIFP(io)) {
760ac839 1026 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
a20bf0c3 1027 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
760ac839
LW
1028 return FALSE; /* this is the most usual case */
1029 }
a687059c 1030
79852593
NC
1031 {
1032 /* getc and ungetc can stomp on errno */
1033 const int saverrno = errno;
1034 const int ch = PerlIO_getc(IoIFP(io));
1035 if (ch != EOF) {
1036 (void)PerlIO_ungetc(IoIFP(io),ch);
1037 errno = saverrno;
1038 return FALSE;
1039 }
505fdf5d 1040 errno = saverrno;
a687059c 1041 }
fab3f3a7 1042
760ac839 1043 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3
JH
1044 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1045 PerlIO_set_cnt(IoIFP(io),-1);
760ac839 1046 }
533c011a 1047 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
ed2c6b9b 1048 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
a687059c
LW
1049 return TRUE;
1050 }
1051 else
1052 return TRUE; /* normal fp, definitely end of file */
1053 }
1054 return TRUE;
1055}
1056
5ff3f7a4 1057Off_t
864dbfa3 1058Perl_do_tell(pTHX_ GV *gv)
a687059c 1059{
97aff369 1060 dVAR;
cbbf8932 1061 register IO *io = NULL;
96e4d5b1 1062 register PerlIO *fp;
a687059c 1063
96e4d5b1 1064 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 1065#ifdef ULTRIX_STDIO_BOTCH
96e4d5b1
PP
1066 if (PerlIO_eof(fp))
1067 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 1068#endif
8903cb82 1069 return PerlIO_tell(fp);
96e4d5b1 1070 }
411caa50
JH
1071 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1072 report_evil_fh(gv, io, PL_op->op_type);
93189314 1073 SETERRNO(EBADF,RMS_IFI);
5ff3f7a4 1074 return (Off_t)-1;
a687059c
LW
1075}
1076
1077bool
864dbfa3 1078Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
a687059c 1079{
97aff369 1080 dVAR;
cbbf8932 1081 register IO *io = NULL;
137443ea 1082 register PerlIO *fp;
a687059c 1083
137443ea 1084 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 1085#ifdef ULTRIX_STDIO_BOTCH
137443ea
PP
1086 if (PerlIO_eof(fp))
1087 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 1088#endif
8903cb82 1089 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 1090 }
411caa50
JH
1091 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1092 report_evil_fh(gv, io, PL_op->op_type);
93189314 1093 SETERRNO(EBADF,RMS_IFI);
a687059c
LW
1094 return FALSE;
1095}
1096
97cc44eb 1097Off_t
864dbfa3 1098Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
8903cb82 1099{
97aff369 1100 dVAR;
cbbf8932 1101 register IO *io = NULL;
8903cb82
PP
1102 register PerlIO *fp;
1103
1104 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
3028581b 1105 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
411caa50
JH
1106 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1107 report_evil_fh(gv, io, PL_op->op_type);
93189314 1108 SETERRNO(EBADF,RMS_IFI);
d9b3e12d 1109 return (Off_t)-1;
8903cb82
PP
1110}
1111
6ff81951 1112int
16fe6d59
GS
1113Perl_mode_from_discipline(pTHX_ SV *discp)
1114{
1115 int mode = O_BINARY;
1116 if (discp) {
1117 STRLEN len;
e62f0680 1118 const char *s = SvPV_const(discp,len);
16fe6d59
GS
1119 while (*s) {
1120 if (*s == ':') {
1121 switch (s[1]) {
1122 case 'r':
e963d6d2 1123 if (s[2] == 'a' && s[3] == 'w'
16fe6d59
GS
1124 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1125 {
1126 mode = O_BINARY;
1127 s += 4;
1128 len -= 4;
1129 break;
1130 }
1131 /* FALL THROUGH */
1132 case 'c':
e963d6d2 1133 if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
16fe6d59
GS
1134 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1135 {
1136 mode = O_TEXT;
1137 s += 5;
1138 len -= 5;
1139 break;
1140 }
1141 /* FALL THROUGH */
1142 default:
1143 goto fail_discipline;
1144 }
1145 }
1146 else if (isSPACE(*s)) {
1147 ++s;
1148 --len;
1149 }
1150 else {
4373e329 1151 const char *end;
16fe6d59
GS
1152fail_discipline:
1153 end = strchr(s+1, ':');
1154 if (!end)
1155 end = s+len;
60382766 1156#ifndef PERLIO_LAYERS
363c40c4 1157 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
60382766 1158#else
18a33fb5 1159 len -= end-s;
60382766
NIS
1160 s = end;
1161#endif
16fe6d59
GS
1162 }
1163 }
1164 }
1165 return mode;
1166}
1167
58e24eff 1168#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
27da23d5
JH
1169I32
1170my_chsize(int fd, Off_t length)
6eb13c3b 1171{
58e24eff
SH
1172#ifdef F_FREESP
1173 /* code courtesy of William Kucharski */
1174#define HAS_CHSIZE
1175
6eb13c3b 1176 struct flock fl;
c623ac67 1177 Stat_t filebuf;
6eb13c3b 1178
3028581b 1179 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b
LW
1180 return -1;
1181
1182 if (filebuf.st_size < length) {
1183
1184 /* extend file length */
1185
3028581b 1186 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b
LW
1187 return -1;
1188
1189 /* write a "0" byte */
1190
3028581b 1191 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b
LW
1192 return -1;
1193 }
1194 else {
1195 /* truncate length */
1196
1197 fl.l_whence = 0;
1198 fl.l_len = 0;
1199 fl.l_start = length;
a0d0e21e 1200 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b
LW
1201
1202 /*
a0d0e21e 1203 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b
LW
1204 * fcntl(2), which truncates the file so that it ends at the
1205 * position indicated by fl.l_start.
1206 *
1207 * Will minor miracles never cease?
1208 */
1209
a0d0e21e 1210 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b
LW
1211 return -1;
1212
1213 }
6eb13c3b 1214 return 0;
58e24eff 1215#else
27da23d5 1216 Perl_croak_nocontext("truncate not implemented");
a0d0e21e 1217#endif /* F_FREESP */
27da23d5 1218 return -1;
58e24eff
SH
1219}
1220#endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
ff8e2863 1221
a687059c 1222bool
864dbfa3 1223Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
a687059c 1224{
97aff369 1225 dVAR;
e1ec3a88 1226 register const char *tmps;
463ee0b2 1227 STRLEN len;
a687059c 1228
79072805
LW
1229 /* assuming fp is checked earlier */
1230 if (!sv)
1231 return TRUE;
79072805
LW
1232 switch (SvTYPE(sv)) {
1233 case SVt_NULL:
411caa50 1234 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 1235 report_uninit(sv);
ff8e2863 1236 return TRUE;
79072805 1237 case SVt_IV:
a0d0e21e 1238 if (SvIOK(sv)) {
5b295bef 1239 SvGETMAGIC(sv);
cf2093f6 1240 if (SvIsUV(sv))
57def98f 1241 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
cf2093f6 1242 else
57def98f 1243 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
760ac839 1244 return !PerlIO_error(fp);
a0d0e21e
LW
1245 }
1246 /* FALL THROUGH */
79072805 1247 default:
7d59b7e4 1248 if (PerlIO_isutf8(fp)) {
f9a63242 1249 if (!SvUTF8(sv))
88632417
JH
1250 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1251 SV_GMAGIC|SV_UTF8_NO_ENCODING);
7d59b7e4 1252 }
ae798467 1253 else if (DO_UTF8(sv)) {
2da16753 1254 if (!sv_utf8_downgrade((sv = sv_mortalcopy(sv)), TRUE)
62961d2e 1255 && ckWARN_d(WARN_UTF8))
2da16753 1256 {
9014280d 1257 Perl_warner(aTHX_ packWARN(WARN_UTF8), "Wide character in print");
ae798467
NIS
1258 }
1259 }
5c144d81 1260 tmps = SvPV_const(sv, len);
79072805 1261 break;
ff8e2863 1262 }
94e4c244
JH
1263 /* To detect whether the process is about to overstep its
1264 * filesize limit we would need getrlimit(). We could then
1265 * also transparently raise the limit with setrlimit() --
1266 * but only until the system hard limit/the filesystem limit,
c5dd3cdd
JH
1267 * at which we would get EPERM. Note that when using buffered
1268 * io the write failure can be delayed until the flush/close. --jhi */
a21ac455 1269 if (len && (PerlIO_write(fp,tmps,len) == 0))
a687059c 1270 return FALSE;
760ac839 1271 return !PerlIO_error(fp);
a687059c
LW
1272}
1273
79072805 1274I32
cea2e8a9 1275Perl_my_stat(pTHX)
a687059c 1276{
97aff369 1277 dVAR;
39644a26 1278 dSP;
79072805 1279 IO *io;
2dd78f96 1280 GV* gv;
79072805 1281
533c011a 1282 if (PL_op->op_flags & OPf_REF) {
924508f0 1283 EXTEND(SP,1);
2dd78f96 1284 gv = cGVOP_gv;
748a9306 1285 do_fstat:
2dd78f96 1286 io = GvIO(gv);
8990e307 1287 if (io && IoIFP(io)) {
2dd78f96 1288 PL_statgv = gv;
c69006e4 1289 sv_setpvn(PL_statname,"", 0);
3280af22
NIS
1290 PL_laststype = OP_STAT;
1291 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c
LW
1292 }
1293 else {
2dd78f96 1294 if (gv == PL_defgv)
3280af22 1295 return PL_laststatval;
2dd78f96
JH
1296 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1297 report_evil_fh(gv, io, PL_op->op_type);
3280af22 1298 PL_statgv = Nullgv;
c69006e4 1299 sv_setpvn(PL_statname,"", 0);
3280af22 1300 return (PL_laststatval = -1);
a687059c
LW
1301 }
1302 }
fbb0b3b3
RGS
1303 else if (PL_op->op_private & OPpFT_STACKED) {
1304 return PL_laststatval;
1305 }
a687059c 1306 else {
0bcc34c2 1307 SV* const sv = POPs;
4373e329 1308 const char *s;
4ecd490c 1309 STRLEN len;
79072805 1310 PUTBACK;
748a9306 1311 if (SvTYPE(sv) == SVt_PVGV) {
2dd78f96 1312 gv = (GV*)sv;
748a9306
LW
1313 goto do_fstat;
1314 }
1315 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
2dd78f96 1316 gv = (GV*)SvRV(sv);
748a9306
LW
1317 goto do_fstat;
1318 }
1319
5c144d81 1320 s = SvPV_const(sv, len);
3280af22 1321 PL_statgv = Nullgv;
4ecd490c 1322 sv_setpvn(PL_statname, s, len);
95a20fc0 1323 s = SvPVX_const(PL_statname); /* s now NUL-terminated */
3280af22
NIS
1324 PL_laststype = OP_STAT;
1325 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
ce44635a 1326 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
9014280d 1327 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
3280af22 1328 return PL_laststatval;
a687059c
LW
1329 }
1330}
1331
fbb0b3b3 1332
79072805 1333I32
cea2e8a9 1334Perl_my_lstat(pTHX)
c623bd54 1335{
97aff369 1336 dVAR;
0bd48802 1337 static const char no_prev_lstat[] = "The stat preceding -l _ wasn't an lstat";
39644a26 1338 dSP;
79072805 1339 SV *sv;
533c011a 1340 if (PL_op->op_flags & OPf_REF) {
924508f0 1341 EXTEND(SP,1);
638eceb6 1342 if (cGVOP_gv == PL_defgv) {
3280af22 1343 if (PL_laststype != OP_LSTAT)
fbb0b3b3 1344 Perl_croak(aTHX_ no_prev_lstat);
3280af22 1345 return PL_laststatval;
fe14fcc3 1346 }
5d3e98de 1347 if (ckWARN(WARN_IO)) {
9014280d 1348 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
5d3e98de
RGS
1349 GvENAME(cGVOP_gv));
1350 return (PL_laststatval = -1);
1351 }
fe14fcc3 1352 }
041457d9
DM
1353 else if (PL_laststype != OP_LSTAT
1354 && (PL_op->op_private & OPpFT_STACKED) && ckWARN(WARN_IO))
fbb0b3b3 1355 Perl_croak(aTHX_ no_prev_lstat);
c623bd54 1356
3280af22
NIS
1357 PL_laststype = OP_LSTAT;
1358 PL_statgv = Nullgv;
79072805
LW
1359 sv = POPs;
1360 PUTBACK;
5d3e98de 1361 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
9014280d 1362 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
5d3e98de
RGS
1363 GvENAME((GV*) SvRV(sv)));
1364 return (PL_laststatval = -1);
1365 }
4373e329 1366 /* XXX Do really need to be calling SvPV() all these times? */
e62f0680
NC
1367 sv_setpv(PL_statname,SvPV_nolen_const(sv));
1368 PL_laststatval = PerlLIO_lstat(SvPV_nolen_const(sv),&PL_statcache);
1369 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV_nolen_const(sv), '\n'))
9014280d 1370 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
3280af22 1371 return PL_laststatval;
c623bd54
LW
1372}
1373
d5a9bfb0 1374bool
2aa1486d
GS
1375Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1376 int fd, int do_report)
d5a9bfb0 1377{
27da23d5 1378 dVAR;
a0fd4948 1379#if defined(MACOS_TRADITIONAL) || defined(__SYMBIAN32__)
cd39f2b6
JH
1380 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1381#else
79072805 1382 if (sp > mark) {
c3c5fad7 1383 char **a;
6136c704 1384 const char *tmps = NULL;
c3c5fad7
DM
1385 Newx(PL_Argv, sp - mark + 1, char*);
1386 a = PL_Argv;
890ce7af 1387
79072805
LW
1388 while (++mark <= sp) {
1389 if (*mark)
e62f0680 1390 *a++ = (char*)SvPV_nolen_const(*mark);
a687059c
LW
1391 else
1392 *a++ = "";
1393 }
6136c704 1394 *a = NULL;
91b2752f 1395 if (really)
e62f0680 1396 tmps = SvPV_nolen_const(really);
91b2752f
RG
1397 if ((!really && *PL_Argv[0] != '/') ||
1398 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1399 TAINT_ENV(); /* testing IFS here is overkill, probably */
b35112e7 1400 PERL_FPU_PRE_EXEC
91b2752f 1401 if (really && *tmps)
b4748376 1402 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1403 else
b4748376 1404 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
b35112e7 1405 PERL_FPU_POST_EXEC
599cee73 1406 if (ckWARN(WARN_EXEC))
9014280d 1407 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
91b2752f 1408 (really ? tmps : PL_Argv[0]), Strerror(errno));
d5a9bfb0 1409 if (do_report) {
79852593 1410 const int e = errno;
d5a9bfb0
IZ
1411
1412 PerlLIO_write(fd, (void*)&e, sizeof(int));
1413 PerlLIO_close(fd);
1414 }
a687059c 1415 }
bee1dbe2 1416 do_execfree();
cd39f2b6 1417#endif
a687059c
LW
1418 return FALSE;
1419}
1420
fe14fcc3 1421void
864dbfa3 1422Perl_do_execfree(pTHX)
ff8e2863 1423{
97aff369 1424 dVAR;
43c5f42d
NC
1425 Safefree(PL_Argv);
1426 PL_Argv = Null(char **);
1427 Safefree(PL_Cmd);
1428 PL_Cmd = Nullch;
ff8e2863
LW
1429}
1430
9555a685 1431#ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
e446cec8
IZ
1432
1433bool
2fbb330f 1434Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
e446cec8 1435{
27da23d5 1436 dVAR;
a687059c
LW
1437 register char **a;
1438 register char *s;
2fbb330f 1439 char *cmd;
2fbb330f
JM
1440
1441 /* Make a copy so we can change it */
0bcc34c2 1442 const int cmdlen = strlen(incmd);
2fbb330f
JM
1443 Newx(cmd, cmdlen+1, char);
1444 strncpy(cmd, incmd, cmdlen);
1445 cmd[cmdlen] = 0;
a687059c 1446
748a9306
LW
1447 while (*cmd && isSPACE(*cmd))
1448 cmd++;
1449
a687059c
LW
1450 /* save an extra exec if possible */
1451
bf38876a 1452#ifdef CSH
d05c1ba0 1453 {
0c19750d 1454 char flags[PERL_FLAGS_MAX];
d05c1ba0
JH
1455 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1456 strnEQ(cmd+PL_cshlen," -c",3)) {
0c19750d
SP
1457#ifdef HAS_STRLCPY
1458 strlcpy(flags, "-c", PERL_FLAGS_MAX);
1459#else
d05c1ba0 1460 strcpy(flags,"-c");
0c19750d 1461#endif
d05c1ba0
JH
1462 s = cmd+PL_cshlen+3;
1463 if (*s == 'f') {
1464 s++;
0c19750d
SP
1465#ifdef HAS_STRLCPY
1466 strlcat(flags, "f", PERL_FLAGS_MAX);
1467#else
d05c1ba0 1468 strcat(flags,"f");
0c19750d 1469#endif
d05c1ba0
JH
1470 }
1471 if (*s == ' ')
1472 s++;
1473 if (*s++ == '\'') {
0bcc34c2 1474 char * const ncmd = s;
d05c1ba0
JH
1475
1476 while (*s)
1477 s++;
1478 if (s[-1] == '\n')
1479 *--s = '\0';
1480 if (s[-1] == '\'') {
1481 *--s = '\0';
b35112e7 1482 PERL_FPU_PRE_EXEC
d05c1ba0 1483 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
b35112e7 1484 PERL_FPU_POST_EXEC
d05c1ba0 1485 *s = '\'';
2fbb330f 1486 Safefree(cmd);
d05c1ba0
JH
1487 return FALSE;
1488 }
1489 }
a687059c
LW
1490 }
1491 }
bf38876a 1492#endif /* CSH */
a687059c
LW
1493
1494 /* see if there are shell metacharacters in it */
1495
748a9306
LW
1496 if (*cmd == '.' && isSPACE(cmd[1]))
1497 goto doshell;
1498
1499 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1500 goto doshell;
1501
c170e444 1502 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1
LW
1503 if (*s == '=')
1504 goto doshell;
748a9306 1505
a687059c 1506 for (s = cmd; *s; s++) {
d05c1ba0
JH
1507 if (*s != ' ' && !isALPHA(*s) &&
1508 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c
LW
1509 if (*s == '\n' && !s[1]) {
1510 *s = '\0';
1511 break;
1512 }
603a98b0
IZ
1513 /* handle the 2>&1 construct at the end */
1514 if (*s == '>' && s[1] == '&' && s[2] == '1'
1515 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1516 && (!s[3] || isSPACE(s[3])))
1517 {
6867be6d 1518 const char *t = s + 3;
603a98b0
IZ
1519
1520 while (*t && isSPACE(*t))
1521 ++t;
943bbd07 1522 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
603a98b0
IZ
1523 s[-2] = '\0';
1524 break;
1525 }
1526 }
a687059c 1527 doshell:
b35112e7 1528 PERL_FPU_PRE_EXEC
3280af22 1529 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
b35112e7 1530 PERL_FPU_POST_EXEC
2fbb330f 1531 Safefree(cmd);
a687059c
LW
1532 return FALSE;
1533 }
1534 }
748a9306 1535
a02a5408 1536 Newx(PL_Argv, (s - cmd) / 2 + 2, char*);
3280af22
NIS
1537 PL_Cmd = savepvn(cmd, s-cmd);
1538 a = PL_Argv;
1539 for (s = PL_Cmd; *s;) {
99b89507 1540 while (*s && isSPACE(*s)) s++;
a687059c
LW
1541 if (*s)
1542 *(a++) = s;
99b89507 1543 while (*s && !isSPACE(*s)) s++;
a687059c
LW
1544 if (*s)
1545 *s++ = '\0';
1546 }
6136c704 1547 *a = NULL;
3280af22 1548 if (PL_Argv[0]) {
b35112e7 1549 PERL_FPU_PRE_EXEC
3280af22 1550 PerlProc_execvp(PL_Argv[0],PL_Argv);
b35112e7 1551 PERL_FPU_POST_EXEC
b1248f16 1552 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1553 do_execfree();
a687059c 1554 goto doshell;
b1248f16 1555 }
6136c704
AL
1556 if (ckWARN(WARN_EXEC))
1557 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1558 PL_Argv[0], Strerror(errno));
1559 if (do_report) {
1560 const int e = errno;
1561 PerlLIO_write(fd, (const void*)&e, sizeof(int));
1562 PerlLIO_close(fd);
d008e5eb 1563 }
a687059c 1564 }
ff8e2863 1565 do_execfree();
2fbb330f 1566 Safefree(cmd);
a687059c
LW
1567 return FALSE;
1568}
1569
6890e559 1570#endif /* OS2 || WIN32 */
760ac839 1571
79072805 1572I32
864dbfa3 1573Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1574{
97aff369 1575 dVAR;
79072805 1576 register I32 val;
79072805 1577 register I32 tot = 0;
4634a855 1578 const char *const what = PL_op_name[type];
5c144d81 1579 const char *s;
890ce7af 1580 SV ** const oldmark = mark;
a687059c 1581
1444765e
NC
1582 /* Doing this ahead of the switch statement preserves the old behaviour,
1583 where attempting to use kill as a taint test test would fail on
1584 platforms where kill was not defined. */
1585#ifndef HAS_KILL
1586 if (type == OP_KILL)
4634a855 1587 Perl_die(aTHX_ PL_no_func, what);
1444765e
NC
1588#endif
1589#ifndef HAS_CHOWN
1590 if (type == OP_CHOWN)
4634a855 1591 Perl_die(aTHX_ PL_no_func, what);
1444765e
NC
1592#endif
1593
1594
20408e3c 1595#define APPLY_TAINT_PROPER() \
3280af22 1596 STMT_START { \
17406bd6 1597 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1598 } STMT_END
20408e3c
GS
1599
1600 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1601 if (PL_tainting) {
463ee0b2 1602 while (++mark <= sp) {
bbce6d69
PP
1603 if (SvTAINTED(*mark)) {
1604 TAINT;
1605 break;
1606 }
463ee0b2
LW
1607 }
1608 mark = oldmark;
1609 }
a687059c 1610 switch (type) {
79072805 1611 case OP_CHMOD:
20408e3c 1612 APPLY_TAINT_PROPER();
79072805 1613 if (++mark <= sp) {
463ee0b2 1614 val = SvIVx(*mark);
20408e3c
GS
1615 APPLY_TAINT_PROPER();
1616 tot = sp - mark;
79072805 1617 while (++mark <= sp) {
c4aca7d0
GA
1618 GV* gv;
1619 if (SvTYPE(*mark) == SVt_PVGV) {
1620 gv = (GV*)*mark;
1621 do_fchmod:
1622 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1623#ifdef HAS_FCHMOD
1624 APPLY_TAINT_PROPER();
1625 if (fchmod(PerlIO_fileno(IoIFP(GvIOn(gv))), val))
1626 tot--;
1627#else
b9c6780e 1628 Perl_die(aTHX_ PL_no_func, "fchmod");
c4aca7d0
GA
1629#endif
1630 }
1631 else {
1632 tot--;
1633 }
1634 }
1635 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1636 gv = (GV*)SvRV(*mark);
1637 goto do_fchmod;
1638 }
1639 else {
1640 const char *name = SvPV_nolen_const(*mark);
1641 APPLY_TAINT_PROPER();
1642 if (PerlLIO_chmod(name, val))
1643 tot--;
1644 }
a687059c
LW
1645 }
1646 }
1647 break;
fe14fcc3 1648#ifdef HAS_CHOWN
79072805 1649 case OP_CHOWN:
20408e3c 1650 APPLY_TAINT_PROPER();
79072805 1651 if (sp - mark > 2) {
6867be6d 1652 register I32 val2;
463ee0b2
LW
1653 val = SvIVx(*++mark);
1654 val2 = SvIVx(*++mark);
20408e3c 1655 APPLY_TAINT_PROPER();
a0d0e21e 1656 tot = sp - mark;
79072805 1657 while (++mark <= sp) {
c4aca7d0
GA
1658 GV* gv;
1659 if (SvTYPE(*mark) == SVt_PVGV) {
1660 gv = (GV*)*mark;
1661 do_fchown:
1662 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1663#ifdef HAS_FCHOWN
1664 APPLY_TAINT_PROPER();
1665 if (fchown(PerlIO_fileno(IoIFP(GvIOn(gv))), val, val2))
1666 tot--;
1667#else
b9c6780e 1668 Perl_die(aTHX_ PL_no_func, "fchown");
c4aca7d0
GA
1669#endif
1670 }
1671 else {
1672 tot--;
1673 }
1674 }
1675 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1676 gv = (GV*)SvRV(*mark);
1677 goto do_fchown;
1678 }
1679 else {
1680 const char *name = SvPV_nolen_const(*mark);
1681 APPLY_TAINT_PROPER();
1682 if (PerlLIO_chown(name, val, val2))
1683 tot--;
1684 }
a687059c
LW
1685 }
1686 }
1687 break;
b1248f16 1688#endif
a1d180c4 1689/*
dd64f1c3
AD
1690XXX Should we make lchown() directly available from perl?
1691For now, we'll let Configure test for HAS_LCHOWN, but do
1692nothing in the core.
1693 --AD 5/1998
1694*/
fe14fcc3 1695#ifdef HAS_KILL
79072805 1696 case OP_KILL:
20408e3c 1697 APPLY_TAINT_PROPER();
55497cff
PP
1698 if (mark == sp)
1699 break;
e62f0680 1700 s = SvPVx_nolen_const(*++mark);
e02bfb16 1701 if (isALPHA(*s)) {
79072805
LW
1702 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1703 s += 3;
e02bfb16 1704 if ((val = whichsig(s)) < 0)
cea2e8a9 1705 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805
LW
1706 }
1707 else
463ee0b2 1708 val = SvIVx(*mark);
20408e3c
GS
1709 APPLY_TAINT_PROPER();
1710 tot = sp - mark;
3595fcef
PP
1711#ifdef VMS
1712 /* kill() doesn't do process groups (job trees?) under VMS */
1713 if (val < 0) val = -val;
1714 if (val == SIGKILL) {
1715# include <starlet.h>
1716 /* Use native sys$delprc() to insure that target process is
1717 * deleted; supervisor-mode images don't pay attention to
1718 * CRTL's emulation of Unix-style signals and kill()
1719 */
1720 while (++mark <= sp) {
1721 I32 proc = SvIVx(*mark);
1722 register unsigned long int __vmssts;
20408e3c 1723 APPLY_TAINT_PROPER();
3595fcef
PP
1724 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1725 tot--;
1726 switch (__vmssts) {
1727 case SS$_NONEXPR:
1728 case SS$_NOSUCHNODE:
1729 SETERRNO(ESRCH,__vmssts);
1730 break;
1731 case SS$_NOPRIV:
1732 SETERRNO(EPERM,__vmssts);
1733 break;
1734 default:
1735 SETERRNO(EVMSERR,__vmssts);
1736 }
1737 }
1738 }
1739 break;
1740 }
1741#endif
79072805
LW
1742 if (val < 0) {
1743 val = -val;
1744 while (++mark <= sp) {
0bcc34c2 1745 const I32 proc = SvIVx(*mark);
20408e3c 1746 APPLY_TAINT_PROPER();
fe14fcc3 1747#ifdef HAS_KILLPG
3028581b 1748 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1749#else
3028581b 1750 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1751#endif
79072805 1752 tot--;
a687059c 1753 }
79072805
LW
1754 }
1755 else {
1756 while (++mark <= sp) {
0bcc34c2 1757 const I32 proc = SvIVx(*mark);
20408e3c
GS
1758 APPLY_TAINT_PROPER();
1759 if (PerlProc_kill(proc, val))
79072805 1760 tot--;
a687059c
LW
1761 }
1762 }
1763 break;
b1248f16 1764#endif
79072805 1765 case OP_UNLINK:
20408e3c 1766 APPLY_TAINT_PROPER();
79072805
LW
1767 tot = sp - mark;
1768 while (++mark <= sp) {
e62f0680 1769 s = SvPV_nolen_const(*mark);
20408e3c 1770 APPLY_TAINT_PROPER();
3280af22 1771 if (PL_euid || PL_unsafe) {
b8ffc8df 1772 if (UNLINK(s))
a687059c
LW
1773 tot--;
1774 }
1775 else { /* don't let root wipe out directories without -U */
3280af22 1776 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c
LW
1777 tot--;
1778 else {
b8ffc8df 1779 if (UNLINK(s))
a687059c
LW
1780 tot--;
1781 }
1782 }
1783 }
1784 break;
e96b369d 1785#if defined(HAS_UTIME) || defined(HAS_FUTIMES)
79072805 1786 case OP_UTIME:
20408e3c 1787 APPLY_TAINT_PROPER();
79072805 1788 if (sp - mark > 2) {
e96b369d
GA
1789#if defined(HAS_FUTIMES)
1790 struct timeval utbuf[2];
1791 void *utbufp = utbuf;
1792#elif defined(I_UTIME) || defined(VMS)
663a0e37 1793 struct utimbuf utbuf;
07409e01 1794 struct utimbuf *utbufp = &utbuf;
663a0e37 1795#else
a687059c 1796 struct {
dd2821f6
GS
1797 Time_t actime;
1798 Time_t modtime;
a687059c 1799 } utbuf;
07409e01 1800 void *utbufp = &utbuf;
663a0e37 1801#endif
a687059c 1802
0bcc34c2
AL
1803 SV* const accessed = *++mark;
1804 SV* const modified = *++mark;
c6f7b413 1805
6ec06612
SB
1806 /* Be like C, and if both times are undefined, let the C
1807 * library figure out what to do. This usually means
1808 * "current time". */
c6f7b413
RS
1809
1810 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
6ec06612
SB
1811 utbufp = NULL;
1812 else {
1813 Zero(&utbuf, sizeof utbuf, char);
e96b369d
GA
1814#ifdef HAS_FUTIMES
1815 utbuf[0].tv_sec = (long)SvIVx(accessed); /* time accessed */
1816 utbuf[0].tv_usec = 0;
1817 utbuf[1].tv_sec = (long)SvIVx(modified); /* time modified */
1818 utbuf[1].tv_usec = 0;
1819#elif defined(BIG_TIME)
6ec06612
SB
1820 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1821 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
517844ec 1822#else
6ec06612
SB
1823 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1824 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
517844ec 1825#endif
6ec06612 1826 }
4373e329 1827 APPLY_TAINT_PROPER();
79072805
LW
1828 tot = sp - mark;
1829 while (++mark <= sp) {
e96b369d
GA
1830 GV* gv;
1831 if (SvTYPE(*mark) == SVt_PVGV) {
1832 gv = (GV*)*mark;
1833 do_futimes:
1834 if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1835#ifdef HAS_FUTIMES
1836 APPLY_TAINT_PROPER();
1837 if (futimes(PerlIO_fileno(IoIFP(GvIOn(gv))), utbufp))
1838 tot--;
1839#else
1840 Perl_die(aTHX_ PL_no_func, "futimes");
1841#endif
1842 }
1843 else {
1844 tot--;
1845 }
1846 }
1847 else if (SvROK(*mark) && SvTYPE(SvRV(*mark)) == SVt_PVGV) {
1848 gv = (GV*)SvRV(*mark);
1849 goto do_futimes;
1850 }
1851 else {
0bcc34c2 1852 const char * const name = SvPV_nolen_const(*mark);
e96b369d
GA
1853 APPLY_TAINT_PROPER();
1854#ifdef HAS_FUTIMES
1855 if (utimes(name, utbufp))
1856#else
1857 if (PerlLIO_utime(name, utbufp))
1858#endif
1859 tot--;
1860 }
1861
a687059c 1862 }
a687059c
LW
1863 }
1864 else
79072805 1865 tot = 0;
a687059c 1866 break;
a0d0e21e 1867#endif
a687059c
LW
1868 }
1869 return tot;
20408e3c 1870
20408e3c 1871#undef APPLY_TAINT_PROPER
a687059c
LW
1872}
1873
1874/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1875#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1876bool
ae1951c1
NC
1877Perl_cando(pTHX_ Mode_t mode, bool effective, register const Stat_t *statbufp)
1878/* effective is a flag, true for EUID, or for checking if the effective gid
1879 * is in the list of groups returned from getgroups().
1880 */
a687059c 1881{
97aff369 1882 dVAR;
bee1dbe2 1883#ifdef DOSISH
fe14fcc3
LW
1884 /* [Comments and code from Len Reed]
1885 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1886 * to write-protected files. The execute permission bit is set
1887 * by the Miscrosoft C library stat() function for the following:
1888 * .exe files
1889 * .com files
1890 * .bat files
1891 * directories
1892 * All files and directories are readable.
1893 * Directories and special files, e.g. "CON", cannot be
1894 * write-protected.
1895 * [Comment by Tom Dinger -- a directory can have the write-protect
1896 * bit set in the file system, but DOS permits changes to
1897 * the directory anyway. In addition, all bets are off
1898 * here for networked software, such as Novell and
1899 * Sun's PC-NFS.]
1900 */
1901
bee1dbe2
LW
1902 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1903 * too so it will actually look into the files for magic numbers
1904 */
7f4774ae 1905 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1906
55497cff 1907#else /* ! DOSISH */
3280af22 1908 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1909 if (mode == S_IXUSR) {
c623bd54 1910 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c
LW
1911 return TRUE;
1912 }
1913 else
1914 return TRUE; /* root reads and writes anything */
1915 return FALSE;
1916 }
3280af22 1917 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1918 if (statbufp->st_mode & mode)
a687059c
LW
1919 return TRUE; /* ok as "user" */
1920 }
d8eceb89 1921 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1922 if (statbufp->st_mode & mode >> 3)
a687059c
LW
1923 return TRUE; /* ok as "group" */
1924 }
7f4774ae 1925 else if (statbufp->st_mode & mode >> 6)
a687059c
LW
1926 return TRUE; /* ok as "other" */
1927 return FALSE;
55497cff 1928#endif /* ! DOSISH */
a687059c 1929}
a0d0e21e 1930#endif /* ! VMS */
a687059c 1931
d8eceb89 1932bool
ae1951c1 1933Perl_ingroup(pTHX_ Gid_t testgid, bool effective)
a687059c 1934{
cd39f2b6
JH
1935#ifdef MACOS_TRADITIONAL
1936 /* This is simply not correct for AppleShare, but fix it yerself. */
1937 return TRUE;
1938#else
97aff369 1939 dVAR;
3280af22 1940 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1941 return TRUE;
fe14fcc3 1942#ifdef HAS_GETGROUPS
a687059c 1943 {
331b57bc 1944 Groups_t *gary = NULL;
79072805 1945 I32 anum;
331b57bc 1946 bool rc = FALSE;
a687059c 1947
331b57bc
SP
1948 anum = getgroups(0, gary);
1949 Newx(gary, anum, Groups_t);
1950 anum = getgroups(anum, gary);
a687059c 1951 while (--anum >= 0)
331b57bc
SP
1952 if (gary[anum] == testgid) {
1953 rc = TRUE;
1954 break;
1955 }
1956
1957 Safefree(gary);
1958 return rc;
a687059c 1959 }
c685562b 1960#else
a687059c 1961 return FALSE;
cd39f2b6 1962#endif
c685562b 1963#endif
a687059c 1964}
c2ab57d4 1965
fe14fcc3 1966#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1967
79072805 1968I32
864dbfa3 1969Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1970{
97aff369 1971 dVAR;
0bcc34c2 1972 const key_t key = (key_t)SvNVx(*++mark);
6867be6d
AL
1973 const I32 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1974 const I32 flags = SvIVx(*++mark);
1975 (void)sp;
c2ab57d4 1976
748a9306 1977 SETERRNO(0,0);
c2ab57d4
LW
1978 switch (optype)
1979 {
fe14fcc3 1980#ifdef HAS_MSG
79072805 1981 case OP_MSGGET:
c2ab57d4 1982 return msgget(key, flags);
e5d73d77 1983#endif
fe14fcc3 1984#ifdef HAS_SEM
79072805 1985 case OP_SEMGET:
c2ab57d4 1986 return semget(key, n, flags);
e5d73d77 1987#endif
fe14fcc3 1988#ifdef HAS_SHM
79072805 1989 case OP_SHMGET:
c2ab57d4 1990 return shmget(key, n, flags);
e5d73d77 1991#endif
fe14fcc3 1992#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1993 default:
cea2e8a9 1994 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1995#endif
c2ab57d4
LW
1996 }
1997 return -1; /* should never happen */
1998}
1999
79072805 2000I32
864dbfa3 2001Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2002{
97aff369 2003 dVAR;
c2ab57d4 2004 char *a;
a0d0e21e 2005 I32 ret = -1;
6867be6d
AL
2006 const I32 id = SvIVx(*++mark);
2007 const I32 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2008 const I32 cmd = SvIVx(*++mark);
0bcc34c2
AL
2009 SV * const astr = *++mark;
2010 STRLEN infosize = 0;
2011 I32 getinfo = (cmd == IPC_STAT);
c2ab57d4 2012
0bcc34c2 2013 PERL_UNUSED_ARG(sp);
c2ab57d4
LW
2014
2015 switch (optype)
2016 {
fe14fcc3 2017#ifdef HAS_MSG
79072805 2018 case OP_MSGCTL:
c2ab57d4
LW
2019 if (cmd == IPC_STAT || cmd == IPC_SET)
2020 infosize = sizeof(struct msqid_ds);
2021 break;
e5d73d77 2022#endif
fe14fcc3 2023#ifdef HAS_SHM
79072805 2024 case OP_SHMCTL:
c2ab57d4
LW
2025 if (cmd == IPC_STAT || cmd == IPC_SET)
2026 infosize = sizeof(struct shmid_ds);
2027 break;
e5d73d77 2028#endif
fe14fcc3 2029#ifdef HAS_SEM
79072805 2030 case OP_SEMCTL:
39398f3f 2031#ifdef Semctl
c2ab57d4
LW
2032 if (cmd == IPC_STAT || cmd == IPC_SET)
2033 infosize = sizeof(struct semid_ds);
2034 else if (cmd == GETALL || cmd == SETALL)
2035 {
8e591e46 2036 struct semid_ds semds;
bd89102f 2037 union semun semun;
e6f0bdd6
GS
2038#ifdef EXTRA_F_IN_SEMUN_BUF
2039 semun.buff = &semds;
2040#else
84902520 2041 semun.buf = &semds;
e6f0bdd6 2042#endif
c2ab57d4 2043 getinfo = (cmd == GETALL);
9b89d93d
GB
2044 if (Semctl(id, 0, IPC_STAT, semun) == -1)
2045 return -1;
6e21c824
LW
2046 infosize = semds.sem_nsems * sizeof(short);
2047 /* "short" is technically wrong but much more portable
2048 than guessing about u_?short(_t)? */
c2ab57d4 2049 }
39398f3f 2050#else
cea2e8a9 2051 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2052#endif
c2ab57d4 2053 break;
e5d73d77 2054#endif
fe14fcc3 2055#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 2056 default:
cea2e8a9 2057 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 2058#endif
c2ab57d4
LW
2059 }
2060
2061 if (infosize)
2062 {
2063 if (getinfo)
2064 {
93524f2b 2065 SvPV_force_nolen(astr);
a0d0e21e 2066 a = SvGROW(astr, infosize+1);
c2ab57d4
LW
2067 }
2068 else
2069 {
93524f2b 2070 STRLEN len;
463ee0b2
LW
2071 a = SvPV(astr, len);
2072 if (len != infosize)
cea2e8a9 2073 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091
JH
2074 PL_op_desc[optype],
2075 (unsigned long)len,
2076 (long)infosize);
c2ab57d4
LW
2077 }
2078 }
2079 else
2080 {
0bcc34c2 2081 const IV i = SvIV(astr);
56431972 2082 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 2083 }
748a9306 2084 SETERRNO(0,0);
c2ab57d4
LW
2085 switch (optype)
2086 {
fe14fcc3 2087#ifdef HAS_MSG
79072805 2088 case OP_MSGCTL:
bee1dbe2 2089 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 2090 break;
e5d73d77 2091#endif
fe14fcc3 2092#ifdef HAS_SEM
bd89102f 2093 case OP_SEMCTL: {
39398f3f 2094#ifdef Semctl
bd89102f
AD
2095 union semun unsemds;
2096
e6f0bdd6
GS
2097#ifdef EXTRA_F_IN_SEMUN_BUF
2098 unsemds.buff = (struct semid_ds *)a;
2099#else
bd89102f 2100 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 2101#endif
bd89102f 2102 ret = Semctl(id, n, cmd, unsemds);
39398f3f 2103#else
cea2e8a9 2104 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2105#endif
bd89102f 2106 }
c2ab57d4 2107 break;
e5d73d77 2108#endif
fe14fcc3 2109#ifdef HAS_SHM
79072805 2110 case OP_SHMCTL:
bee1dbe2 2111 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 2112 break;
e5d73d77 2113#endif
c2ab57d4
LW
2114 }
2115 if (getinfo && ret >= 0) {
79072805
LW
2116 SvCUR_set(astr, infosize);
2117 *SvEND(astr) = '\0';
a0d0e21e 2118 SvSETMAGIC(astr);
c2ab57d4
LW
2119 }
2120 return ret;
2121}
2122
79072805 2123I32
864dbfa3 2124Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 2125{
97aff369 2126 dVAR;
fe14fcc3 2127#ifdef HAS_MSG
463ee0b2 2128 STRLEN len;
6867be6d 2129 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2130 SV * const mstr = *++mark;
2131 const I32 flags = SvIVx(*++mark);
2132 const char * const mbuf = SvPV_const(mstr, len);
2133 const I32 msize = len - sizeof(long);
2134
890ce7af 2135 PERL_UNUSED_ARG(sp);
c2ab57d4 2136
0bcc34c2 2137 if (msize < 0)
cea2e8a9 2138 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 2139 SETERRNO(0,0);
bee1dbe2 2140 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 2141#else
cea2e8a9 2142 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 2143#endif
c2ab57d4
LW
2144}
2145
79072805 2146I32
864dbfa3 2147Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 2148{
fe14fcc3 2149#ifdef HAS_MSG
97aff369 2150 dVAR;
c2ab57d4
LW
2151 char *mbuf;
2152 long mtype;
6867be6d 2153 I32 msize, flags, ret;
6867be6d 2154 const I32 id = SvIVx(*++mark);
0bcc34c2 2155 SV * const mstr = *++mark;
890ce7af 2156 PERL_UNUSED_ARG(sp);
79072805 2157
c2e66d9e
GS
2158 /* suppress warning when reading into undef var --jhi */
2159 if (! SvOK(mstr))
2160 sv_setpvn(mstr, "", 0);
463ee0b2
LW
2161 msize = SvIVx(*++mark);
2162 mtype = (long)SvIVx(*++mark);
2163 flags = SvIVx(*++mark);
93524f2b 2164 SvPV_force_nolen(mstr);
a0d0e21e 2165 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 2166
748a9306 2167 SETERRNO(0,0);
bee1dbe2 2168 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 2169 if (ret >= 0) {
79072805
LW
2170 SvCUR_set(mstr, sizeof(long)+ret);
2171 *SvEND(mstr) = '\0';
41d6edb2
JH
2172#ifndef INCOMPLETE_TAINTS
2173 /* who knows who has been playing with this message? */
2174 SvTAINTED_on(mstr);
2175#endif
c2ab57d4
LW
2176 }
2177 return ret;
e5d73d77 2178#else
cea2e8a9 2179 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 2180#endif
c2ab57d4
LW
2181}
2182
79072805 2183I32
864dbfa3 2184Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 2185{
fe14fcc3 2186#ifdef HAS_SEM
97aff369 2187 dVAR;
463ee0b2 2188 STRLEN opsize;
6867be6d 2189 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2190 SV * const opstr = *++mark;
2191 const char * const opbuf = SvPV_const(opstr, opsize);
890ce7af 2192 PERL_UNUSED_ARG(sp);
c2ab57d4 2193
248ff010
NC
2194 if (opsize < 3 * SHORTSIZE
2195 || (opsize % (3 * SHORTSIZE))) {
93189314 2196 SETERRNO(EINVAL,LIB_INVARG);
c2ab57d4
LW
2197 return -1;
2198 }
748a9306 2199 SETERRNO(0,0);
248ff010
NC
2200 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2201 {
6867be6d 2202 const int nsops = opsize / (3 * sizeof (short));
248ff010 2203 int i = nsops;
0bcc34c2 2204 short * const ops = (short *) opbuf;
248ff010
NC
2205 short *o = ops;
2206 struct sembuf *temps, *t;
2207 I32 result;
2208
a02a5408 2209 Newx (temps, nsops, struct sembuf);
248ff010
NC
2210 t = temps;
2211 while (i--) {
2212 t->sem_num = *o++;
2213 t->sem_op = *o++;
2214 t->sem_flg = *o++;
2215 t++;
2216 }
2217 result = semop(id, temps, nsops);
2218 t = temps;
2219 o = ops;
2220 i = nsops;
2221 while (i--) {
2222 *o++ = t->sem_num;
2223 *o++ = t->sem_op;
2224 *o++ = t->sem_flg;
2225 t++;
2226 }
2227 Safefree(temps);
2228 return result;
2229 }
e5d73d77 2230#else
cea2e8a9 2231 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2232#endif
c2ab57d4
LW
2233}
2234
79072805 2235I32
864dbfa3 2236Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2237{
fe14fcc3 2238#ifdef HAS_SHM
97aff369 2239 dVAR;
4373e329 2240 char *shm;
c2ab57d4 2241 struct shmid_ds shmds;
6867be6d 2242 const I32 id = SvIVx(*++mark);
0bcc34c2
AL
2243 SV * const mstr = *++mark;
2244 const I32 mpos = SvIVx(*++mark);
2245 const I32 msize = SvIVx(*++mark);
890ce7af 2246 PERL_UNUSED_ARG(sp);
c2ab57d4 2247
748a9306 2248 SETERRNO(0,0);
c2ab57d4
LW
2249 if (shmctl(id, IPC_STAT, &shmds) == -1)
2250 return -1;
2251 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
93189314 2252 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
c2ab57d4
LW
2253 return -1;
2254 }
8ac85365 2255 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4
LW
2256 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2257 return -1;
79072805 2258 if (optype == OP_SHMREAD) {
4373e329 2259 const char *mbuf;
9f538c04
GS
2260 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2261 if (! SvOK(mstr))
2262 sv_setpvn(mstr, "", 0);
93524f2b 2263 SvPV_force_nolen(mstr);
a0d0e21e
LW
2264 mbuf = SvGROW(mstr, msize+1);
2265
bee1dbe2 2266 Copy(shm + mpos, mbuf, msize, char);
79072805
LW
2267 SvCUR_set(mstr, msize);
2268 *SvEND(mstr) = '\0';
a0d0e21e 2269 SvSETMAGIC(mstr);
d929ce6f
JH
2270#ifndef INCOMPLETE_TAINTS
2271 /* who knows who has been playing with this shared memory? */
2272 SvTAINTED_on(mstr);
2273#endif
c2ab57d4
LW
2274 }
2275 else {
79072805 2276 I32 n;
93524f2b 2277 STRLEN len;
c2ab57d4 2278
93524f2b 2279 const char *mbuf = SvPV_const(mstr, len);
463ee0b2 2280 if ((n = len) > msize)
c2ab57d4 2281 n = msize;
bee1dbe2 2282 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2283 if (n < msize)
bee1dbe2 2284 memzero(shm + mpos + n, msize - n);
c2ab57d4
LW
2285 }
2286 return shmdt(shm);
e5d73d77 2287#else
cea2e8a9 2288 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2289#endif
c2ab57d4
LW
2290}
2291
fe14fcc3 2292#endif /* SYSV IPC */
4e35701f 2293
0d44d22b 2294/*
ccfc67b7
JH
2295=head1 IO Functions
2296
0d44d22b
NC
2297=for apidoc start_glob
2298
2299Function called by C<do_readline> to spawn a glob (or do the glob inside
2300perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
210b36aa 2301this glob starter is only used by miniperl during the build process.
0d44d22b 2302Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
fab3f3a7 2303
0d44d22b
NC
2304=cut
2305*/
2306
2307PerlIO *
2308Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2309{
27da23d5 2310 dVAR;
561b68a9 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];
890ce7af
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;
95a20fc0 2358 if (!PerlLIO_stat(SvPVX_const(tmpglob),&st) && S_ISDIR(st.st_mode))
94a11853
CB
2359 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2360 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(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)) {
3ce6b0cb
CB
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 &&
93189314 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 */
93524f2b 2438 (void)do_open(PL_last_in_gv, (char*)SvPVX_const(tmpcmd), SvCUR(tmpcmd),
0d44d22b
NC
2439 FALSE, O_RDONLY, 0, Nullfp);
2440 fp = IoIFP(io);
2441#endif /* !VMS */
2442 LEAVE;
2443 return fp;
2444}
66610fdd
RGS
2445
2446/*
2447 * Local variables:
2448 * c-indentation-style: bsd
2449 * c-basic-offset: 4
2450 * indent-tabs-mode: t
2451 * End:
2452 *
37442d52
RGS
2453 * ex: set ts=8 sts=4 sw=4 noet:
2454 */