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