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