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