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