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