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