Possible cruft removal.
[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 }
54edfe98 326 /*SUPPRESS 530*/
327 for (; isSPACE(*type); type++) ;
1771866f 328 if (num_svs && (SvIOK(*svp) || (SvPOK(*svp) && looks_like_number(*svp)))) {
e620cd72 329 fd = SvUV(*svp);
24a7a40d 330 num_svs = 0;
ee518936 331 }
e620cd72 332 else if (isDIGIT(*type)) {
e620cd72 333 fd = atoi(type);
334 }
c07a80fd 335 else {
336 IO* thatio;
e620cd72 337 if (num_svs) {
338 thatio = sv_2io(*svp);
339 }
340 else {
341 GV *thatgv;
e620cd72 342 thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
343 thatio = GvIO(thatgv);
344 }
c07a80fd 345 if (!thatio) {
6e21c824 346#ifdef EINVAL
93189314 347 SETERRNO(EINVAL,SS_IVCHAN);
6e21c824 348#endif
c07a80fd 349 goto say_false;
350 }
f4e789af 351 if ((that_fp = IoIFP(thatio))) {
7211d486 352 /* Flush stdio buffer before dup. --mjd
353 * Unfortunately SEEK_CURing 0 seems to
354 * be optimized away on most platforms;
355 * only Solaris and Linux seem to flush
356 * on that. --jhi */
2c534a3f 357#ifdef USE_SFIO
358 /* sfio fails to clear error on next
359 sfwrite, contrary to documentation.
360 -- Nick Clark */
ecdeb87c 361 if (PerlIO_seek(that_fp, 0, SEEK_CUR) == -1)
362 PerlIO_clearerr(that_fp);
2c534a3f 363#endif
7211d486 364 /* On the other hand, do all platforms
365 * take gracefully to flushing a read-only
366 * filehandle? Perhaps we should do
367 * fsetpos(src)+fgetpos(dst)? --nik */
ecdeb87c 368 PerlIO_flush(that_fp);
369 fd = PerlIO_fileno(that_fp);
0759c907 370 /* When dup()ing STDIN, STDOUT or STDERR
371 * explicitly set appropriate access mode */
f4e789af 372 if (that_fp == PerlIO_stdout()
373 || that_fp == PerlIO_stderr())
0759c907 374 IoTYPE(io) = IoTYPE_WRONLY;
f4e789af 375 else if (that_fp == PerlIO_stdin())
0759c907 376 IoTYPE(io) = IoTYPE_RDONLY;
377 /* When dup()ing a socket, say result is
378 * one as well */
379 else if (IoTYPE(thatio) == IoTYPE_SOCKET)
50952442 380 IoTYPE(io) = IoTYPE_SOCKET;
c07a80fd 381 }
382 else
383 fd = -1;
a0d0e21e 384 }
ee518936 385 if (!num_svs)
1141d9f8 386 type = Nullch;
ecdeb87c 387 if (that_fp) {
388 fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
389 }
390 else {
c07a80fd 391 if (dodup)
ecdeb87c 392 fd = PerlLIO_dup(fd);
393 else
394 was_fdopen = TRUE;
395 if (!(fp = PerlIO_openn(aTHX_ type,mode,fd,0,0,NULL,num_svs,svp))) {
396 if (dodup)
397 PerlLIO_close(fd);
398 }
faecd977 399 }
c07a80fd 400 }
ee518936 401 } /* & */
c07a80fd 402 else {
403 /*SUPPRESS 530*/
6170680b 404 for (; isSPACE(*type); type++) ;
b931b1d9 405 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
406 /*SUPPRESS 530*/
407 type++;
760ac839 408 fp = PerlIO_stdout();
50952442 409 IoTYPE(io) = IoTYPE_STD;
7cf31beb 410 if (num_svs > 1) {
411 Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
412 }
c07a80fd 413 }
414 else {
ee518936 415 if (!num_svs) {
416 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
417 num_svs = 1;
418 svp = &namesv;
1141d9f8 419 type = Nullch;
ee518936 420 }
6e60e805 421 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
c07a80fd 422 }
ee518936 423 } /* !& */
bf38876a 424 }
9f37169a 425 else if (*type == IoTYPE_RDONLY) {
c07a80fd 426 /*SUPPRESS 530*/
6170680b 427 for (type++; isSPACE(*type); type++) ;
bf38876a 428 mode[0] = 'r';
16fe6d59 429 if (in_raw)
430 strcat(mode, "b");
431 else if (in_crlf)
432 strcat(mode, "t");
433
6170680b 434 if (*type == '&') {
bf38876a 435 goto duplicity;
6170680b 436 }
b931b1d9 437 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
438 /*SUPPRESS 530*/
439 type++;
760ac839 440 fp = PerlIO_stdin();
50952442 441 IoTYPE(io) = IoTYPE_STD;
7cf31beb 442 if (num_svs > 1) {
443 Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
444 }
a687059c 445 }
ee518936 446 else {
447 if (!num_svs) {
448 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
449 num_svs = 1;
450 svp = &namesv;
1141d9f8 451 type = Nullch;
ee518936 452 }
6e60e805 453 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
ee518936 454 }
a687059c 455 }
b931b1d9 456 else if ((num_svs && type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
457 (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
6170680b 458 if (num_svs) {
b931b1d9 459 type += 2; /* skip over '-|' */
6170680b 460 }
461 else {
b931b1d9 462 *--tend = '\0';
463 while (tend > type && isSPACE(tend[-1]))
464 *--tend = '\0';
6170680b 465 /*SUPPRESS 530*/
466 for (; isSPACE(*type); type++) ;
467 name = type;
b931b1d9 468 len = tend-type;
6170680b 469 }
4a7d1889 470 if (*name == '\0') {
471 /* command is missing 19990114 */
06eaf0bc 472 if (ckWARN(WARN_PIPE))
9014280d 473 Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
06eaf0bc 474 errno = EPIPE;
475 goto say_false;
476 }
6170680b 477 if (strNE(name,"-") || num_svs)
79072805 478 TAINT_ENV();
479 TAINT_PROPER("piped open");
a1d180c4 480 mode[0] = 'r';
481 if (in_raw)
482 strcat(mode, "b");
483 else if (in_crlf)
484 strcat(mode, "t");
4a7d1889 485 if (num_svs > 1) {
486 fp = PerlProc_popen_list(mode,num_svs,svp);
487 }
e620cd72 488 else {
4a7d1889 489 fp = PerlProc_popen(name,mode);
490 }
50952442 491 IoTYPE(io) = IoTYPE_PIPE;
1771866f 492 if (num_svs) {
493 for (; isSPACE(*type); type++) ;
494 if (*type) {
495 if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
496 goto say_false;
497 }
498 }
499 }
a687059c 500 }
501 else {
6170680b 502 if (num_svs)
503 goto unknown_desr;
504 name = type;
50952442 505 IoTYPE(io) = IoTYPE_RDONLY;
99b89507 506 /*SUPPRESS 530*/
507 for (; isSPACE(*name); name++) ;
88b61e10 508 mode[0] = 'r';
509 if (in_raw)
510 strcat(mode, "b");
511 else if (in_crlf)
512 strcat(mode, "t");
a687059c 513 if (strEQ(name,"-")) {
760ac839 514 fp = PerlIO_stdin();
50952442 515 IoTYPE(io) = IoTYPE_STD;
a687059c 516 }
16fe6d59 517 else {
ee518936 518 if (!num_svs) {
519 namesv = sv_2mortal(newSVpvn(type,strlen(type)));
520 num_svs = 1;
521 svp = &namesv;
1141d9f8 522 type = Nullch;
ee518936 523 }
6e60e805 524 fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
16fe6d59 525 }
a687059c 526 }
527 }
bee1dbe2 528 if (!fp) {
50952442 529 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == IoTYPE_RDONLY && strchr(name, '\n'))
9014280d 530 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
6e21c824 531 goto say_false;
bee1dbe2 532 }
a00b5bd3 533
534 if (ckWARN(WARN_IO)) {
535 if ((IoTYPE(io) == IoTYPE_RDONLY) &&
536 (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
9014280d 537 Perl_warner(aTHX_ packWARN(WARN_IO),
97828cef 538 "Filehandle STD%s reopened as %s only for input",
539 ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
540 GvENAME(gv));
a00b5bd3 541 }
ee518936 542 else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
9014280d 543 Perl_warner(aTHX_ packWARN(WARN_IO),
97828cef 544 "Filehandle STDIN reopened as %s only for output",
545 GvENAME(gv));
a00b5bd3 546 }
547 }
548
e99cca91 549 fd = PerlIO_fileno(fp);
e934609f 550 /* If there is no fd (e.g. PerlIO::scalar) assume it isn't a
551 * socket - this covers PerlIO::scalar - otherwise unless we "know" the
e99cca91 552 * type probe for socket-ness.
553 */
554 if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
555 if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
556 /* If PerlIO claims to have fd we had better be able to fstat() it. */
557 (void) PerlIO_close(fp);
6e21c824 558 goto say_false;
a687059c 559 }
7114a2d2 560#ifndef PERL_MICRO
3280af22 561 if (S_ISSOCK(PL_statbuf.st_mode))
50952442 562 IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
99b89507 563#ifdef HAS_SOCKET
564 else if (
c623bd54 565#ifdef S_IFMT
3280af22 566 !(PL_statbuf.st_mode & S_IFMT)
99b89507 567#else
b28d0864 568 !PL_statbuf.st_mode
99b89507 569#endif
0759c907 570 && IoTYPE(io) != IoTYPE_WRONLY /* Dups of STD* filehandles already have */
571 && IoTYPE(io) != IoTYPE_RDONLY /* type so they aren't marked as sockets */
572 ) { /* on OS's that return 0 on fstat()ed pipe */
e99cca91 573 char tmpbuf[256];
574 Sock_size_t buflen = sizeof tmpbuf;
575 if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
576 || errno != ENOTSOCK)
577 IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
578 /* but some return 0 for streams too, sigh */
99b89507 579 }
e99cca91 580#endif /* HAS_SOCKET */
7114a2d2 581#endif /* !PERL_MICRO */
a687059c 582 }
e99cca91 583
584 /* Eeek - FIXME !!!
585 * If this is a standard handle we discard all the layer stuff
586 * and just dup the fd into whatever was on the handle before !
587 */
588
6e21c824 589 if (saveifp) { /* must use old fp? */
f5b9d040 590 /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
24c23ab4 591 then dup the new fileno down
f5b9d040 592 */
6e21c824 593 if (saveofp) {
f5b9d040 594 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
6e21c824 595 if (saveofp != saveifp) { /* was a socket? */
760ac839 596 PerlIO_close(saveofp);
6e21c824 597 }
598 }
6e60e805 599 if (savefd != fd) {
e934609f 600 /* Still a small can-of-worms here if (say) PerlIO::scalar
ecdeb87c 601 is assigned to (say) STDOUT - for now let dup2() fail
602 and provide the error
603 */
bd4a5668 604 if (PerlLIO_dup2(fd, savefd) < 0) {
605 (void)PerlIO_close(fp);
606 goto say_false;
607 }
d082dcd6 608#ifdef VMS
6e60e805 609 if (savefd != PerlIO_fileno(PerlIO_stdin())) {
d0e2cf63 610 char newname[FILENAME_MAX+1];
611 if (PerlIO_getname(fp, newname)) {
612 if (fd == PerlIO_fileno(PerlIO_stdout()))
613 Perl_vmssetuserlnm(aTHX_ "SYS$OUTPUT", newname);
614 if (fd == PerlIO_fileno(PerlIO_stderr()))
615 Perl_vmssetuserlnm(aTHX_ "SYS$ERROR", newname);
616 }
d082dcd6 617 }
618#endif
d0e2cf63 619
620#if !defined(WIN32)
621 /* PL_fdpid isn't used on Windows, so avoid this useless work.
622 * XXX Probably the same for a lot of other places. */
623 {
624 Pid_t pid;
625 SV *sv;
626
627 LOCK_FDPID_MUTEX;
628 sv = *av_fetch(PL_fdpid,fd,TRUE);
629 (void)SvUPGRADE(sv, SVt_IV);
630 pid = SvIVX(sv);
631 SvIVX(sv) = 0;
632 sv = *av_fetch(PL_fdpid,savefd,TRUE);
633 (void)SvUPGRADE(sv, SVt_IV);
634 SvIVX(sv) = pid;
635 UNLOCK_FDPID_MUTEX;
636 }
637#endif
638
e212fc47 639 if (was_fdopen) {
640 /* need to close fp without closing underlying fd */
641 int ofd = PerlIO_fileno(fp);
642 int dupfd = PerlLIO_dup(ofd);
643 PerlIO_close(fp);
644 PerlLIO_dup2(dupfd,ofd);
645 PerlLIO_close(dupfd);
ecdeb87c 646 }
e212fc47 647 else
648 PerlIO_close(fp);
6e21c824 649 }
650 fp = saveifp;
760ac839 651 PerlIO_clearerr(fp);
e99cca91 652 fd = PerlIO_fileno(fp);
6e21c824 653 }
a0d0e21e 654#if defined(HAS_FCNTL) && defined(F_SETFD)
e99cca91 655 if (fd >= 0) {
a8710ca1 656 int save_errno = errno;
a8710ca1 657 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
658 errno = save_errno;
659 }
1462b684 660#endif
8990e307 661 IoIFP(io) = fp;
b931b1d9 662
684bef36 663 IoFLAGS(io) &= ~IOf_NOLINE;
bf38876a 664 if (writing) {
50952442 665 if (IoTYPE(io) == IoTYPE_SOCKET
e99cca91 666 || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
a33cf58c 667 char *s = mode;
668 if (*s == 'I' || *s == '#')
669 s++;
670 *s = 'w';
1feb1812 671 if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,svp))) {
760ac839 672 PerlIO_close(fp);
8990e307 673 IoIFP(io) = Nullfp;
6e21c824 674 goto say_false;
fe14fcc3 675 }
1462b684 676 }
677 else
8990e307 678 IoOFP(io) = fp;
bf38876a 679 }
a687059c 680 return TRUE;
6e21c824 681
682say_false:
8990e307 683 IoIFP(io) = saveifp;
684 IoOFP(io) = saveofp;
685 IoTYPE(io) = savetype;
6e21c824 686 return FALSE;
a687059c 687}
688
760ac839 689PerlIO *
864dbfa3 690Perl_nextargv(pTHX_ register GV *gv)
a687059c 691{
79072805 692 register SV *sv;
99b89507 693#ifndef FLEXFILENAMES
c623bd54 694 int filedev;
695 int fileino;
99b89507 696#endif
761237fe 697 Uid_t fileuid;
698 Gid_t filegid;
18708f5a 699 IO *io = GvIOp(gv);
fe14fcc3 700
3280af22 701 if (!PL_argvoutgv)
702 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
18708f5a 703 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
704 IoFLAGS(io) &= ~IOf_START;
7a1c5554 705 if (PL_inplace) {
706 if (!PL_argvout_stack)
707 PL_argvout_stack = newAV();
18708f5a 708 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
7a1c5554 709 }
18708f5a 710 }
3280af22 711 if (PL_filemode & (S_ISUID|S_ISGID)) {
712 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 713#ifdef HAS_FCHMOD
3280af22 714 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 715#else
b28d0864 716 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3 717#endif
718 }
3280af22 719 PL_filemode = 0;
79072805 720 while (av_len(GvAV(gv)) >= 0) {
85aff577 721 STRLEN oldlen;
79072805 722 sv = av_shift(GvAV(gv));
8990e307 723 SAVEFREESV(sv);
79072805 724 sv_setsv(GvSV(gv),sv);
725 SvSETMAGIC(GvSV(gv));
3280af22 726 PL_oldname = SvPVx(GvSV(gv), oldlen);
9d116dd7 727 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
3280af22 728 if (PL_inplace) {
79072805 729 TAINT_PROPER("inplace open");
3280af22 730 if (oldlen == 1 && *PL_oldname == '-') {
4633a7c4 731 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a0d0e21e 732 return IoIFP(GvIOp(gv));
c623bd54 733 }
99b89507 734#ifndef FLEXFILENAMES
b28d0864 735 filedev = PL_statbuf.st_dev;
736 fileino = PL_statbuf.st_ino;
99b89507 737#endif
3280af22 738 PL_filemode = PL_statbuf.st_mode;
739 fileuid = PL_statbuf.st_uid;
740 filegid = PL_statbuf.st_gid;
741 if (!S_ISREG(PL_filemode)) {
0453d815 742 if (ckWARN_d(WARN_INPLACE))
9014280d 743 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
0453d815 744 "Can't do inplace edit: %s is not a regular file",
745 PL_oldname );
79072805 746 do_close(gv,FALSE);
c623bd54 747 continue;
748 }
3280af22 749 if (*PL_inplace) {
750 char *star = strchr(PL_inplace, '*');
2d259d92 751 if (star) {
3280af22 752 char *begin = PL_inplace;
2d259d92 753 sv_setpvn(sv, "", 0);
754 do {
755 sv_catpvn(sv, begin, star - begin);
3280af22 756 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92 757 begin = ++star;
758 } while ((star = strchr(begin, '*')));
3d66d7bb 759 if (*begin)
760 sv_catpv(sv,begin);
2d259d92 761 }
762 else {
3280af22 763 sv_catpv(sv,PL_inplace);
2d259d92 764 }
c623bd54 765#ifndef FLEXFILENAMES
5f74f29c 766 if ((PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
767 && PL_statbuf.st_dev == filedev
768 && PL_statbuf.st_ino == fileino)
39e571d4 769#ifdef DJGPP
5f74f29c 770 || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
39e571d4 771#endif
f248d071 772 )
773 {
774 if (ckWARN_d(WARN_INPLACE))
9014280d 775 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
35c1215d 776 "Can't do inplace edit: %"SVf" would not be unique",
777 sv);
79072805 778 do_close(gv,FALSE);
c623bd54 779 continue;
780 }
781#endif
fe14fcc3 782#ifdef HAS_RENAME
2585f9a3 783#if !defined(DOSISH) && !defined(__CYGWIN__) && !defined(EPOC)
3280af22 784 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
0453d815 785 if (ckWARN_d(WARN_INPLACE))
9014280d 786 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
35c1215d 787 "Can't rename %s to %"SVf": %s, skipping file",
788 PL_oldname, sv, Strerror(errno) );
79072805 789 do_close(gv,FALSE);
c623bd54 790 continue;
791 }
a687059c 792#else
79072805 793 do_close(gv,FALSE);
3028581b 794 (void)PerlLIO_unlink(SvPVX(sv));
b28d0864 795 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
9d116dd7 796 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
55497cff 797#endif /* DOSISH */
ff8e2863 798#else
463ee0b2 799 (void)UNLINK(SvPVX(sv));
b28d0864 800 if (link(PL_oldname,SvPVX(sv)) < 0) {
0453d815 801 if (ckWARN_d(WARN_INPLACE))
9014280d 802 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
35c1215d 803 "Can't rename %s to %"SVf": %s, skipping file",
804 PL_oldname, sv, Strerror(errno) );
79072805 805 do_close(gv,FALSE);
c623bd54 806 continue;
807 }
b28d0864 808 (void)UNLINK(PL_oldname);
a687059c 809#endif
810 }
811 else {
c030f24b 812#if !defined(DOSISH) && !defined(AMIGAOS)
edc7bc49 813# ifndef VMS /* Don't delete; use automatic file versioning */
3280af22 814 if (UNLINK(PL_oldname) < 0) {
0453d815 815 if (ckWARN_d(WARN_INPLACE))
9014280d 816 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
0453d815 817 "Can't remove %s: %s, skipping file",
818 PL_oldname, Strerror(errno) );
79072805 819 do_close(gv,FALSE);
fe14fcc3 820 continue;
821 }
edc7bc49 822# endif
ff8e2863 823#else
cea2e8a9 824 Perl_croak(aTHX_ "Can't do inplace edit without backup");
ff8e2863 825#endif
a687059c 826 }
827
3280af22 828 sv_setpvn(sv,">",!PL_inplace);
829 sv_catpvn(sv,PL_oldname,oldlen);
748a9306 830 SETERRNO(0,0); /* in case sprintf set errno */
4119ab01 831#ifdef VMS
832 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
18708f5a 833 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
4119ab01 834#else
3280af22 835 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
18708f5a 836 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
4119ab01 837#endif
18708f5a 838 {
0453d815 839 if (ckWARN_d(WARN_INPLACE))
9014280d 840 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
0453d815 841 PL_oldname, Strerror(errno) );
79072805 842 do_close(gv,FALSE);
fe14fcc3 843 continue;
844 }
3280af22 845 setdefout(PL_argvoutgv);
846 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
847 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
fe14fcc3 848#ifdef HAS_FCHMOD
3280af22 849 (void)fchmod(PL_lastfd,PL_filemode);
a687059c 850#else
3e3baf6d 851# if !(defined(WIN32) && defined(__BORLANDC__))
852 /* Borland runtime creates a readonly file! */
b28d0864 853 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
3e3baf6d 854# endif
a687059c 855#endif
3280af22 856 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
fe14fcc3 857#ifdef HAS_FCHOWN
3280af22 858 (void)fchown(PL_lastfd,fileuid,filegid);
a687059c 859#else
fe14fcc3 860#ifdef HAS_CHOWN
b28d0864 861 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
a687059c 862#endif
b1248f16 863#endif
fe14fcc3 864 }
a687059c 865 }
a0d0e21e 866 return IoIFP(GvIOp(gv));
a687059c 867 }
4d61ec05 868 else {
4d61ec05 869 if (ckWARN_d(WARN_INPLACE)) {
6af84f9f 870 int eno = errno;
871 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
872 && !S_ISREG(PL_statbuf.st_mode))
873 {
9014280d 874 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
4d61ec05 875 "Can't do inplace edit: %s is not a regular file",
9a7dcd9c 876 PL_oldname);
6af84f9f 877 }
4d61ec05 878 else
9014280d 879 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
6af84f9f 880 PL_oldname, Strerror(eno));
4d61ec05 881 }
882 }
a687059c 883 }
18708f5a 884 if (io && (IoFLAGS(io) & IOf_ARGV))
885 IoFLAGS(io) |= IOf_START;
3280af22 886 if (PL_inplace) {
887 (void)do_close(PL_argvoutgv,FALSE);
7a1c5554 888 if (io && (IoFLAGS(io) & IOf_ARGV)
889 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
890 {
18708f5a 891 GV *oldout = (GV*)av_pop(PL_argvout_stack);
892 setdefout(oldout);
893 SvREFCNT_dec(oldout);
894 return Nullfp;
895 }
4633a7c4 896 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a687059c 897 }
898 return Nullfp;
899}
900
fe14fcc3 901#ifdef HAS_PIPE
afd9f252 902void
864dbfa3 903Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
afd9f252 904{
79072805 905 register IO *rstio;
906 register IO *wstio;
afd9f252 907 int fd[2];
908
79072805 909 if (!rgv)
afd9f252 910 goto badexit;
79072805 911 if (!wgv)
afd9f252 912 goto badexit;
913
a0d0e21e 914 rstio = GvIOn(rgv);
915 wstio = GvIOn(wgv);
afd9f252 916
a0d0e21e 917 if (IoIFP(rstio))
79072805 918 do_close(rgv,FALSE);
a0d0e21e 919 if (IoIFP(wstio))
79072805 920 do_close(wgv,FALSE);
afd9f252 921
3028581b 922 if (PerlProc_pipe(fd) < 0)
afd9f252 923 goto badexit;
479b2847 924 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"PIPESOCK_MODE);
925 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"PIPESOCK_MODE);
b5ac89c3 926 IoOFP(rstio) = IoIFP(rstio);
8990e307 927 IoIFP(wstio) = IoOFP(wstio);
50952442 928 IoTYPE(rstio) = IoTYPE_RDONLY;
929 IoTYPE(wstio) = IoTYPE_WRONLY;
8990e307 930 if (!IoIFP(rstio) || !IoOFP(wstio)) {
760ac839 931 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
3028581b 932 else PerlLIO_close(fd[0]);
760ac839 933 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
3028581b 934 else PerlLIO_close(fd[1]);
fe14fcc3 935 goto badexit;
936 }
afd9f252 937
3280af22 938 sv_setsv(sv,&PL_sv_yes);
afd9f252 939 return;
940
941badexit:
3280af22 942 sv_setsv(sv,&PL_sv_undef);
afd9f252 943 return;
944}
b1248f16 945#endif
afd9f252 946
517844ec 947/* explicit renamed to avoid C++ conflict -- kja */
a687059c 948bool
864dbfa3 949Perl_do_close(pTHX_ GV *gv, bool not_implicit)
a687059c 950{
1193dd27 951 bool retval;
952 IO *io;
a687059c 953
79072805 954 if (!gv)
3280af22 955 gv = PL_argvgv;
a0d0e21e 956 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1d2dff63 957 if (not_implicit)
93189314 958 SETERRNO(EBADF,SS_IVCHAN);
c2ab57d4 959 return FALSE;
99b89507 960 }
79072805 961 io = GvIO(gv);
962 if (!io) { /* never opened */
1d2dff63 963 if (not_implicit) {
2dd78f96 964 if (ckWARN(WARN_UNOPENED)) /* no check for closed here */
965 report_evil_fh(gv, io, PL_op->op_type);
93189314 966 SETERRNO(EBADF,SS_IVCHAN);
1d2dff63 967 }
a687059c 968 return FALSE;
969 }
f2b5be74 970 retval = io_close(io, not_implicit);
517844ec 971 if (not_implicit) {
1193dd27 972 IoLINES(io) = 0;
973 IoPAGE(io) = 0;
974 IoLINES_LEFT(io) = IoPAGE_LEN(io);
975 }
50952442 976 IoTYPE(io) = IoTYPE_CLOSED;
1193dd27 977 return retval;
978}
979
980bool
f2b5be74 981Perl_io_close(pTHX_ IO *io, bool not_implicit)
1193dd27 982{
983 bool retval = FALSE;
984 int status;
985
8990e307 986 if (IoIFP(io)) {
50952442 987 if (IoTYPE(io) == IoTYPE_PIPE) {
3028581b 988 status = PerlProc_pclose(IoIFP(io));
f2b5be74 989 if (not_implicit) {
990 STATUS_NATIVE_SET(status);
991 retval = (STATUS_POSIX == 0);
992 }
993 else {
994 retval = (status != -1);
995 }
a687059c 996 }
50952442 997 else if (IoTYPE(io) == IoTYPE_STD)
a687059c 998 retval = TRUE;
999 else {
8990e307 1000 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
760ac839 1001 retval = (PerlIO_close(IoOFP(io)) != EOF);
1002 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4 1003 }
1004 else
760ac839 1005 retval = (PerlIO_close(IoIFP(io)) != EOF);
a687059c 1006 }
8990e307 1007 IoOFP(io) = IoIFP(io) = Nullfp;
79072805 1008 }
f2b5be74 1009 else if (not_implicit) {
93189314 1010 SETERRNO(EBADF,SS_IVCHAN);
20408e3c 1011 }
1193dd27 1012
a687059c 1013 return retval;
1014}
1015
1016bool
864dbfa3 1017Perl_do_eof(pTHX_ GV *gv)
a687059c 1018{
79072805 1019 register IO *io;
a687059c 1020 int ch;
1021
79072805 1022 io = GvIO(gv);
a687059c 1023
79072805 1024 if (!io)
a687059c 1025 return TRUE;
a00b5bd3 1026 else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
06bcfee8 1027 report_evil_fh(gv, io, OP_phoney_OUTPUT_ONLY);
a687059c 1028
8990e307 1029 while (IoIFP(io)) {
a687059c 1030
760ac839 1031 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
a20bf0c3 1032 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
760ac839 1033 return FALSE; /* this is the most usual case */
1034 }
a687059c 1035
760ac839 1036 ch = PerlIO_getc(IoIFP(io));
a687059c 1037 if (ch != EOF) {
760ac839 1038 (void)PerlIO_ungetc(IoIFP(io),ch);
a687059c 1039 return FALSE;
1040 }
fab3f3a7 1041
760ac839 1042 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3 1043 if (PerlIO_get_cnt(IoIFP(io)) < -1)
1044 PerlIO_set_cnt(IoIFP(io),-1);
760ac839 1045 }
533c011a 1046 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
ed2c6b9b 1047 if (gv != PL_argvgv || !nextargv(gv)) /* get another fp handy */
a687059c 1048 return TRUE;
1049 }
1050 else
1051 return TRUE; /* normal fp, definitely end of file */
1052 }
1053 return TRUE;
1054}
1055
5ff3f7a4 1056Off_t
864dbfa3 1057Perl_do_tell(pTHX_ GV *gv)
a687059c 1058{
9c5ffd7c 1059 register IO *io = 0;
96e4d5b1 1060 register PerlIO *fp;
a687059c 1061
96e4d5b1 1062 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 1063#ifdef ULTRIX_STDIO_BOTCH
96e4d5b1 1064 if (PerlIO_eof(fp))
1065 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 1066#endif
8903cb82 1067 return PerlIO_tell(fp);
96e4d5b1 1068 }
411caa50 1069 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1070 report_evil_fh(gv, io, PL_op->op_type);
93189314 1071 SETERRNO(EBADF,RMS_IFI);
5ff3f7a4 1072 return (Off_t)-1;
a687059c 1073}
1074
1075bool
864dbfa3 1076Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
a687059c 1077{
9c5ffd7c 1078 register IO *io = 0;
137443ea 1079 register PerlIO *fp;
a687059c 1080
137443ea 1081 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 1082#ifdef ULTRIX_STDIO_BOTCH
137443ea 1083 if (PerlIO_eof(fp))
1084 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 1085#endif
8903cb82 1086 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 1087 }
411caa50 1088 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1089 report_evil_fh(gv, io, PL_op->op_type);
93189314 1090 SETERRNO(EBADF,RMS_IFI);
a687059c 1091 return FALSE;
1092}
1093
97cc44eb 1094Off_t
864dbfa3 1095Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
8903cb82 1096{
9c5ffd7c 1097 register IO *io = 0;
8903cb82 1098 register PerlIO *fp;
1099
1100 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
3028581b 1101 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
411caa50 1102 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
1103 report_evil_fh(gv, io, PL_op->op_type);
93189314 1104 SETERRNO(EBADF,RMS_IFI);
d9b3e12d 1105 return (Off_t)-1;
8903cb82 1106}
1107
6ff81951 1108int
16fe6d59 1109Perl_mode_from_discipline(pTHX_ SV *discp)
1110{
1111 int mode = O_BINARY;
1112 if (discp) {
1113 STRLEN len;
1114 char *s = SvPV(discp,len);
1115 while (*s) {
1116 if (*s == ':') {
1117 switch (s[1]) {
1118 case 'r':
1119 if (len > 3 && strnEQ(s+1, "raw", 3)
1120 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1121 {
1122 mode = O_BINARY;
1123 s += 4;
1124 len -= 4;
1125 break;
1126 }
1127 /* FALL THROUGH */
1128 case 'c':
1129 if (len > 4 && strnEQ(s+1, "crlf", 4)
1130 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1131 {
1132 mode = O_TEXT;
1133 s += 5;
1134 len -= 5;
1135 break;
1136 }
1137 /* FALL THROUGH */
1138 default:
1139 goto fail_discipline;
1140 }
1141 }
1142 else if (isSPACE(*s)) {
1143 ++s;
1144 --len;
1145 }
1146 else {
1147 char *end;
1148fail_discipline:
1149 end = strchr(s+1, ':');
1150 if (!end)
1151 end = s+len;
60382766 1152#ifndef PERLIO_LAYERS
16fe6d59 1153 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
60382766 1154#else
1155 s = end;
1156#endif
16fe6d59 1157 }
1158 }
1159 }
1160 return mode;
1161}
1162
1163int
1164Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
6ff81951 1165{
60382766 1166 /* The old body of this is now in non-LAYER part of perlio.c
1167 * This is a stub for any XS code which might have been calling it.
1168 */
6ce75a77 1169 char *name = ":raw";
35990314 1170#ifdef PERLIO_USING_CRLF
8b24e160 1171 if (!(mode & O_BINARY))
6ce75a77 1172 name = ":crlf";
1173#endif
60382766 1174 return PerlIO_binmode(aTHX_ fp, iotype, mode, name);
6ff81951 1175}
1176
a0d0e21e 1177#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
c2ab57d4 1178 /* code courtesy of William Kucharski */
fe14fcc3 1179#define HAS_CHSIZE
6eb13c3b 1180
517844ec 1181I32 my_chsize(fd, length)
79072805 1182I32 fd; /* file descriptor */
85e6fe83 1183Off_t length; /* length to set file to */
6eb13c3b 1184{
6eb13c3b 1185 struct flock fl;
c623ac67 1186 Stat_t filebuf;
6eb13c3b 1187
3028581b 1188 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b 1189 return -1;
1190
1191 if (filebuf.st_size < length) {
1192
1193 /* extend file length */
1194
3028581b 1195 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b 1196 return -1;
1197
1198 /* write a "0" byte */
1199
3028581b 1200 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b 1201 return -1;
1202 }
1203 else {
1204 /* truncate length */
1205
1206 fl.l_whence = 0;
1207 fl.l_len = 0;
1208 fl.l_start = length;
a0d0e21e 1209 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b 1210
1211 /*
a0d0e21e 1212 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b 1213 * fcntl(2), which truncates the file so that it ends at the
1214 * position indicated by fl.l_start.
1215 *
1216 * Will minor miracles never cease?
1217 */
1218
a0d0e21e 1219 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b 1220 return -1;
1221
1222 }
1223
1224 return 0;
1225}
a0d0e21e 1226#endif /* F_FREESP */
ff8e2863 1227
a687059c 1228bool
864dbfa3 1229Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
a687059c 1230{
1231 register char *tmps;
463ee0b2 1232 STRLEN len;
a687059c 1233
79072805 1234 /* assuming fp is checked earlier */
1235 if (!sv)
1236 return TRUE;
3280af22 1237 if (PL_ofmt) {
8990e307 1238 if (SvGMAGICAL(sv))
79072805 1239 mg_get(sv);
463ee0b2 1240 if (SvIOK(sv) && SvIVX(sv) != 0) {
65202027 1241 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
760ac839 1242 return !PerlIO_error(fp);
79072805 1243 }
463ee0b2 1244 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
79072805 1245 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
3280af22 1246 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
760ac839 1247 return !PerlIO_error(fp);
79072805 1248 }
a687059c 1249 }
79072805 1250 switch (SvTYPE(sv)) {
1251 case SVt_NULL:
411caa50 1252 if (ckWARN(WARN_UNINITIALIZED))
1253 report_uninit();
ff8e2863 1254 return TRUE;
79072805 1255 case SVt_IV:
a0d0e21e 1256 if (SvIOK(sv)) {
1257 if (SvGMAGICAL(sv))
1258 mg_get(sv);
cf2093f6 1259 if (SvIsUV(sv))
57def98f 1260 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
cf2093f6 1261 else
57def98f 1262 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
760ac839 1263 return !PerlIO_error(fp);
a0d0e21e 1264 }
1265 /* FALL THROUGH */
79072805 1266 default:
7d59b7e4 1267 if (PerlIO_isutf8(fp)) {
f9a63242 1268 if (!SvUTF8(sv))
88632417 1269 sv_utf8_upgrade_flags(sv = sv_mortalcopy(sv),
1270 SV_GMAGIC|SV_UTF8_NO_ENCODING);
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}