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