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