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