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