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