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