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