Missing final \n.
[p5sagit/p5-mst-13.2.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
4c79ee7a 3 * Copyright (c) 1991-2003, 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;
4ecd490c 1324 STRLEN len;
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
4ecd490c 1335 s = SvPV(sv, len);
3280af22 1336 PL_statgv = Nullgv;
4ecd490c 1337 sv_setpvn(PL_statname, s, len);
1338 s = SvPVX(PL_statname); /* s now NUL-terminated */
3280af22 1339 PL_laststype = OP_STAT;
1340 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1341 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
9014280d 1342 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
3280af22 1343 return PL_laststatval;
a687059c 1344 }
1345}
1346
79072805 1347I32
cea2e8a9 1348Perl_my_lstat(pTHX)
c623bd54 1349{
39644a26 1350 dSP;
79072805 1351 SV *sv;
2d8e6c8d 1352 STRLEN n_a;
533c011a 1353 if (PL_op->op_flags & OPf_REF) {
924508f0 1354 EXTEND(SP,1);
638eceb6 1355 if (cGVOP_gv == PL_defgv) {
3280af22 1356 if (PL_laststype != OP_LSTAT)
cea2e8a9 1357 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1358 return PL_laststatval;
fe14fcc3 1359 }
5d3e98de 1360 if (ckWARN(WARN_IO)) {
9014280d 1361 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
5d3e98de 1362 GvENAME(cGVOP_gv));
1363 return (PL_laststatval = -1);
1364 }
fe14fcc3 1365 }
c623bd54 1366
3280af22 1367 PL_laststype = OP_LSTAT;
1368 PL_statgv = Nullgv;
79072805 1369 sv = POPs;
1370 PUTBACK;
5d3e98de 1371 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV && ckWARN(WARN_IO)) {
9014280d 1372 Perl_warner(aTHX_ packWARN(WARN_IO), "Use of -l on filehandle %s",
5d3e98de 1373 GvENAME((GV*) SvRV(sv)));
1374 return (PL_laststatval = -1);
1375 }
2d8e6c8d 1376 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1377 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1378 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
9014280d 1379 Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
3280af22 1380 return PL_laststatval;
c623bd54 1381}
1382
a687059c 1383bool
864dbfa3 1384Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1385{
d5a9bfb0 1386 return do_aexec5(really, mark, sp, 0, 0);
1387}
1388
1389bool
2aa1486d 1390Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1391 int fd, int do_report)
d5a9bfb0 1392{
cd39f2b6 1393#ifdef MACOS_TRADITIONAL
1394 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1395#else
a687059c 1396 register char **a;
9c5ffd7c 1397 char *tmps = Nullch;
2d8e6c8d 1398 STRLEN n_a;
a687059c 1399
79072805 1400 if (sp > mark) {
3280af22 1401 New(401,PL_Argv, sp - mark + 1, char*);
1402 a = PL_Argv;
79072805 1403 while (++mark <= sp) {
1404 if (*mark)
2d8e6c8d 1405 *a++ = SvPVx(*mark, n_a);
a687059c 1406 else
1407 *a++ = "";
1408 }
1409 *a = Nullch;
91b2752f 1410 if (really)
1411 tmps = SvPV(really, n_a);
1412 if ((!really && *PL_Argv[0] != '/') ||
1413 (really && *tmps != '/')) /* will execvp use PATH? */
79072805 1414 TAINT_ENV(); /* testing IFS here is overkill, probably */
91b2752f 1415 if (really && *tmps)
b4748376 1416 PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
a687059c 1417 else
b4748376 1418 PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
599cee73 1419 if (ckWARN(WARN_EXEC))
9014280d 1420 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
91b2752f 1421 (really ? tmps : PL_Argv[0]), Strerror(errno));
d5a9bfb0 1422 if (do_report) {
1423 int e = errno;
1424
1425 PerlLIO_write(fd, (void*)&e, sizeof(int));
1426 PerlLIO_close(fd);
1427 }
a687059c 1428 }
bee1dbe2 1429 do_execfree();
cd39f2b6 1430#endif
a687059c 1431 return FALSE;
1432}
1433
fe14fcc3 1434void
864dbfa3 1435Perl_do_execfree(pTHX)
ff8e2863 1436{
3280af22 1437 if (PL_Argv) {
1438 Safefree(PL_Argv);
1439 PL_Argv = Null(char **);
ff8e2863 1440 }
3280af22 1441 if (PL_Cmd) {
1442 Safefree(PL_Cmd);
1443 PL_Cmd = Nullch;
ff8e2863 1444 }
1445}
1446
cd39f2b6 1447#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1448
a687059c 1449bool
864dbfa3 1450Perl_do_exec(pTHX_ char *cmd)
a687059c 1451{
e446cec8 1452 return do_exec3(cmd,0,0);
1453}
1454
1455bool
864dbfa3 1456Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1457{
a687059c 1458 register char **a;
1459 register char *s;
a687059c 1460
748a9306 1461 while (*cmd && isSPACE(*cmd))
1462 cmd++;
1463
a687059c 1464 /* save an extra exec if possible */
1465
bf38876a 1466#ifdef CSH
d05c1ba0 1467 {
1468 char flags[10];
1469 if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1470 strnEQ(cmd+PL_cshlen," -c",3)) {
1471 strcpy(flags,"-c");
1472 s = cmd+PL_cshlen+3;
1473 if (*s == 'f') {
1474 s++;
1475 strcat(flags,"f");
1476 }
1477 if (*s == ' ')
1478 s++;
1479 if (*s++ == '\'') {
1480 char *ncmd = s;
1481
1482 while (*s)
1483 s++;
1484 if (s[-1] == '\n')
1485 *--s = '\0';
1486 if (s[-1] == '\'') {
1487 *--s = '\0';
1488 PerlProc_execl(PL_cshname,"csh", flags, ncmd, (char*)0);
1489 *s = '\'';
1490 return FALSE;
1491 }
1492 }
a687059c 1493 }
1494 }
bf38876a 1495#endif /* CSH */
a687059c 1496
1497 /* see if there are shell metacharacters in it */
1498
748a9306 1499 if (*cmd == '.' && isSPACE(cmd[1]))
1500 goto doshell;
1501
1502 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1503 goto doshell;
1504
c170e444 1505 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1506 if (*s == '=')
1507 goto doshell;
748a9306 1508
a687059c 1509 for (s = cmd; *s; s++) {
d05c1ba0 1510 if (*s != ' ' && !isALPHA(*s) &&
1511 strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1512 if (*s == '\n' && !s[1]) {
1513 *s = '\0';
1514 break;
1515 }
603a98b0 1516 /* handle the 2>&1 construct at the end */
1517 if (*s == '>' && s[1] == '&' && s[2] == '1'
1518 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1519 && (!s[3] || isSPACE(s[3])))
1520 {
1521 char *t = s + 3;
1522
1523 while (*t && isSPACE(*t))
1524 ++t;
1525 if (!*t && (dup2(1,2) != -1)) {
1526 s[-2] = '\0';
1527 break;
1528 }
1529 }
a687059c 1530 doshell:
3280af22 1531 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1532 return FALSE;
1533 }
1534 }
748a9306 1535
3280af22 1536 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1537 PL_Cmd = savepvn(cmd, s-cmd);
1538 a = PL_Argv;
1539 for (s = PL_Cmd; *s;) {
99b89507 1540 while (*s && isSPACE(*s)) s++;
a687059c 1541 if (*s)
1542 *(a++) = s;
99b89507 1543 while (*s && !isSPACE(*s)) s++;
a687059c 1544 if (*s)
1545 *s++ = '\0';
1546 }
1547 *a = Nullch;
3280af22 1548 if (PL_Argv[0]) {
1549 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1550 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1551 do_execfree();
a687059c 1552 goto doshell;
b1248f16 1553 }
d008e5eb 1554 {
e446cec8 1555 int e = errno;
1556
d008e5eb 1557 if (ckWARN(WARN_EXEC))
9014280d 1558 Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
d008e5eb 1559 PL_Argv[0], Strerror(errno));
e446cec8 1560 if (do_report) {
1561 PerlLIO_write(fd, (void*)&e, sizeof(int));
1562 PerlLIO_close(fd);
1563 }
d008e5eb 1564 }
a687059c 1565 }
ff8e2863 1566 do_execfree();
a687059c 1567 return FALSE;
1568}
1569
6890e559 1570#endif /* OS2 || WIN32 */
760ac839 1571
79072805 1572I32
864dbfa3 1573Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1574{
79072805 1575 register I32 val;
1576 register I32 val2;
1577 register I32 tot = 0;
20408e3c 1578 char *what;
a687059c 1579 char *s;
79072805 1580 SV **oldmark = mark;
2d8e6c8d 1581 STRLEN n_a;
a687059c 1582
20408e3c 1583#define APPLY_TAINT_PROPER() \
3280af22 1584 STMT_START { \
17406bd6 1585 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1586 } STMT_END
20408e3c 1587
1588 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1589 if (PL_tainting) {
463ee0b2 1590 while (++mark <= sp) {
bbce6d69 1591 if (SvTAINTED(*mark)) {
1592 TAINT;
1593 break;
1594 }
463ee0b2 1595 }
1596 mark = oldmark;
1597 }
a687059c 1598 switch (type) {
79072805 1599 case OP_CHMOD:
20408e3c 1600 what = "chmod";
1601 APPLY_TAINT_PROPER();
79072805 1602 if (++mark <= sp) {
463ee0b2 1603 val = SvIVx(*mark);
20408e3c 1604 APPLY_TAINT_PROPER();
1605 tot = sp - mark;
79072805 1606 while (++mark <= sp) {
2d8e6c8d 1607 char *name = SvPVx(*mark, n_a);
20408e3c 1608 APPLY_TAINT_PROPER();
1609 if (PerlLIO_chmod(name, val))
a687059c 1610 tot--;
1611 }
1612 }
1613 break;
fe14fcc3 1614#ifdef HAS_CHOWN
79072805 1615 case OP_CHOWN:
20408e3c 1616 what = "chown";
1617 APPLY_TAINT_PROPER();
79072805 1618 if (sp - mark > 2) {
463ee0b2 1619 val = SvIVx(*++mark);
1620 val2 = SvIVx(*++mark);
20408e3c 1621 APPLY_TAINT_PROPER();
a0d0e21e 1622 tot = sp - mark;
79072805 1623 while (++mark <= sp) {
2d8e6c8d 1624 char *name = SvPVx(*mark, n_a);
20408e3c 1625 APPLY_TAINT_PROPER();
36660982 1626 if (PerlLIO_chown(name, val, val2))
a687059c 1627 tot--;
1628 }
1629 }
1630 break;
b1248f16 1631#endif
a1d180c4 1632/*
dd64f1c3 1633XXX Should we make lchown() directly available from perl?
1634For now, we'll let Configure test for HAS_LCHOWN, but do
1635nothing in the core.
1636 --AD 5/1998
1637*/
fe14fcc3 1638#ifdef HAS_KILL
79072805 1639 case OP_KILL:
20408e3c 1640 what = "kill";
1641 APPLY_TAINT_PROPER();
55497cff 1642 if (mark == sp)
1643 break;
2d8e6c8d 1644 s = SvPVx(*++mark, n_a);
e02bfb16 1645 if (isALPHA(*s)) {
79072805 1646 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1647 s += 3;
e02bfb16 1648 if ((val = whichsig(s)) < 0)
cea2e8a9 1649 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805 1650 }
1651 else
463ee0b2 1652 val = SvIVx(*mark);
20408e3c 1653 APPLY_TAINT_PROPER();
1654 tot = sp - mark;
3595fcef 1655#ifdef VMS
1656 /* kill() doesn't do process groups (job trees?) under VMS */
1657 if (val < 0) val = -val;
1658 if (val == SIGKILL) {
1659# include <starlet.h>
1660 /* Use native sys$delprc() to insure that target process is
1661 * deleted; supervisor-mode images don't pay attention to
1662 * CRTL's emulation of Unix-style signals and kill()
1663 */
1664 while (++mark <= sp) {
1665 I32 proc = SvIVx(*mark);
1666 register unsigned long int __vmssts;
20408e3c 1667 APPLY_TAINT_PROPER();
3595fcef 1668 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1669 tot--;
1670 switch (__vmssts) {
1671 case SS$_NONEXPR:
1672 case SS$_NOSUCHNODE:
1673 SETERRNO(ESRCH,__vmssts);
1674 break;
1675 case SS$_NOPRIV:
1676 SETERRNO(EPERM,__vmssts);
1677 break;
1678 default:
1679 SETERRNO(EVMSERR,__vmssts);
1680 }
1681 }
1682 }
1683 break;
1684 }
1685#endif
79072805 1686 if (val < 0) {
1687 val = -val;
1688 while (++mark <= sp) {
463ee0b2 1689 I32 proc = SvIVx(*mark);
20408e3c 1690 APPLY_TAINT_PROPER();
fe14fcc3 1691#ifdef HAS_KILLPG
3028581b 1692 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1693#else
3028581b 1694 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1695#endif
79072805 1696 tot--;
a687059c 1697 }
79072805 1698 }
1699 else {
1700 while (++mark <= sp) {
20408e3c 1701 I32 proc = SvIVx(*mark);
1702 APPLY_TAINT_PROPER();
1703 if (PerlProc_kill(proc, val))
79072805 1704 tot--;
a687059c 1705 }
1706 }
1707 break;
b1248f16 1708#endif
79072805 1709 case OP_UNLINK:
20408e3c 1710 what = "unlink";
1711 APPLY_TAINT_PROPER();
79072805 1712 tot = sp - mark;
1713 while (++mark <= sp) {
2d8e6c8d 1714 s = SvPVx(*mark, n_a);
20408e3c 1715 APPLY_TAINT_PROPER();
3280af22 1716 if (PL_euid || PL_unsafe) {
a687059c 1717 if (UNLINK(s))
1718 tot--;
1719 }
1720 else { /* don't let root wipe out directories without -U */
3280af22 1721 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1722 tot--;
1723 else {
1724 if (UNLINK(s))
1725 tot--;
1726 }
1727 }
1728 }
1729 break;
a0d0e21e 1730#ifdef HAS_UTIME
79072805 1731 case OP_UTIME:
20408e3c 1732 what = "utime";
1733 APPLY_TAINT_PROPER();
79072805 1734 if (sp - mark > 2) {
748a9306 1735#if defined(I_UTIME) || defined(VMS)
663a0e37 1736 struct utimbuf utbuf;
1737#else
a687059c 1738 struct {
dd2821f6 1739 Time_t actime;
1740 Time_t modtime;
a687059c 1741 } utbuf;
663a0e37 1742#endif
a687059c 1743
c6f7b413 1744 SV* accessed = *++mark;
1745 SV* modified = *++mark;
1746 void * utbufp = &utbuf;
1747
6ec06612 1748 /* Be like C, and if both times are undefined, let the C
1749 * library figure out what to do. This usually means
1750 * "current time". */
c6f7b413 1751
1752 if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
6ec06612 1753 utbufp = NULL;
1754 else {
1755 Zero(&utbuf, sizeof utbuf, char);
517844ec 1756#ifdef BIG_TIME
6ec06612 1757 utbuf.actime = (Time_t)SvNVx(accessed); /* time accessed */
1758 utbuf.modtime = (Time_t)SvNVx(modified); /* time modified */
517844ec 1759#else
6ec06612 1760 utbuf.actime = (Time_t)SvIVx(accessed); /* time accessed */
1761 utbuf.modtime = (Time_t)SvIVx(modified); /* time modified */
517844ec 1762#endif
6ec06612 1763 }
1764 APPLY_TAINT_PROPER();
79072805 1765 tot = sp - mark;
1766 while (++mark <= sp) {
2d8e6c8d 1767 char *name = SvPVx(*mark, n_a);
20408e3c 1768 APPLY_TAINT_PROPER();
c6f7b413 1769 if (PerlLIO_utime(name, utbufp))
a687059c 1770 tot--;
1771 }
a687059c 1772 }
1773 else
79072805 1774 tot = 0;
a687059c 1775 break;
a0d0e21e 1776#endif
a687059c 1777 }
1778 return tot;
20408e3c 1779
20408e3c 1780#undef APPLY_TAINT_PROPER
a687059c 1781}
1782
1783/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1784#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1785bool
1786Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1787/* Note: we use `effective' both for uids and gids.
1788 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1789{
bee1dbe2 1790#ifdef DOSISH
fe14fcc3 1791 /* [Comments and code from Len Reed]
1792 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1793 * to write-protected files. The execute permission bit is set
1794 * by the Miscrosoft C library stat() function for the following:
1795 * .exe files
1796 * .com files
1797 * .bat files
1798 * directories
1799 * All files and directories are readable.
1800 * Directories and special files, e.g. "CON", cannot be
1801 * write-protected.
1802 * [Comment by Tom Dinger -- a directory can have the write-protect
1803 * bit set in the file system, but DOS permits changes to
1804 * the directory anyway. In addition, all bets are off
1805 * here for networked software, such as Novell and
1806 * Sun's PC-NFS.]
1807 */
1808
bee1dbe2 1809 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1810 * too so it will actually look into the files for magic numbers
1811 */
7f4774ae 1812 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1813
55497cff 1814#else /* ! DOSISH */
3280af22 1815 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1816 if (mode == S_IXUSR) {
c623bd54 1817 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1818 return TRUE;
1819 }
1820 else
1821 return TRUE; /* root reads and writes anything */
1822 return FALSE;
1823 }
3280af22 1824 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1825 if (statbufp->st_mode & mode)
a687059c 1826 return TRUE; /* ok as "user" */
1827 }
d8eceb89 1828 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1829 if (statbufp->st_mode & mode >> 3)
a687059c 1830 return TRUE; /* ok as "group" */
1831 }
7f4774ae 1832 else if (statbufp->st_mode & mode >> 6)
a687059c 1833 return TRUE; /* ok as "other" */
1834 return FALSE;
55497cff 1835#endif /* ! DOSISH */
a687059c 1836}
a0d0e21e 1837#endif /* ! VMS */
a687059c 1838
d8eceb89 1839bool
1840Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1841{
cd39f2b6 1842#ifdef MACOS_TRADITIONAL
1843 /* This is simply not correct for AppleShare, but fix it yerself. */
1844 return TRUE;
1845#else
3280af22 1846 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1847 return TRUE;
fe14fcc3 1848#ifdef HAS_GETGROUPS
a687059c 1849#ifndef NGROUPS
1850#define NGROUPS 32
1851#endif
1852 {
a0d0e21e 1853 Groups_t gary[NGROUPS];
79072805 1854 I32 anum;
a687059c 1855
1856 anum = getgroups(NGROUPS,gary);
1857 while (--anum >= 0)
1858 if (gary[anum] == testgid)
1859 return TRUE;
1860 }
1861#endif
1862 return FALSE;
cd39f2b6 1863#endif
a687059c 1864}
c2ab57d4 1865
fe14fcc3 1866#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1867
79072805 1868I32
864dbfa3 1869Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1870{
c2ab57d4 1871 key_t key;
79072805 1872 I32 n, flags;
c2ab57d4 1873
463ee0b2 1874 key = (key_t)SvNVx(*++mark);
1875 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1876 flags = SvIVx(*++mark);
748a9306 1877 SETERRNO(0,0);
c2ab57d4 1878 switch (optype)
1879 {
fe14fcc3 1880#ifdef HAS_MSG
79072805 1881 case OP_MSGGET:
c2ab57d4 1882 return msgget(key, flags);
e5d73d77 1883#endif
fe14fcc3 1884#ifdef HAS_SEM
79072805 1885 case OP_SEMGET:
c2ab57d4 1886 return semget(key, n, flags);
e5d73d77 1887#endif
fe14fcc3 1888#ifdef HAS_SHM
79072805 1889 case OP_SHMGET:
c2ab57d4 1890 return shmget(key, n, flags);
e5d73d77 1891#endif
fe14fcc3 1892#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1893 default:
cea2e8a9 1894 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1895#endif
c2ab57d4 1896 }
1897 return -1; /* should never happen */
1898}
1899
79072805 1900I32
864dbfa3 1901Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1902{
79072805 1903 SV *astr;
c2ab57d4 1904 char *a;
a0d0e21e 1905 I32 id, n, cmd, infosize, getinfo;
1906 I32 ret = -1;
c2ab57d4 1907
463ee0b2 1908 id = SvIVx(*++mark);
1909 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1910 cmd = SvIVx(*++mark);
79072805 1911 astr = *++mark;
c2ab57d4 1912 infosize = 0;
1913 getinfo = (cmd == IPC_STAT);
1914
1915 switch (optype)
1916 {
fe14fcc3 1917#ifdef HAS_MSG
79072805 1918 case OP_MSGCTL:
c2ab57d4 1919 if (cmd == IPC_STAT || cmd == IPC_SET)
1920 infosize = sizeof(struct msqid_ds);
1921 break;
e5d73d77 1922#endif
fe14fcc3 1923#ifdef HAS_SHM
79072805 1924 case OP_SHMCTL:
c2ab57d4 1925 if (cmd == IPC_STAT || cmd == IPC_SET)
1926 infosize = sizeof(struct shmid_ds);
1927 break;
e5d73d77 1928#endif
fe14fcc3 1929#ifdef HAS_SEM
79072805 1930 case OP_SEMCTL:
39398f3f 1931#ifdef Semctl
c2ab57d4 1932 if (cmd == IPC_STAT || cmd == IPC_SET)
1933 infosize = sizeof(struct semid_ds);
1934 else if (cmd == GETALL || cmd == SETALL)
1935 {
8e591e46 1936 struct semid_ds semds;
bd89102f 1937 union semun semun;
e6f0bdd6 1938#ifdef EXTRA_F_IN_SEMUN_BUF
1939 semun.buff = &semds;
1940#else
84902520 1941 semun.buf = &semds;
e6f0bdd6 1942#endif
c2ab57d4 1943 getinfo = (cmd == GETALL);
9b89d93d 1944 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1945 return -1;
6e21c824 1946 infosize = semds.sem_nsems * sizeof(short);
1947 /* "short" is technically wrong but much more portable
1948 than guessing about u_?short(_t)? */
c2ab57d4 1949 }
39398f3f 1950#else
cea2e8a9 1951 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1952#endif
c2ab57d4 1953 break;
e5d73d77 1954#endif
fe14fcc3 1955#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1956 default:
cea2e8a9 1957 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1958#endif
c2ab57d4 1959 }
1960
1961 if (infosize)
1962 {
a0d0e21e 1963 STRLEN len;
c2ab57d4 1964 if (getinfo)
1965 {
a0d0e21e 1966 SvPV_force(astr, len);
1967 a = SvGROW(astr, infosize+1);
c2ab57d4 1968 }
1969 else
1970 {
463ee0b2 1971 a = SvPV(astr, len);
1972 if (len != infosize)
cea2e8a9 1973 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1974 PL_op_desc[optype],
1975 (unsigned long)len,
1976 (long)infosize);
c2ab57d4 1977 }
1978 }
1979 else
1980 {
c030ccd9 1981 IV i = SvIV(astr);
56431972 1982 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 1983 }
748a9306 1984 SETERRNO(0,0);
c2ab57d4 1985 switch (optype)
1986 {
fe14fcc3 1987#ifdef HAS_MSG
79072805 1988 case OP_MSGCTL:
bee1dbe2 1989 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1990 break;
e5d73d77 1991#endif
fe14fcc3 1992#ifdef HAS_SEM
bd89102f 1993 case OP_SEMCTL: {
39398f3f 1994#ifdef Semctl
bd89102f 1995 union semun unsemds;
1996
e6f0bdd6 1997#ifdef EXTRA_F_IN_SEMUN_BUF
1998 unsemds.buff = (struct semid_ds *)a;
1999#else
bd89102f 2000 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 2001#endif
bd89102f 2002 ret = Semctl(id, n, cmd, unsemds);
39398f3f 2003#else
cea2e8a9 2004 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 2005#endif
bd89102f 2006 }
c2ab57d4 2007 break;
e5d73d77 2008#endif
fe14fcc3 2009#ifdef HAS_SHM
79072805 2010 case OP_SHMCTL:
bee1dbe2 2011 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 2012 break;
e5d73d77 2013#endif
c2ab57d4 2014 }
2015 if (getinfo && ret >= 0) {
79072805 2016 SvCUR_set(astr, infosize);
2017 *SvEND(astr) = '\0';
a0d0e21e 2018 SvSETMAGIC(astr);
c2ab57d4 2019 }
2020 return ret;
2021}
2022
79072805 2023I32
864dbfa3 2024Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 2025{
fe14fcc3 2026#ifdef HAS_MSG
79072805 2027 SV *mstr;
c2ab57d4 2028 char *mbuf;
79072805 2029 I32 id, msize, flags;
463ee0b2 2030 STRLEN len;
c2ab57d4 2031
463ee0b2 2032 id = SvIVx(*++mark);
79072805 2033 mstr = *++mark;
463ee0b2 2034 flags = SvIVx(*++mark);
2035 mbuf = SvPV(mstr, len);
2036 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 2037 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 2038 SETERRNO(0,0);
bee1dbe2 2039 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 2040#else
cea2e8a9 2041 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 2042#endif
c2ab57d4 2043}
2044
79072805 2045I32
864dbfa3 2046Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 2047{
fe14fcc3 2048#ifdef HAS_MSG
79072805 2049 SV *mstr;
c2ab57d4 2050 char *mbuf;
2051 long mtype;
79072805 2052 I32 id, msize, flags, ret;
463ee0b2 2053 STRLEN len;
79072805 2054
463ee0b2 2055 id = SvIVx(*++mark);
79072805 2056 mstr = *++mark;
c2e66d9e 2057 /* suppress warning when reading into undef var --jhi */
2058 if (! SvOK(mstr))
2059 sv_setpvn(mstr, "", 0);
463ee0b2 2060 msize = SvIVx(*++mark);
2061 mtype = (long)SvIVx(*++mark);
2062 flags = SvIVx(*++mark);
a0d0e21e 2063 SvPV_force(mstr, len);
2064 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
a1d180c4 2065
748a9306 2066 SETERRNO(0,0);
bee1dbe2 2067 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 2068 if (ret >= 0) {
79072805 2069 SvCUR_set(mstr, sizeof(long)+ret);
2070 *SvEND(mstr) = '\0';
41d6edb2 2071#ifndef INCOMPLETE_TAINTS
2072 /* who knows who has been playing with this message? */
2073 SvTAINTED_on(mstr);
2074#endif
c2ab57d4 2075 }
2076 return ret;
e5d73d77 2077#else
cea2e8a9 2078 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 2079#endif
c2ab57d4 2080}
2081
79072805 2082I32
864dbfa3 2083Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 2084{
fe14fcc3 2085#ifdef HAS_SEM
79072805 2086 SV *opstr;
c2ab57d4 2087 char *opbuf;
463ee0b2 2088 I32 id;
2089 STRLEN opsize;
c2ab57d4 2090
463ee0b2 2091 id = SvIVx(*++mark);
79072805 2092 opstr = *++mark;
463ee0b2 2093 opbuf = SvPV(opstr, opsize);
248ff010 2094 if (opsize < 3 * SHORTSIZE
2095 || (opsize % (3 * SHORTSIZE))) {
93189314 2096 SETERRNO(EINVAL,LIB_INVARG);
c2ab57d4 2097 return -1;
2098 }
748a9306 2099 SETERRNO(0,0);
248ff010 2100 /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2101 {
2102 int nsops = opsize / (3 * sizeof (short));
2103 int i = nsops;
2104 short *ops = (short *) opbuf;
2105 short *o = ops;
2106 struct sembuf *temps, *t;
2107 I32 result;
2108
2109 New (0, temps, nsops, struct sembuf);
2110 t = temps;
2111 while (i--) {
2112 t->sem_num = *o++;
2113 t->sem_op = *o++;
2114 t->sem_flg = *o++;
2115 t++;
2116 }
2117 result = semop(id, temps, nsops);
2118 t = temps;
2119 o = ops;
2120 i = nsops;
2121 while (i--) {
2122 *o++ = t->sem_num;
2123 *o++ = t->sem_op;
2124 *o++ = t->sem_flg;
2125 t++;
2126 }
2127 Safefree(temps);
2128 return result;
2129 }
e5d73d77 2130#else
cea2e8a9 2131 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 2132#endif
c2ab57d4 2133}
2134
79072805 2135I32
864dbfa3 2136Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 2137{
fe14fcc3 2138#ifdef HAS_SHM
79072805 2139 SV *mstr;
c2ab57d4 2140 char *mbuf, *shm;
79072805 2141 I32 id, mpos, msize;
463ee0b2 2142 STRLEN len;
c2ab57d4 2143 struct shmid_ds shmds;
c2ab57d4 2144
463ee0b2 2145 id = SvIVx(*++mark);
79072805 2146 mstr = *++mark;
463ee0b2 2147 mpos = SvIVx(*++mark);
2148 msize = SvIVx(*++mark);
748a9306 2149 SETERRNO(0,0);
c2ab57d4 2150 if (shmctl(id, IPC_STAT, &shmds) == -1)
2151 return -1;
2152 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
93189314 2153 SETERRNO(EFAULT,SS_ACCVIO); /* can't do as caller requested */
c2ab57d4 2154 return -1;
2155 }
8ac85365 2156 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 2157 if (shm == (char *)-1) /* I hate System V IPC, I really do */
2158 return -1;
79072805 2159 if (optype == OP_SHMREAD) {
9f538c04 2160 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2161 if (! SvOK(mstr))
2162 sv_setpvn(mstr, "", 0);
a0d0e21e 2163 SvPV_force(mstr, len);
2164 mbuf = SvGROW(mstr, msize+1);
2165
bee1dbe2 2166 Copy(shm + mpos, mbuf, msize, char);
79072805 2167 SvCUR_set(mstr, msize);
2168 *SvEND(mstr) = '\0';
a0d0e21e 2169 SvSETMAGIC(mstr);
d929ce6f 2170#ifndef INCOMPLETE_TAINTS
2171 /* who knows who has been playing with this shared memory? */
2172 SvTAINTED_on(mstr);
2173#endif
c2ab57d4 2174 }
2175 else {
79072805 2176 I32 n;
c2ab57d4 2177
a0d0e21e 2178 mbuf = SvPV(mstr, len);
463ee0b2 2179 if ((n = len) > msize)
c2ab57d4 2180 n = msize;
bee1dbe2 2181 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2182 if (n < msize)
bee1dbe2 2183 memzero(shm + mpos + n, msize - n);
c2ab57d4 2184 }
2185 return shmdt(shm);
e5d73d77 2186#else
cea2e8a9 2187 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2188#endif
c2ab57d4 2189}
2190
fe14fcc3 2191#endif /* SYSV IPC */
4e35701f 2192
0d44d22b 2193/*
ccfc67b7 2194=head1 IO Functions
2195
0d44d22b 2196=for apidoc start_glob
2197
2198Function called by C<do_readline> to spawn a glob (or do the glob inside
2199perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
210b36aa 2200this glob starter is only used by miniperl during the build process.
0d44d22b 2201Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
fab3f3a7 2202
0d44d22b 2203=cut
2204*/
2205
2206PerlIO *
2207Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2208{
2209 SV *tmpcmd = NEWSV(55, 0);
2210 PerlIO *fp;
2211 ENTER;
2212 SAVEFREESV(tmpcmd);
2213#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2214 /* since spawning off a process is a real performance hit */
2215 {
2216#include <descrip.h>
2217#include <lib$routines.h>
2218#include <nam.h>
2219#include <rmsdef.h>
2220 char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
2221 char vmsspec[NAM$C_MAXRSS+1];
2222 char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
0d44d22b 2223 $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
2224 PerlIO *tmpfp;
2225 STRLEN i;
2226 struct dsc$descriptor_s wilddsc
2227 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2228 struct dsc$descriptor_vs rsdsc
2229 = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
2230 unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
2231
2232 /* We could find out if there's an explicit dev/dir or version
2233 by peeking into lib$find_file's internal context at
2234 ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
2235 but that's unsupported, so I don't want to do it now and
2236 have it bite someone in the future. */
0d44d22b 2237 cp = SvPV(tmpglob,i);
2238 for (; i; i--) {
2239 if (cp[i] == ';') hasver = 1;
2240 if (cp[i] == '.') {
2241 if (sts) hasver = 1;
2242 else sts = 1;
2243 }
2244 if (cp[i] == '/') {
2245 hasdir = isunix = 1;
2246 break;
2247 }
2248 if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
2249 hasdir = 1;
2250 break;
2251 }
2252 }
a15cef0c 2253 if ((tmpfp = PerlIO_tmpfile()) != NULL) {
0d44d22b 2254 Stat_t st;
2255 if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
2256 ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
2257 else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
2258 if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
a3987cb8 2259 for (cp=wilddsc.dsc$a_pointer; ok && cp && *cp; cp++)
2260 if (*cp == '?') *cp = '%'; /* VMS style single-char wildcard */
0d44d22b 2261 while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
2262 &dfltdsc,NULL,NULL,NULL))&1)) {
2263 end = rstr + (unsigned long int) *rslt;
2264 if (!hasver) while (*end != ';') end--;
2265 *(end++) = '\n'; *end = '\0';
2266 for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
2267 if (hasdir) {
2268 if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
2269 begin = rstr;
2270 }
2271 else {
2272 begin = end;
2273 while (*(--begin) != ']' && *begin != '>') ;
2274 ++begin;
2275 }
2276 ok = (PerlIO_puts(tmpfp,begin) != EOF);
2277 }
2278 if (cxt) (void)lib$find_file_end(&cxt);
2279 if (ok && sts != RMS$_NMF &&
93189314 2280 sts != RMS$_DNF && sts != RMS_FNF) ok = 0;
0d44d22b 2281 if (!ok) {
2282 if (!(sts & 1)) {
2283 SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
2284 }
2285 PerlIO_close(tmpfp);
2286 fp = NULL;
2287 }
2288 else {
2289 PerlIO_rewind(tmpfp);
2290 IoTYPE(io) = IoTYPE_RDONLY;
2291 IoIFP(io) = fp = tmpfp;
2292 IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
2293 }
2294 }
2295 }
2296#else /* !VMS */
2297#ifdef MACOS_TRADITIONAL
2298 sv_setpv(tmpcmd, "glob ");
2299 sv_catsv(tmpcmd, tmpglob);
2300 sv_catpv(tmpcmd, " |");
2301#else
2302#ifdef DOSISH
2303#ifdef OS2
2304 sv_setpv(tmpcmd, "for a in ");
2305 sv_catsv(tmpcmd, tmpglob);
2306 sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2307#else
2308#ifdef DJGPP
2309 sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2310 sv_catsv(tmpcmd, tmpglob);
2311#else
2312 sv_setpv(tmpcmd, "perlglob ");
2313 sv_catsv(tmpcmd, tmpglob);
2314 sv_catpv(tmpcmd, " |");
2315#endif /* !DJGPP */
2316#endif /* !OS2 */
2317#else /* !DOSISH */
2318#if defined(CSH)
2319 sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2320 sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2321 sv_catsv(tmpcmd, tmpglob);
2322 sv_catpv(tmpcmd, "' 2>/dev/null |");
2323#else
2324 sv_setpv(tmpcmd, "echo ");
2325 sv_catsv(tmpcmd, tmpglob);
2326#if 'z' - 'a' == 25
2327 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
2328#else
2329 sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2330#endif
2331#endif /* !CSH */
2332#endif /* !DOSISH */
2333#endif /* MACOS_TRADITIONAL */
2334 (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
2335 FALSE, O_RDONLY, 0, Nullfp);
2336 fp = IoIFP(io);
2337#endif /* !VMS */
2338 LEAVE;
2339 return fp;
2340}