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