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