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