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