This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid double close().
[perl5.git] / doio.c
CommitLineData
a0d0e21e 1/* doio.c
a687059c 2 *
3818b22b 3 * Copyright (c) 1991-2000, Larry Wall
a687059c 4 *
6e21c824
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
a687059c 7 *
a0d0e21e
LW
8 */
9
10/*
11 * "Far below them they saw the white waters pour into a foaming bowl, and
12 * then swirl darkly about a deep oval basin in the rocks, until they found
13 * their way out again through a narrow gate, and flowed away, fuming and
14 * chattering, into calmer and more level reaches."
a687059c
LW
15 */
16
17#include "EXTERN.h"
864dbfa3 18#define PERL_IN_DOIO_C
a687059c
LW
19#include "perl.h"
20
fe14fcc3 21#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
aec308ec 22#ifndef HAS_SEM
c2ab57d4 23#include <sys/ipc.h>
aec308ec 24#endif
fe14fcc3 25#ifdef HAS_MSG
c2ab57d4 26#include <sys/msg.h>
e5d73d77 27#endif
fe14fcc3 28#ifdef HAS_SHM
c2ab57d4 29#include <sys/shm.h>
a0d0e21e 30# ifndef HAS_SHMAT_PROTOTYPE
20ce7b12 31 extern Shmat_t shmat (int, char *, int);
a0d0e21e 32# endif
c2ab57d4 33#endif
e5d73d77 34#endif
c2ab57d4 35
663a0e37 36#ifdef I_UTIME
3730b96e 37# if defined(_MSC_VER) || defined(__MINGW32__)
3fe9a6f1
PP
38# include <sys/utime.h>
39# else
40# include <utime.h>
41# endif
663a0e37 42#endif
85aff577 43
85aff577
CS
44#ifdef O_EXCL
45# define OPEN_EXCL O_EXCL
46#else
47# define OPEN_EXCL 0
48#endif
a687059c 49
76121258
PP
50#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
51#include <signal.h>
52#endif
53
54/* XXX If this causes problems, set i_unistd=undef in the hint file. */
55#ifdef I_UNISTD
56# include <unistd.h>
57#endif
58
232e078e
AD
59#if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
60# include <sys/socket.h>
29209bc5 61# if defined(USE_SOCKS) && defined(I_SOCKS)
86959918
JH
62# include <socks.h>
63# endif
64# ifdef I_NETBSD
65# include <netdb.h>
66# endif
232e078e
AD
67# ifndef ENOTSOCK
68# ifdef I_NET_ERRNO
69# include <net/errno.h>
70# endif
71# endif
72#endif
73
a687059c 74bool
6170680b
IZ
75Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
76 int rawmode, int rawperm, PerlIO *supplied_fp)
77{
78 return do_open9(gv, name, len, as_raw, rawmode, rawperm,
79 supplied_fp, Nullsv, 0);
80}
81
82bool
83Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
84 int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
85 I32 num_svs)
a687059c 86{
a0d0e21e 87 register IO *io = GvIOn(gv);
760ac839
LW
88 PerlIO *saveifp = Nullfp;
89 PerlIO *saveofp = Nullfp;
6e21c824 90 char savetype = ' ';
c07a80fd 91 int writing = 0;
760ac839 92 PerlIO *fp;
c07a80fd
PP
93 int fd;
94 int result;
3500f679 95 bool was_fdopen = FALSE;
16fe6d59 96 bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
a687059c 97
3280af22 98 PL_forkprocess = 1; /* assume true if no fork */
c07a80fd 99
16fe6d59
GS
100 if (PL_op && PL_op->op_type == OP_OPEN) {
101 /* set up disciplines */
102 U8 flags = PL_op->op_private;
103 in_raw = (flags & OPpOPEN_IN_RAW);
104 in_crlf = (flags & OPpOPEN_IN_CRLF);
105 out_raw = (flags & OPpOPEN_OUT_RAW);
106 out_crlf = (flags & OPpOPEN_OUT_CRLF);
107 }
108
a0d0e21e 109 if (IoIFP(io)) {
760ac839 110 fd = PerlIO_fileno(IoIFP(io));
8990e307 111 if (IoTYPE(io) == '-')
c2ab57d4 112 result = 0;
3280af22 113 else if (fd <= PL_maxsysfd) {
8990e307
LW
114 saveifp = IoIFP(io);
115 saveofp = IoOFP(io);
116 savetype = IoTYPE(io);
6e21c824
LW
117 result = 0;
118 }
8990e307 119 else if (IoTYPE(io) == '|')
3028581b 120 result = PerlProc_pclose(IoIFP(io));
8990e307
LW
121 else if (IoIFP(io) != IoOFP(io)) {
122 if (IoOFP(io)) {
760ac839 123 result = PerlIO_close(IoOFP(io));
6170680b 124 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4
LW
125 }
126 else
760ac839 127 result = PerlIO_close(IoIFP(io));
a687059c 128 }
a687059c 129 else
760ac839 130 result = PerlIO_close(IoIFP(io));
3280af22 131 if (result == EOF && fd > PL_maxsysfd)
bf49b057 132 PerlIO_printf(Perl_error_log,
6170680b
IZ
133 "Warning: unable to close filehandle %s properly.\n",
134 GvENAME(gv));
8990e307 135 IoOFP(io) = IoIFP(io) = Nullfp;
a687059c 136 }
c07a80fd
PP
137
138 if (as_raw) {
09458382 139#if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
5ff3f7a4
GS
140 rawmode |= O_LARGEFILE;
141#endif
142
9d116dd7
JH
143#ifndef O_ACCMODE
144#define O_ACCMODE 3 /* Assume traditional implementation */
145#endif
5ff3f7a4 146
9d116dd7
JH
147 switch (result = rawmode & O_ACCMODE) {
148 case O_RDONLY:
149 IoTYPE(io) = '<';
150 break;
151 case O_WRONLY:
152 IoTYPE(io) = '>';
153 break;
154 case O_RDWR:
155 default:
156 IoTYPE(io) = '+';
157 break;
158 }
159
c07a80fd 160 writing = (result > 0);
3028581b 161 fd = PerlLIO_open3(name, rawmode, rawperm);
9d116dd7 162
c07a80fd
PP
163 if (fd == -1)
164 fp = NULL;
165 else {
16fe6d59
GS
166 char fpmode[4];
167 STRLEN ix = 0;
9d116dd7 168 if (result == O_RDONLY)
16fe6d59 169 fpmode[ix++] = 'r';
360e5741 170#ifdef O_APPEND
16fe6d59
GS
171 else if (rawmode & O_APPEND) {
172 fpmode[ix++] = 'a';
173 if (result != O_WRONLY)
174 fpmode[ix++] = '+';
175 }
360e5741 176#endif
16fe6d59
GS
177 else {
178 if (result == O_WRONLY)
179 fpmode[ix++] = 'w';
180 else {
181 fpmode[ix++] = 'r';
182 fpmode[ix++] = '+';
183 }
184 }
185 if (rawmode & O_BINARY)
186 fpmode[ix++] = 'b';
187 fpmode[ix] = '\0';
360e5741 188 fp = PerlIO_fdopen(fd, fpmode);
c07a80fd 189 if (!fp)
3028581b 190 PerlLIO_close(fd);
c07a80fd 191 }
a687059c 192 }
c07a80fd 193 else {
faecd977
GS
194 char *type;
195 char *oname = name;
6170680b 196 STRLEN tlen;
faecd977 197 STRLEN olen = len;
16fe6d59 198 char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
c07a80fd
PP
199 int dodup;
200
faecd977
GS
201 type = savepvn(name, len);
202 tlen = len;
203 SAVEFREEPV(type);
6170680b 204 if (num_svs) {
faecd977
GS
205 STRLEN l;
206 name = SvPV(svs, l) ;
207 len = (I32)l;
208 name = savepvn(name, len);
209 SAVEFREEPV(name);
6170680b 210 }
faecd977 211 else {
6170680b
IZ
212 while (tlen && isSPACE(type[tlen-1]))
213 type[--tlen] = '\0';
faecd977
GS
214 name = type;
215 len = tlen;
216 }
16fe6d59 217 mode[0] = mode[1] = mode[2] = mode[3] = '\0';
6170680b
IZ
218 IoTYPE(io) = *type;
219 if (*type == '+' && tlen > 1 && type[tlen-1] != '|') { /* scary */
220 mode[1] = *type++;
221 --tlen;
c07a80fd 222 writing = 1;
a687059c 223 }
c07a80fd 224
6170680b
IZ
225 if (*type == '|') {
226 if (num_svs && (tlen != 2 || type[1] != '-')) {
227 unknown_desr:
894356b3 228 Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
6170680b 229 }
c07a80fd 230 /*SUPPRESS 530*/
faecd977
GS
231 for (type++, tlen--; isSPACE(*type); type++, tlen--) ;
232 if (!num_svs) {
6170680b 233 name = type;
faecd977
GS
234 len = tlen;
235 }
06eaf0bc
GS
236 if (*name == '\0') { /* command is missing 19990114 */
237 dTHR;
238 if (ckWARN(WARN_PIPE))
cea2e8a9 239 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
06eaf0bc
GS
240 errno = EPIPE;
241 goto say_false;
242 }
6170680b 243 if (strNE(name,"-") || num_svs)
c07a80fd
PP
244 TAINT_ENV();
245 TAINT_PROPER("piped open");
faecd977 246 if (name[len-1] == '|') {
d008e5eb 247 dTHR;
faecd977 248 name[--len] = '\0' ;
599cee73 249 if (ckWARN(WARN_PIPE))
9a7dcd9c 250 Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe");
7b8d334a 251 }
16fe6d59
GS
252 {
253 char *mode;
254 if (out_raw)
255 mode = "wb";
256 else if (out_crlf)
257 mode = "wt";
258 else
259 mode = "w";
260 fp = PerlProc_popen(name,mode);
261 }
c07a80fd
PP
262 writing = 1;
263 }
6170680b 264 else if (*type == '>') {
c07a80fd 265 TAINT_PROPER("open");
6170680b
IZ
266 type++;
267 if (*type == '>') {
c07a80fd 268 mode[0] = IoTYPE(io) = 'a';
6170680b
IZ
269 type++;
270 tlen--;
a0d0e21e 271 }
c07a80fd
PP
272 else
273 mode[0] = 'w';
274 writing = 1;
275
16fe6d59
GS
276 if (out_raw)
277 strcat(mode, "b");
278 else if (out_crlf)
279 strcat(mode, "t");
280
6170680b
IZ
281 if (num_svs && tlen != 1)
282 goto unknown_desr;
283 if (*type == '&') {
284 name = type;
c07a80fd
PP
285 duplicity:
286 dodup = 1;
287 name++;
288 if (*name == '=') {
289 dodup = 0;
a0d0e21e 290 name++;
c07a80fd
PP
291 }
292 if (!*name && supplied_fp)
293 fp = supplied_fp;
a0d0e21e 294 else {
c07a80fd
PP
295 /*SUPPRESS 530*/
296 for (; isSPACE(*name); name++) ;
297 if (isDIGIT(*name))
298 fd = atoi(name);
299 else {
300 IO* thatio;
301 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
302 thatio = GvIO(gv);
303 if (!thatio) {
6e21c824 304#ifdef EINVAL
c07a80fd 305 SETERRNO(EINVAL,SS$_IVCHAN);
6e21c824 306#endif
c07a80fd
PP
307 goto say_false;
308 }
309 if (IoIFP(thatio)) {
54195c32 310 PerlIO *fp = IoIFP(thatio);
7211d486
JH
311 /* Flush stdio buffer before dup. --mjd
312 * Unfortunately SEEK_CURing 0 seems to
313 * be optimized away on most platforms;
314 * only Solaris and Linux seem to flush
315 * on that. --jhi */
54195c32 316 PerlIO_seek(fp, 0, SEEK_CUR);
7211d486
JH
317 /* On the other hand, do all platforms
318 * take gracefully to flushing a read-only
319 * filehandle? Perhaps we should do
320 * fsetpos(src)+fgetpos(dst)? --nik */
321 PerlIO_flush(fp);
54195c32 322 fd = PerlIO_fileno(fp);
c07a80fd
PP
323 if (IoTYPE(thatio) == 's')
324 IoTYPE(io) = 's';
325 }
326 else
327 fd = -1;
a0d0e21e 328 }
fec02dd3 329 if (dodup)
3028581b 330 fd = PerlLIO_dup(fd);
3500f679
RS
331 else
332 was_fdopen = TRUE;
760ac839 333 if (!(fp = PerlIO_fdopen(fd,mode))) {
c07a80fd 334 if (dodup)
3028581b 335 PerlLIO_close(fd);
faecd977 336 }
c07a80fd 337 }
bf38876a 338 }
c07a80fd
PP
339 else {
340 /*SUPPRESS 530*/
6170680b
IZ
341 for (; isSPACE(*type); type++) ;
342 if (strEQ(type,"-")) {
760ac839 343 fp = PerlIO_stdout();
c07a80fd
PP
344 IoTYPE(io) = '-';
345 }
346 else {
6170680b 347 fp = PerlIO_open((num_svs ? name : type), mode);
c07a80fd 348 }
bf38876a
LW
349 }
350 }
6170680b
IZ
351 else if (*type == '<') {
352 if (num_svs && tlen != 1)
353 goto unknown_desr;
c07a80fd 354 /*SUPPRESS 530*/
6170680b 355 for (type++; isSPACE(*type); type++) ;
bf38876a 356 mode[0] = 'r';
16fe6d59
GS
357 if (in_raw)
358 strcat(mode, "b");
359 else if (in_crlf)
360 strcat(mode, "t");
361
6170680b
IZ
362 if (*type == '&') {
363 name = type;
bf38876a 364 goto duplicity;
6170680b
IZ
365 }
366 if (strEQ(type,"-")) {
760ac839 367 fp = PerlIO_stdin();
8990e307 368 IoTYPE(io) = '-';
a687059c 369 }
bf38876a 370 else
6170680b 371 fp = PerlIO_open((num_svs ? name : type), mode);
a687059c 372 }
6170680b
IZ
373 else if (tlen > 1 && type[tlen-1] == '|') {
374 if (num_svs) {
375 if (tlen != 2 || type[0] != '-')
376 goto unknown_desr;
377 }
378 else {
379 type[--tlen] = '\0';
380 while (tlen && isSPACE(type[tlen-1]))
381 type[--tlen] = '\0';
382 /*SUPPRESS 530*/
383 for (; isSPACE(*type); type++) ;
384 name = type;
385 }
06eaf0bc
GS
386 if (*name == '\0') { /* command is missing 19990114 */
387 dTHR;
388 if (ckWARN(WARN_PIPE))
cea2e8a9 389 Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
06eaf0bc
GS
390 errno = EPIPE;
391 goto say_false;
392 }
6170680b 393 if (strNE(name,"-") || num_svs)
79072805
LW
394 TAINT_ENV();
395 TAINT_PROPER("piped open");
16fe6d59
GS
396 {
397 char *mode;
398 if (in_raw)
399 mode = "rb";
400 else if (in_crlf)
401 mode = "rt";
402 else
403 mode = "r";
404 fp = PerlProc_popen(name,mode);
405 }
8990e307 406 IoTYPE(io) = '|';
a687059c
LW
407 }
408 else {
6170680b
IZ
409 if (num_svs)
410 goto unknown_desr;
411 name = type;
8990e307 412 IoTYPE(io) = '<';
99b89507
LW
413 /*SUPPRESS 530*/
414 for (; isSPACE(*name); name++) ;
a687059c 415 if (strEQ(name,"-")) {
760ac839 416 fp = PerlIO_stdin();
8990e307 417 IoTYPE(io) = '-';
a687059c 418 }
16fe6d59
GS
419 else {
420 char *mode;
421 if (in_raw)
422 mode = "rb";
423 else if (in_crlf)
424 mode = "rt";
425 else
426 mode = "r";
427 fp = PerlIO_open(name,mode);
428 }
a687059c
LW
429 }
430 }
bee1dbe2 431 if (!fp) {
d008e5eb 432 dTHR;
599cee73 433 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
cea2e8a9 434 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
6e21c824 435 goto say_false;
bee1dbe2 436 }
8990e307
LW
437 if (IoTYPE(io) &&
438 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
96827780 439 dTHR;
3280af22 440 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
760ac839 441 (void)PerlIO_close(fp);
6e21c824 442 goto say_false;
a687059c 443 }
3280af22 444 if (S_ISSOCK(PL_statbuf.st_mode))
8990e307 445 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
99b89507
LW
446#ifdef HAS_SOCKET
447 else if (
c623bd54 448#ifdef S_IFMT
3280af22 449 !(PL_statbuf.st_mode & S_IFMT)
99b89507 450#else
b28d0864 451 !PL_statbuf.st_mode
99b89507
LW
452#endif
453 ) {
96827780
MB
454 char tmpbuf[256];
455 Sock_size_t buflen = sizeof tmpbuf;
3028581b 456 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
d574b85e
CS
457 &buflen) >= 0
458 || errno != ENOTSOCK)
8990e307 459 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
99b89507
LW
460 /* but some return 0 for streams too, sigh */
461 }
bf38876a 462#endif
a687059c 463 }
6e21c824 464 if (saveifp) { /* must use old fp? */
760ac839 465 fd = PerlIO_fileno(saveifp);
6e21c824 466 if (saveofp) {
760ac839 467 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
6e21c824 468 if (saveofp != saveifp) { /* was a socket? */
760ac839 469 PerlIO_close(saveofp);
99b89507
LW
470 if (fd > 2)
471 Safefree(saveofp);
6e21c824
LW
472 }
473 }
760ac839 474 if (fd != PerlIO_fileno(fp)) {
d8a83dd3 475 Pid_t pid;
79072805 476 SV *sv;
bee1dbe2 477
3028581b 478 PerlLIO_dup2(PerlIO_fileno(fp), fd);
cc9b6768 479 MUTEX_LOCK(&PL_fdpid_mutex);
3280af22 480 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
a0d0e21e 481 (void)SvUPGRADE(sv, SVt_IV);
463ee0b2
LW
482 pid = SvIVX(sv);
483 SvIVX(sv) = 0;
3280af22 484 sv = *av_fetch(PL_fdpid,fd,TRUE);
cc9b6768 485 MUTEX_UNLOCK(&PL_fdpid_mutex);
a0d0e21e 486 (void)SvUPGRADE(sv, SVt_IV);
463ee0b2 487 SvIVX(sv) = pid;
3500f679
RS
488 if (!was_fdopen)
489 PerlIO_close(fp);
bee1dbe2 490
6e21c824
LW
491 }
492 fp = saveifp;
760ac839 493 PerlIO_clearerr(fp);
6e21c824 494 }
a0d0e21e 495#if defined(HAS_FCNTL) && defined(F_SETFD)
a8710ca1
GS
496 {
497 int save_errno = errno;
498 fd = PerlIO_fileno(fp);
499 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
500 errno = save_errno;
501 }
1462b684 502#endif
8990e307 503 IoIFP(io) = fp;
684bef36 504 IoFLAGS(io) &= ~IOf_NOLINE;
bf38876a 505 if (writing) {
96827780 506 dTHR;
38432390
DM
507 bool is_reg = IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode);
508 if (IoTYPE(io) == 's' || is_reg)
16fe6d59
GS
509 {
510 char *mode;
511 if (out_raw)
512 mode = "wb";
513 else if (out_crlf)
514 mode = "wt";
515 else
516 mode = "w";
517
518 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),mode))) {
760ac839 519 PerlIO_close(fp);
8990e307 520 IoIFP(io) = Nullfp;
6e21c824 521 goto say_false;
fe14fcc3 522 }
38432390
DM
523 else {
524 if (is_reg)
525 IoIFP(io) = IoOFP(io); /* avoid double close() */
526 }
1462b684
LW
527 }
528 else
8990e307 529 IoOFP(io) = fp;
bf38876a 530 }
a687059c 531 return TRUE;
6e21c824
LW
532
533say_false:
8990e307
LW
534 IoIFP(io) = saveifp;
535 IoOFP(io) = saveofp;
536 IoTYPE(io) = savetype;
6e21c824 537 return FALSE;
a687059c
LW
538}
539
760ac839 540PerlIO *
864dbfa3 541Perl_nextargv(pTHX_ register GV *gv)
a687059c 542{
79072805 543 register SV *sv;
99b89507 544#ifndef FLEXFILENAMES
c623bd54
LW
545 int filedev;
546 int fileino;
99b89507 547#endif
761237fe
JB
548 Uid_t fileuid;
549 Gid_t filegid;
18708f5a 550 IO *io = GvIOp(gv);
fe14fcc3 551
3280af22
NIS
552 if (!PL_argvoutgv)
553 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
18708f5a
GS
554 if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
555 IoFLAGS(io) &= ~IOf_START;
7a1c5554
GS
556 if (PL_inplace) {
557 if (!PL_argvout_stack)
558 PL_argvout_stack = newAV();
18708f5a 559 av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv));
7a1c5554 560 }
18708f5a 561 }
3280af22
NIS
562 if (PL_filemode & (S_ISUID|S_ISGID)) {
563 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
fe14fcc3 564#ifdef HAS_FCHMOD
3280af22 565 (void)fchmod(PL_lastfd,PL_filemode);
fe14fcc3 566#else
b28d0864 567 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
fe14fcc3
LW
568#endif
569 }
3280af22 570 PL_filemode = 0;
79072805 571 while (av_len(GvAV(gv)) >= 0) {
11343788 572 dTHR;
85aff577 573 STRLEN oldlen;
79072805 574 sv = av_shift(GvAV(gv));
8990e307 575 SAVEFREESV(sv);
79072805
LW
576 sv_setsv(GvSV(gv),sv);
577 SvSETMAGIC(GvSV(gv));
3280af22 578 PL_oldname = SvPVx(GvSV(gv), oldlen);
9d116dd7 579 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
3280af22 580 if (PL_inplace) {
79072805 581 TAINT_PROPER("inplace open");
3280af22 582 if (oldlen == 1 && *PL_oldname == '-') {
4633a7c4 583 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a0d0e21e 584 return IoIFP(GvIOp(gv));
c623bd54 585 }
99b89507 586#ifndef FLEXFILENAMES
b28d0864
NIS
587 filedev = PL_statbuf.st_dev;
588 fileino = PL_statbuf.st_ino;
99b89507 589#endif
3280af22
NIS
590 PL_filemode = PL_statbuf.st_mode;
591 fileuid = PL_statbuf.st_uid;
592 filegid = PL_statbuf.st_gid;
593 if (!S_ISREG(PL_filemode)) {
0453d815
PM
594 if (ckWARN_d(WARN_INPLACE))
595 Perl_warner(aTHX_ WARN_INPLACE,
596 "Can't do inplace edit: %s is not a regular file",
597 PL_oldname );
79072805 598 do_close(gv,FALSE);
c623bd54
LW
599 continue;
600 }
3280af22
NIS
601 if (*PL_inplace) {
602 char *star = strchr(PL_inplace, '*');
2d259d92 603 if (star) {
3280af22 604 char *begin = PL_inplace;
2d259d92
CK
605 sv_setpvn(sv, "", 0);
606 do {
607 sv_catpvn(sv, begin, star - begin);
3280af22 608 sv_catpvn(sv, PL_oldname, oldlen);
2d259d92
CK
609 begin = ++star;
610 } while ((star = strchr(begin, '*')));
3d66d7bb
GS
611 if (*begin)
612 sv_catpv(sv,begin);
2d259d92
CK
613 }
614 else {
3280af22 615 sv_catpv(sv,PL_inplace);
2d259d92 616 }
c623bd54 617#ifndef FLEXFILENAMES
b28d0864
NIS
618 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
619 && PL_statbuf.st_dev == filedev
620 && PL_statbuf.st_ino == fileino
39e571d4
ML
621#ifdef DJGPP
622 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
623#endif
f248d071
GS
624 )
625 {
626 if (ckWARN_d(WARN_INPLACE))
627 Perl_warner(aTHX_ WARN_INPLACE,
628 "Can't do inplace edit: %s would not be unique",
629 SvPVX(sv));
79072805 630 do_close(gv,FALSE);
c623bd54
LW
631 continue;
632 }
633#endif
fe14fcc3 634#ifdef HAS_RENAME
d308986b 635#if !defined(DOSISH) && !defined(__CYGWIN__)
3280af22 636 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
0453d815
PM
637 if (ckWARN_d(WARN_INPLACE))
638 Perl_warner(aTHX_ WARN_INPLACE,
639 "Can't rename %s to %s: %s, skipping file",
640 PL_oldname, SvPVX(sv), Strerror(errno) );
79072805 641 do_close(gv,FALSE);
c623bd54
LW
642 continue;
643 }
a687059c 644#else
79072805 645 do_close(gv,FALSE);
3028581b 646 (void)PerlLIO_unlink(SvPVX(sv));
b28d0864 647 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
9d116dd7 648 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
55497cff 649#endif /* DOSISH */
ff8e2863 650#else
463ee0b2 651 (void)UNLINK(SvPVX(sv));
b28d0864 652 if (link(PL_oldname,SvPVX(sv)) < 0) {
0453d815
PM
653 if (ckWARN_d(WARN_INPLACE))
654 Perl_warner(aTHX_ WARN_INPLACE,
655 "Can't rename %s to %s: %s, skipping file",
656 PL_oldname, SvPVX(sv), Strerror(errno) );
79072805 657 do_close(gv,FALSE);
c623bd54
LW
658 continue;
659 }
b28d0864 660 (void)UNLINK(PL_oldname);
a687059c
LW
661#endif
662 }
663 else {
a8c18271 664#if !defined(DOSISH) && !defined(AMIGAOS)
edc7bc49 665# ifndef VMS /* Don't delete; use automatic file versioning */
3280af22 666 if (UNLINK(PL_oldname) < 0) {
0453d815
PM
667 if (ckWARN_d(WARN_INPLACE))
668 Perl_warner(aTHX_ WARN_INPLACE,
669 "Can't remove %s: %s, skipping file",
670 PL_oldname, Strerror(errno) );
79072805 671 do_close(gv,FALSE);
fe14fcc3
LW
672 continue;
673 }
edc7bc49 674# endif
ff8e2863 675#else
cea2e8a9 676 Perl_croak(aTHX_ "Can't do inplace edit without backup");
ff8e2863 677#endif
a687059c
LW
678 }
679
3280af22
NIS
680 sv_setpvn(sv,">",!PL_inplace);
681 sv_catpvn(sv,PL_oldname,oldlen);
748a9306 682 SETERRNO(0,0); /* in case sprintf set errno */
4119ab01
HM
683#ifdef VMS
684 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
18708f5a 685 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp))
4119ab01 686#else
3280af22 687 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
18708f5a 688 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp))
4119ab01 689#endif
18708f5a 690 {
0453d815
PM
691 if (ckWARN_d(WARN_INPLACE))
692 Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s",
693 PL_oldname, Strerror(errno) );
79072805 694 do_close(gv,FALSE);
fe14fcc3
LW
695 continue;
696 }
3280af22
NIS
697 setdefout(PL_argvoutgv);
698 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
699 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
fe14fcc3 700#ifdef HAS_FCHMOD
3280af22 701 (void)fchmod(PL_lastfd,PL_filemode);
a687059c 702#else
3e3baf6d
TB
703# if !(defined(WIN32) && defined(__BORLANDC__))
704 /* Borland runtime creates a readonly file! */
b28d0864 705 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
3e3baf6d 706# endif
a687059c 707#endif
3280af22 708 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
fe14fcc3 709#ifdef HAS_FCHOWN
3280af22 710 (void)fchown(PL_lastfd,fileuid,filegid);
a687059c 711#else
fe14fcc3 712#ifdef HAS_CHOWN
b28d0864 713 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
a687059c 714#endif
b1248f16 715#endif
fe14fcc3 716 }
a687059c 717 }
a0d0e21e 718 return IoIFP(GvIOp(gv));
a687059c 719 }
4d61ec05
GS
720 else {
721 dTHR;
722 if (ckWARN_d(WARN_INPLACE)) {
6af84f9f
GS
723 int eno = errno;
724 if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
725 && !S_ISREG(PL_statbuf.st_mode))
726 {
4d61ec05
GS
727 Perl_warner(aTHX_ WARN_INPLACE,
728 "Can't do inplace edit: %s is not a regular file",
9a7dcd9c 729 PL_oldname);
6af84f9f 730 }
4d61ec05 731 else
9a7dcd9c 732 Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s",
6af84f9f 733 PL_oldname, Strerror(eno));
4d61ec05
GS
734 }
735 }
a687059c 736 }
18708f5a
GS
737 if (io && (IoFLAGS(io) & IOf_ARGV))
738 IoFLAGS(io) |= IOf_START;
3280af22
NIS
739 if (PL_inplace) {
740 (void)do_close(PL_argvoutgv,FALSE);
7a1c5554
GS
741 if (io && (IoFLAGS(io) & IOf_ARGV)
742 && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
743 {
18708f5a
GS
744 GV *oldout = (GV*)av_pop(PL_argvout_stack);
745 setdefout(oldout);
746 SvREFCNT_dec(oldout);
747 return Nullfp;
748 }
4633a7c4 749 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
a687059c
LW
750 }
751 return Nullfp;
752}
753
fe14fcc3 754#ifdef HAS_PIPE
afd9f252 755void
864dbfa3 756Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv)
afd9f252 757{
79072805
LW
758 register IO *rstio;
759 register IO *wstio;
afd9f252
LW
760 int fd[2];
761
79072805 762 if (!rgv)
afd9f252 763 goto badexit;
79072805 764 if (!wgv)
afd9f252
LW
765 goto badexit;
766
a0d0e21e
LW
767 rstio = GvIOn(rgv);
768 wstio = GvIOn(wgv);
afd9f252 769
a0d0e21e 770 if (IoIFP(rstio))
79072805 771 do_close(rgv,FALSE);
a0d0e21e 772 if (IoIFP(wstio))
79072805 773 do_close(wgv,FALSE);
afd9f252 774
3028581b 775 if (PerlProc_pipe(fd) < 0)
afd9f252 776 goto badexit;
760ac839
LW
777 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
778 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
8990e307
LW
779 IoIFP(wstio) = IoOFP(wstio);
780 IoTYPE(rstio) = '<';
781 IoTYPE(wstio) = '>';
782 if (!IoIFP(rstio) || !IoOFP(wstio)) {
760ac839 783 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
3028581b 784 else PerlLIO_close(fd[0]);
760ac839 785 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
3028581b 786 else PerlLIO_close(fd[1]);
fe14fcc3
LW
787 goto badexit;
788 }
afd9f252 789
3280af22 790 sv_setsv(sv,&PL_sv_yes);
afd9f252
LW
791 return;
792
793badexit:
3280af22 794 sv_setsv(sv,&PL_sv_undef);
afd9f252
LW
795 return;
796}
b1248f16 797#endif
afd9f252 798
517844ec 799/* explicit renamed to avoid C++ conflict -- kja */
a687059c 800bool
864dbfa3 801Perl_do_close(pTHX_ GV *gv, bool not_implicit)
a687059c 802{
1193dd27
IZ
803 bool retval;
804 IO *io;
a687059c 805
79072805 806 if (!gv)
3280af22 807 gv = PL_argvgv;
a0d0e21e 808 if (!gv || SvTYPE(gv) != SVt_PVGV) {
1d2dff63
GS
809 if (not_implicit)
810 SETERRNO(EBADF,SS$_IVCHAN);
c2ab57d4 811 return FALSE;
99b89507 812 }
79072805
LW
813 io = GvIO(gv);
814 if (!io) { /* never opened */
1d2dff63 815 if (not_implicit) {
d008e5eb 816 dTHR;
599cee73 817 if (ckWARN(WARN_UNOPENED))
cea2e8a9 818 Perl_warner(aTHX_ WARN_UNOPENED,
599cee73 819 "Close on unopened file <%s>",GvENAME(gv));
1d2dff63
GS
820 SETERRNO(EBADF,SS$_IVCHAN);
821 }
a687059c
LW
822 return FALSE;
823 }
f2b5be74 824 retval = io_close(io, not_implicit);
517844ec 825 if (not_implicit) {
1193dd27
IZ
826 IoLINES(io) = 0;
827 IoPAGE(io) = 0;
828 IoLINES_LEFT(io) = IoPAGE_LEN(io);
829 }
830 IoTYPE(io) = ' ';
831 return retval;
832}
833
834bool
f2b5be74 835Perl_io_close(pTHX_ IO *io, bool not_implicit)
1193dd27
IZ
836{
837 bool retval = FALSE;
838 int status;
839
8990e307
LW
840 if (IoIFP(io)) {
841 if (IoTYPE(io) == '|') {
3028581b 842 status = PerlProc_pclose(IoIFP(io));
f2b5be74
GS
843 if (not_implicit) {
844 STATUS_NATIVE_SET(status);
845 retval = (STATUS_POSIX == 0);
846 }
847 else {
848 retval = (status != -1);
849 }
a687059c 850 }
8990e307 851 else if (IoTYPE(io) == '-')
a687059c
LW
852 retval = TRUE;
853 else {
8990e307 854 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
760ac839
LW
855 retval = (PerlIO_close(IoOFP(io)) != EOF);
856 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
c2ab57d4
LW
857 }
858 else
760ac839 859 retval = (PerlIO_close(IoIFP(io)) != EOF);
a687059c 860 }
8990e307 861 IoOFP(io) = IoIFP(io) = Nullfp;
79072805 862 }
f2b5be74 863 else if (not_implicit) {
20408e3c
GS
864 SETERRNO(EBADF,SS$_IVCHAN);
865 }
1193dd27 866
a687059c
LW
867 return retval;
868}
869
870bool
864dbfa3 871Perl_do_eof(pTHX_ GV *gv)
a687059c 872{
11343788 873 dTHR;
79072805 874 register IO *io;
a687059c
LW
875 int ch;
876
79072805 877 io = GvIO(gv);
a687059c 878
79072805 879 if (!io)
a687059c 880 return TRUE;
af8c498a
GS
881 else if (ckWARN(WARN_IO)
882 && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout()
883 || IoIFP(io) == PerlIO_stderr()))
884 {
885 SV* sv = sv_newmortal();
886 gv_efullname3(sv, gv, Nullch);
887 Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output",
888 SvPV_nolen(sv));
889 }
a687059c 890
8990e307 891 while (IoIFP(io)) {
a687059c 892
760ac839 893 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
a20bf0c3 894 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
760ac839
LW
895 return FALSE; /* this is the most usual case */
896 }
a687059c 897
760ac839 898 ch = PerlIO_getc(IoIFP(io));
a687059c 899 if (ch != EOF) {
760ac839 900 (void)PerlIO_ungetc(IoIFP(io),ch);
a687059c
LW
901 return FALSE;
902 }
760ac839 903 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
a20bf0c3
JH
904 if (PerlIO_get_cnt(IoIFP(io)) < -1)
905 PerlIO_set_cnt(IoIFP(io),-1);
760ac839 906 }
533c011a 907 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
3280af22 908 if (!nextargv(PL_argvgv)) /* get another fp handy */
a687059c
LW
909 return TRUE;
910 }
911 else
912 return TRUE; /* normal fp, definitely end of file */
913 }
914 return TRUE;
915}
916
5ff3f7a4 917Off_t
864dbfa3 918Perl_do_tell(pTHX_ GV *gv)
a687059c 919{
79072805 920 register IO *io;
96e4d5b1 921 register PerlIO *fp;
a687059c 922
96e4d5b1 923 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 924#ifdef ULTRIX_STDIO_BOTCH
96e4d5b1
PP
925 if (PerlIO_eof(fp))
926 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 927#endif
8903cb82 928 return PerlIO_tell(fp);
96e4d5b1 929 }
d008e5eb
GS
930 {
931 dTHR;
932 if (ckWARN(WARN_UNOPENED))
cea2e8a9 933 Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file");
d008e5eb 934 }
748a9306 935 SETERRNO(EBADF,RMS$_IFI);
5ff3f7a4 936 return (Off_t)-1;
a687059c
LW
937}
938
939bool
864dbfa3 940Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
a687059c 941{
79072805 942 register IO *io;
137443ea 943 register PerlIO *fp;
a687059c 944
137443ea 945 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
bee1dbe2 946#ifdef ULTRIX_STDIO_BOTCH
137443ea
PP
947 if (PerlIO_eof(fp))
948 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 949#endif
8903cb82 950 return PerlIO_seek(fp, pos, whence) >= 0;
137443ea 951 }
d008e5eb
GS
952 {
953 dTHR;
954 if (ckWARN(WARN_UNOPENED))
cea2e8a9 955 Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file");
d008e5eb 956 }
748a9306 957 SETERRNO(EBADF,RMS$_IFI);
a687059c
LW
958 return FALSE;
959}
960
97cc44eb 961Off_t
864dbfa3 962Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
8903cb82
PP
963{
964 register IO *io;
965 register PerlIO *fp;
966
967 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
3028581b 968 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
d008e5eb
GS
969 {
970 dTHR;
971 if (ckWARN(WARN_UNOPENED))
cea2e8a9 972 Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file");
d008e5eb 973 }
8903cb82 974 SETERRNO(EBADF,RMS$_IFI);
d9b3e12d 975 return (Off_t)-1;
8903cb82
PP
976}
977
6ff81951 978int
16fe6d59
GS
979Perl_mode_from_discipline(pTHX_ SV *discp)
980{
981 int mode = O_BINARY;
982 if (discp) {
983 STRLEN len;
984 char *s = SvPV(discp,len);
985 while (*s) {
986 if (*s == ':') {
987 switch (s[1]) {
988 case 'r':
989 if (len > 3 && strnEQ(s+1, "raw", 3)
990 && (!s[4] || s[4] == ':' || isSPACE(s[4])))
991 {
992 mode = O_BINARY;
993 s += 4;
994 len -= 4;
995 break;
996 }
997 /* FALL THROUGH */
998 case 'c':
999 if (len > 4 && strnEQ(s+1, "crlf", 4)
1000 && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1001 {
1002 mode = O_TEXT;
1003 s += 5;
1004 len -= 5;
1005 break;
1006 }
1007 /* FALL THROUGH */
1008 default:
1009 goto fail_discipline;
1010 }
1011 }
1012 else if (isSPACE(*s)) {
1013 ++s;
1014 --len;
1015 }
1016 else {
1017 char *end;
1018fail_discipline:
1019 end = strchr(s+1, ':');
1020 if (!end)
1021 end = s+len;
1022 Perl_croak(aTHX_ "Unknown discipline '%.*s'", end-s, s);
1023 }
1024 }
1025 }
1026 return mode;
1027}
1028
1029int
1030Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int mode)
6ff81951 1031{
6ff81951 1032#ifdef DOSISH
16fe6d59
GS
1033# if defined(atarist) || defined(__MINT__)
1034 if (!PerlIO_flush(fp)) {
1035 if (mode & O_BINARY)
1036 ((FILE*)fp)->_flag |= _IOBIN;
1037 else
1038 ((FILE*)fp)->_flag &= ~ _IOBIN;
6ff81951 1039 return 1;
16fe6d59
GS
1040 }
1041 return 0;
1042# else
1043 if (PerlLIO_setmode(PerlIO_fileno(fp), mode) != -1) {
1044# if defined(WIN32) && defined(__BORLANDC__)
6ff81951
GS
1045 /* The translation mode of the stream is maintained independent
1046 * of the translation mode of the fd in the Borland RTL (heavy
1047 * digging through their runtime sources reveal). User has to
1048 * set the mode explicitly for the stream (though they don't
1049 * document this anywhere). GSAR 97-5-24
1050 */
1051 PerlIO_seek(fp,0L,0);
16fe6d59
GS
1052 if (mode & O_BINARY)
1053 ((FILE*)fp)->flags |= _F_BIN;
1054 else
1055 ((FILE*)fp)->flags &= ~ _F_BIN;
1056# endif
6ff81951
GS
1057 return 1;
1058 }
1059 else
1060 return 0;
16fe6d59 1061# endif
6ff81951 1062#else
16fe6d59
GS
1063# if defined(USEMYBINMODE)
1064 if (my_binmode(fp, iotype, mode) != FALSE)
6ff81951
GS
1065 return 1;
1066 else
1067 return 0;
16fe6d59 1068# else
6ff81951 1069 return 1;
16fe6d59 1070# endif
6ff81951
GS
1071#endif
1072}
1073
a0d0e21e 1074#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
c2ab57d4 1075 /* code courtesy of William Kucharski */
fe14fcc3 1076#define HAS_CHSIZE
6eb13c3b 1077
517844ec 1078I32 my_chsize(fd, length)
79072805 1079I32 fd; /* file descriptor */
85e6fe83 1080Off_t length; /* length to set file to */
6eb13c3b 1081{
6eb13c3b
LW
1082 struct flock fl;
1083 struct stat filebuf;
1084
3028581b 1085 if (PerlLIO_fstat(fd, &filebuf) < 0)
6eb13c3b
LW
1086 return -1;
1087
1088 if (filebuf.st_size < length) {
1089
1090 /* extend file length */
1091
3028581b 1092 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
6eb13c3b
LW
1093 return -1;
1094
1095 /* write a "0" byte */
1096
3028581b 1097 if ((PerlLIO_write(fd, "", 1)) != 1)
6eb13c3b
LW
1098 return -1;
1099 }
1100 else {
1101 /* truncate length */
1102
1103 fl.l_whence = 0;
1104 fl.l_len = 0;
1105 fl.l_start = length;
a0d0e21e 1106 fl.l_type = F_WRLCK; /* write lock on file space */
6eb13c3b
LW
1107
1108 /*
a0d0e21e 1109 * This relies on the UNDOCUMENTED F_FREESP argument to
6eb13c3b
LW
1110 * fcntl(2), which truncates the file so that it ends at the
1111 * position indicated by fl.l_start.
1112 *
1113 * Will minor miracles never cease?
1114 */
1115
a0d0e21e 1116 if (fcntl(fd, F_FREESP, &fl) < 0)
6eb13c3b
LW
1117 return -1;
1118
1119 }
1120
1121 return 0;
1122}
a0d0e21e 1123#endif /* F_FREESP */
ff8e2863 1124
a687059c 1125bool
864dbfa3 1126Perl_do_print(pTHX_ register SV *sv, PerlIO *fp)
a687059c
LW
1127{
1128 register char *tmps;
463ee0b2 1129 STRLEN len;
a687059c 1130
79072805
LW
1131 /* assuming fp is checked earlier */
1132 if (!sv)
1133 return TRUE;
3280af22 1134 if (PL_ofmt) {
8990e307 1135 if (SvGMAGICAL(sv))
79072805 1136 mg_get(sv);
463ee0b2 1137 if (SvIOK(sv) && SvIVX(sv) != 0) {
65202027 1138 PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv));
760ac839 1139 return !PerlIO_error(fp);
79072805 1140 }
463ee0b2 1141 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
79072805 1142 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
3280af22 1143 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
760ac839 1144 return !PerlIO_error(fp);
79072805 1145 }
a687059c 1146 }
79072805
LW
1147 switch (SvTYPE(sv)) {
1148 case SVt_NULL:
d008e5eb
GS
1149 {
1150 dTHR;
1151 if (ckWARN(WARN_UNINITIALIZED))
b89fed5f 1152 report_uninit();
d008e5eb 1153 }
ff8e2863 1154 return TRUE;
79072805 1155 case SVt_IV:
a0d0e21e
LW
1156 if (SvIOK(sv)) {
1157 if (SvGMAGICAL(sv))
1158 mg_get(sv);
cf2093f6 1159 if (SvIsUV(sv))
57def98f 1160 PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
cf2093f6 1161 else
57def98f 1162 PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
760ac839 1163 return !PerlIO_error(fp);
a0d0e21e
LW
1164 }
1165 /* FALL THROUGH */
79072805 1166 default:
463ee0b2 1167 tmps = SvPV(sv, len);
79072805 1168 break;
ff8e2863 1169 }
94e4c244
JH
1170 /* To detect whether the process is about to overstep its
1171 * filesize limit we would need getrlimit(). We could then
1172 * also transparently raise the limit with setrlimit() --
1173 * but only until the system hard limit/the filesystem limit,
c5dd3cdd
JH
1174 * at which we would get EPERM. Note that when using buffered
1175 * io the write failure can be delayed until the flush/close. --jhi */
760ac839 1176 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
a687059c 1177 return FALSE;
760ac839 1178 return !PerlIO_error(fp);
a687059c
LW
1179}
1180
79072805 1181I32
cea2e8a9 1182Perl_my_stat(pTHX)
a687059c 1183{
4e35701f 1184 djSP;
79072805 1185 IO *io;
748a9306 1186 GV* tmpgv;
79072805 1187
533c011a 1188 if (PL_op->op_flags & OPf_REF) {
924508f0 1189 EXTEND(SP,1);
638eceb6 1190 tmpgv = cGVOP_gv;
748a9306
LW
1191 do_fstat:
1192 io = GvIO(tmpgv);
8990e307 1193 if (io && IoIFP(io)) {
3280af22
NIS
1194 PL_statgv = tmpgv;
1195 sv_setpv(PL_statname,"");
1196 PL_laststype = OP_STAT;
1197 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c
LW
1198 }
1199 else {
3280af22
NIS
1200 if (tmpgv == PL_defgv)
1201 return PL_laststatval;
599cee73 1202 if (ckWARN(WARN_UNOPENED))
cea2e8a9 1203 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
748a9306 1204 GvENAME(tmpgv));
3280af22
NIS
1205 PL_statgv = Nullgv;
1206 sv_setpv(PL_statname,"");
1207 return (PL_laststatval = -1);
a687059c
LW
1208 }
1209 }
1210 else {
748a9306 1211 SV* sv = POPs;
4b74e3fb 1212 char *s;
2d8e6c8d 1213 STRLEN n_a;
79072805 1214 PUTBACK;
748a9306
LW
1215 if (SvTYPE(sv) == SVt_PVGV) {
1216 tmpgv = (GV*)sv;
1217 goto do_fstat;
1218 }
1219 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1220 tmpgv = (GV*)SvRV(sv);
1221 goto do_fstat;
1222 }
1223
2d8e6c8d 1224 s = SvPV(sv, n_a);
3280af22
NIS
1225 PL_statgv = Nullgv;
1226 sv_setpv(PL_statname, s);
1227 PL_laststype = OP_STAT;
1228 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1229 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
cea2e8a9 1230 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 1231 return PL_laststatval;
a687059c
LW
1232 }
1233}
1234
79072805 1235I32
cea2e8a9 1236Perl_my_lstat(pTHX)
c623bd54 1237{
4e35701f 1238 djSP;
79072805 1239 SV *sv;
2d8e6c8d 1240 STRLEN n_a;
533c011a 1241 if (PL_op->op_flags & OPf_REF) {
924508f0 1242 EXTEND(SP,1);
638eceb6 1243 if (cGVOP_gv == PL_defgv) {
3280af22 1244 if (PL_laststype != OP_LSTAT)
cea2e8a9 1245 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1246 return PL_laststatval;
fe14fcc3 1247 }
cea2e8a9 1248 Perl_croak(aTHX_ "You can't use -l on a filehandle");
fe14fcc3 1249 }
c623bd54 1250
3280af22
NIS
1251 PL_laststype = OP_LSTAT;
1252 PL_statgv = Nullgv;
79072805
LW
1253 sv = POPs;
1254 PUTBACK;
2d8e6c8d 1255 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1256 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1257 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
cea2e8a9 1258 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1259 return PL_laststatval;
c623bd54
LW
1260}
1261
a687059c 1262bool
864dbfa3 1263Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1264{
d5a9bfb0
IZ
1265 return do_aexec5(really, mark, sp, 0, 0);
1266}
1267
1268bool
2aa1486d
GS
1269Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1270 int fd, int do_report)
d5a9bfb0 1271{
cd39f2b6
JH
1272#ifdef MACOS_TRADITIONAL
1273 Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1274#else
a687059c 1275 register char **a;
a687059c 1276 char *tmps;
2d8e6c8d 1277 STRLEN n_a;
a687059c 1278
79072805 1279 if (sp > mark) {
11343788 1280 dTHR;
3280af22
NIS
1281 New(401,PL_Argv, sp - mark + 1, char*);
1282 a = PL_Argv;
79072805
LW
1283 while (++mark <= sp) {
1284 if (*mark)
2d8e6c8d 1285 *a++ = SvPVx(*mark, n_a);
a687059c
LW
1286 else
1287 *a++ = "";
1288 }
1289 *a = Nullch;
3280af22 1290 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
79072805 1291 TAINT_ENV(); /* testing IFS here is overkill, probably */
2d8e6c8d 1292 if (really && *(tmps = SvPV(really, n_a)))
3280af22 1293 PerlProc_execvp(tmps,PL_Argv);
a687059c 1294 else
3280af22 1295 PerlProc_execvp(PL_Argv[0],PL_Argv);
599cee73 1296 if (ckWARN(WARN_EXEC))
cea2e8a9 1297 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
599cee73 1298 PL_Argv[0], Strerror(errno));
d5a9bfb0
IZ
1299 if (do_report) {
1300 int e = errno;
1301
1302 PerlLIO_write(fd, (void*)&e, sizeof(int));
1303 PerlLIO_close(fd);
1304 }
a687059c 1305 }
bee1dbe2 1306 do_execfree();
cd39f2b6 1307#endif
a687059c
LW
1308 return FALSE;
1309}
1310
fe14fcc3 1311void
864dbfa3 1312Perl_do_execfree(pTHX)
ff8e2863 1313{
3280af22
NIS
1314 if (PL_Argv) {
1315 Safefree(PL_Argv);
1316 PL_Argv = Null(char **);
ff8e2863 1317 }
3280af22
NIS
1318 if (PL_Cmd) {
1319 Safefree(PL_Cmd);
1320 PL_Cmd = Nullch;
ff8e2863
LW
1321 }
1322}
1323
cd39f2b6 1324#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL)
760ac839 1325
a687059c 1326bool
864dbfa3 1327Perl_do_exec(pTHX_ char *cmd)
a687059c 1328{
e446cec8
IZ
1329 return do_exec3(cmd,0,0);
1330}
1331
1332bool
864dbfa3 1333Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1334{
a687059c
LW
1335 register char **a;
1336 register char *s;
a687059c
LW
1337 char flags[10];
1338
748a9306
LW
1339 while (*cmd && isSPACE(*cmd))
1340 cmd++;
1341
a687059c
LW
1342 /* save an extra exec if possible */
1343
bf38876a 1344#ifdef CSH
3280af22 1345 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1346 strcpy(flags,"-c");
3280af22 1347 s = cmd+PL_cshlen+3;
a687059c
LW
1348 if (*s == 'f') {
1349 s++;
1350 strcat(flags,"f");
1351 }
1352 if (*s == ' ')
1353 s++;
1354 if (*s++ == '\'') {
1355 char *ncmd = s;
1356
1357 while (*s)
1358 s++;
1359 if (s[-1] == '\n')
1360 *--s = '\0';
1361 if (s[-1] == '\'') {
1362 *--s = '\0';
3280af22 1363 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c
LW
1364 *s = '\'';
1365 return FALSE;
1366 }
1367 }
1368 }
bf38876a 1369#endif /* CSH */
a687059c
LW
1370
1371 /* see if there are shell metacharacters in it */
1372
748a9306
LW
1373 if (*cmd == '.' && isSPACE(cmd[1]))
1374 goto doshell;
1375
1376 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1377 goto doshell;
1378
c170e444 1379 for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1
LW
1380 if (*s == '=')
1381 goto doshell;
748a9306 1382
a687059c 1383 for (s = cmd; *s; s++) {
93a17b20 1384 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c
LW
1385 if (*s == '\n' && !s[1]) {
1386 *s = '\0';
1387 break;
1388 }
603a98b0
IZ
1389 /* handle the 2>&1 construct at the end */
1390 if (*s == '>' && s[1] == '&' && s[2] == '1'
1391 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1392 && (!s[3] || isSPACE(s[3])))
1393 {
1394 char *t = s + 3;
1395
1396 while (*t && isSPACE(*t))
1397 ++t;
1398 if (!*t && (dup2(1,2) != -1)) {
1399 s[-2] = '\0';
1400 break;
1401 }
1402 }
a687059c 1403 doshell:
3280af22 1404 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c
LW
1405 return FALSE;
1406 }
1407 }
748a9306 1408
3280af22
NIS
1409 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1410 PL_Cmd = savepvn(cmd, s-cmd);
1411 a = PL_Argv;
1412 for (s = PL_Cmd; *s;) {
99b89507 1413 while (*s && isSPACE(*s)) s++;
a687059c
LW
1414 if (*s)
1415 *(a++) = s;
99b89507 1416 while (*s && !isSPACE(*s)) s++;
a687059c
LW
1417 if (*s)
1418 *s++ = '\0';
1419 }
1420 *a = Nullch;
3280af22
NIS
1421 if (PL_Argv[0]) {
1422 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1423 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1424 do_execfree();
a687059c 1425 goto doshell;
b1248f16 1426 }
d008e5eb
GS
1427 {
1428 dTHR;
e446cec8
IZ
1429 int e = errno;
1430
d008e5eb 1431 if (ckWARN(WARN_EXEC))
cea2e8a9 1432 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
d008e5eb 1433 PL_Argv[0], Strerror(errno));
e446cec8
IZ
1434 if (do_report) {
1435 PerlLIO_write(fd, (void*)&e, sizeof(int));
1436 PerlLIO_close(fd);
1437 }
d008e5eb 1438 }
a687059c 1439 }
ff8e2863 1440 do_execfree();
a687059c
LW
1441 return FALSE;
1442}
1443
6890e559 1444#endif /* OS2 || WIN32 */
760ac839 1445
79072805 1446I32
864dbfa3 1447Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1448{
11343788 1449 dTHR;
79072805
LW
1450 register I32 val;
1451 register I32 val2;
1452 register I32 tot = 0;
20408e3c 1453 char *what;
a687059c 1454 char *s;
79072805 1455 SV **oldmark = mark;
2d8e6c8d 1456 STRLEN n_a;
a687059c 1457
20408e3c 1458#define APPLY_TAINT_PROPER() \
3280af22 1459 STMT_START { \
17406bd6 1460 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1461 } STMT_END
20408e3c
GS
1462
1463 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1464 if (PL_tainting) {
463ee0b2 1465 while (++mark <= sp) {
bbce6d69
PP
1466 if (SvTAINTED(*mark)) {
1467 TAINT;
1468 break;
1469 }
463ee0b2
LW
1470 }
1471 mark = oldmark;
1472 }
a687059c 1473 switch (type) {
79072805 1474 case OP_CHMOD:
20408e3c
GS
1475 what = "chmod";
1476 APPLY_TAINT_PROPER();
79072805 1477 if (++mark <= sp) {
463ee0b2 1478 val = SvIVx(*mark);
20408e3c
GS
1479 APPLY_TAINT_PROPER();
1480 tot = sp - mark;
79072805 1481 while (++mark <= sp) {
2d8e6c8d 1482 char *name = SvPVx(*mark, n_a);
20408e3c
GS
1483 APPLY_TAINT_PROPER();
1484 if (PerlLIO_chmod(name, val))
a687059c
LW
1485 tot--;
1486 }
1487 }
1488 break;
fe14fcc3 1489#ifdef HAS_CHOWN
79072805 1490 case OP_CHOWN:
20408e3c
GS
1491 what = "chown";
1492 APPLY_TAINT_PROPER();
79072805 1493 if (sp - mark > 2) {
463ee0b2
LW
1494 val = SvIVx(*++mark);
1495 val2 = SvIVx(*++mark);
20408e3c 1496 APPLY_TAINT_PROPER();
a0d0e21e 1497 tot = sp - mark;
79072805 1498 while (++mark <= sp) {
2d8e6c8d 1499 char *name = SvPVx(*mark, n_a);
20408e3c 1500 APPLY_TAINT_PROPER();
36660982 1501 if (PerlLIO_chown(name, val, val2))
a687059c
LW
1502 tot--;
1503 }
1504 }
1505 break;
b1248f16 1506#endif
dd64f1c3
AD
1507/*
1508XXX Should we make lchown() directly available from perl?
1509For now, we'll let Configure test for HAS_LCHOWN, but do
1510nothing in the core.
1511 --AD 5/1998
1512*/
fe14fcc3 1513#ifdef HAS_KILL
79072805 1514 case OP_KILL:
20408e3c
GS
1515 what = "kill";
1516 APPLY_TAINT_PROPER();
55497cff
PP
1517 if (mark == sp)
1518 break;
2d8e6c8d 1519 s = SvPVx(*++mark, n_a);
79072805
LW
1520 if (isUPPER(*s)) {
1521 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1522 s += 3;
1523 if (!(val = whichsig(s)))
cea2e8a9 1524 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805
LW
1525 }
1526 else
463ee0b2 1527 val = SvIVx(*mark);
20408e3c
GS
1528 APPLY_TAINT_PROPER();
1529 tot = sp - mark;
3595fcef
PP
1530#ifdef VMS
1531 /* kill() doesn't do process groups (job trees?) under VMS */
1532 if (val < 0) val = -val;
1533 if (val == SIGKILL) {
1534# include <starlet.h>
1535 /* Use native sys$delprc() to insure that target process is
1536 * deleted; supervisor-mode images don't pay attention to
1537 * CRTL's emulation of Unix-style signals and kill()
1538 */
1539 while (++mark <= sp) {
1540 I32 proc = SvIVx(*mark);
1541 register unsigned long int __vmssts;
20408e3c 1542 APPLY_TAINT_PROPER();
3595fcef
PP
1543 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1544 tot--;
1545 switch (__vmssts) {
1546 case SS$_NONEXPR:
1547 case SS$_NOSUCHNODE:
1548 SETERRNO(ESRCH,__vmssts);
1549 break;
1550 case SS$_NOPRIV:
1551 SETERRNO(EPERM,__vmssts);
1552 break;
1553 default:
1554 SETERRNO(EVMSERR,__vmssts);
1555 }
1556 }
1557 }
1558 break;
1559 }
1560#endif
79072805
LW
1561 if (val < 0) {
1562 val = -val;
1563 while (++mark <= sp) {
463ee0b2 1564 I32 proc = SvIVx(*mark);
20408e3c 1565 APPLY_TAINT_PROPER();
fe14fcc3 1566#ifdef HAS_KILLPG
3028581b 1567 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1568#else
3028581b 1569 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1570#endif
79072805 1571 tot--;
a687059c 1572 }
79072805
LW
1573 }
1574 else {
1575 while (++mark <= sp) {
20408e3c
GS
1576 I32 proc = SvIVx(*mark);
1577 APPLY_TAINT_PROPER();
1578 if (PerlProc_kill(proc, val))
79072805 1579 tot--;
a687059c
LW
1580 }
1581 }
1582 break;
b1248f16 1583#endif
79072805 1584 case OP_UNLINK:
20408e3c
GS
1585 what = "unlink";
1586 APPLY_TAINT_PROPER();
79072805
LW
1587 tot = sp - mark;
1588 while (++mark <= sp) {
2d8e6c8d 1589 s = SvPVx(*mark, n_a);
20408e3c 1590 APPLY_TAINT_PROPER();
3280af22 1591 if (PL_euid || PL_unsafe) {
a687059c
LW
1592 if (UNLINK(s))
1593 tot--;
1594 }
1595 else { /* don't let root wipe out directories without -U */
3280af22 1596 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c
LW
1597 tot--;
1598 else {
1599 if (UNLINK(s))
1600 tot--;
1601 }
1602 }
1603 }
1604 break;
a0d0e21e 1605#ifdef HAS_UTIME
79072805 1606 case OP_UTIME:
20408e3c
GS
1607 what = "utime";
1608 APPLY_TAINT_PROPER();
79072805 1609 if (sp - mark > 2) {
748a9306 1610#if defined(I_UTIME) || defined(VMS)
663a0e37
LW
1611 struct utimbuf utbuf;
1612#else
a687059c 1613 struct {
dd2821f6
GS
1614 Time_t actime;
1615 Time_t modtime;
a687059c 1616 } utbuf;
663a0e37 1617#endif
a687059c 1618
afd9f252 1619 Zero(&utbuf, sizeof utbuf, char);
517844ec 1620#ifdef BIG_TIME
dd2821f6
GS
1621 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1622 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1623#else
dd2821f6
GS
1624 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1625 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1626#endif
20408e3c 1627 APPLY_TAINT_PROPER();
79072805
LW
1628 tot = sp - mark;
1629 while (++mark <= sp) {
2d8e6c8d 1630 char *name = SvPVx(*mark, n_a);
20408e3c
GS
1631 APPLY_TAINT_PROPER();
1632 if (PerlLIO_utime(name, &utbuf))
a687059c
LW
1633 tot--;
1634 }
a687059c
LW
1635 }
1636 else
79072805 1637 tot = 0;
a687059c 1638 break;
a0d0e21e 1639#endif
a687059c
LW
1640 }
1641 return tot;
20408e3c 1642
20408e3c 1643#undef APPLY_TAINT_PROPER
a687059c
LW
1644}
1645
1646/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1647#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae
JH
1648bool
1649Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1650/* Note: we use `effective' both for uids and gids.
1651 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1652{
bee1dbe2 1653#ifdef DOSISH
fe14fcc3
LW
1654 /* [Comments and code from Len Reed]
1655 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1656 * to write-protected files. The execute permission bit is set
1657 * by the Miscrosoft C library stat() function for the following:
1658 * .exe files
1659 * .com files
1660 * .bat files
1661 * directories
1662 * All files and directories are readable.
1663 * Directories and special files, e.g. "CON", cannot be
1664 * write-protected.
1665 * [Comment by Tom Dinger -- a directory can have the write-protect
1666 * bit set in the file system, but DOS permits changes to
1667 * the directory anyway. In addition, all bets are off
1668 * here for networked software, such as Novell and
1669 * Sun's PC-NFS.]
1670 */
1671
bee1dbe2
LW
1672 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1673 * too so it will actually look into the files for magic numbers
1674 */
7f4774ae 1675 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1676
55497cff 1677#else /* ! DOSISH */
3280af22 1678 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1679 if (mode == S_IXUSR) {
c623bd54 1680 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c
LW
1681 return TRUE;
1682 }
1683 else
1684 return TRUE; /* root reads and writes anything */
1685 return FALSE;
1686 }
3280af22 1687 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1688 if (statbufp->st_mode & mode)
a687059c
LW
1689 return TRUE; /* ok as "user" */
1690 }
d8eceb89 1691 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1692 if (statbufp->st_mode & mode >> 3)
a687059c
LW
1693 return TRUE; /* ok as "group" */
1694 }
7f4774ae 1695 else if (statbufp->st_mode & mode >> 6)
a687059c
LW
1696 return TRUE; /* ok as "other" */
1697 return FALSE;
55497cff 1698#endif /* ! DOSISH */
a687059c 1699}
a0d0e21e 1700#endif /* ! VMS */
a687059c 1701
d8eceb89
JH
1702bool
1703Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1704{
cd39f2b6
JH
1705#ifdef MACOS_TRADITIONAL
1706 /* This is simply not correct for AppleShare, but fix it yerself. */
1707 return TRUE;
1708#else
3280af22 1709 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1710 return TRUE;
fe14fcc3 1711#ifdef HAS_GETGROUPS
a687059c
LW
1712#ifndef NGROUPS
1713#define NGROUPS 32
1714#endif
1715 {
a0d0e21e 1716 Groups_t gary[NGROUPS];
79072805 1717 I32 anum;
a687059c
LW
1718
1719 anum = getgroups(NGROUPS,gary);
1720 while (--anum >= 0)
1721 if (gary[anum] == testgid)
1722 return TRUE;
1723 }
1724#endif
1725 return FALSE;
cd39f2b6 1726#endif
a687059c 1727}
c2ab57d4 1728
fe14fcc3 1729#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1730
79072805 1731I32
864dbfa3 1732Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1733{
11343788 1734 dTHR;
c2ab57d4 1735 key_t key;
79072805 1736 I32 n, flags;
c2ab57d4 1737
463ee0b2
LW
1738 key = (key_t)SvNVx(*++mark);
1739 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1740 flags = SvIVx(*++mark);
748a9306 1741 SETERRNO(0,0);
c2ab57d4
LW
1742 switch (optype)
1743 {
fe14fcc3 1744#ifdef HAS_MSG
79072805 1745 case OP_MSGGET:
c2ab57d4 1746 return msgget(key, flags);
e5d73d77 1747#endif
fe14fcc3 1748#ifdef HAS_SEM
79072805 1749 case OP_SEMGET:
c2ab57d4 1750 return semget(key, n, flags);
e5d73d77 1751#endif
fe14fcc3 1752#ifdef HAS_SHM
79072805 1753 case OP_SHMGET:
c2ab57d4 1754 return shmget(key, n, flags);
e5d73d77 1755#endif
fe14fcc3 1756#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1757 default:
cea2e8a9 1758 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1759#endif
c2ab57d4
LW
1760 }
1761 return -1; /* should never happen */
1762}
1763
79072805 1764I32
864dbfa3 1765Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1766{
11343788 1767 dTHR;
79072805 1768 SV *astr;
c2ab57d4 1769 char *a;
a0d0e21e
LW
1770 I32 id, n, cmd, infosize, getinfo;
1771 I32 ret = -1;
c2ab57d4 1772
463ee0b2
LW
1773 id = SvIVx(*++mark);
1774 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1775 cmd = SvIVx(*++mark);
79072805 1776 astr = *++mark;
c2ab57d4
LW
1777 infosize = 0;
1778 getinfo = (cmd == IPC_STAT);
1779
1780 switch (optype)
1781 {
fe14fcc3 1782#ifdef HAS_MSG
79072805 1783 case OP_MSGCTL:
c2ab57d4
LW
1784 if (cmd == IPC_STAT || cmd == IPC_SET)
1785 infosize = sizeof(struct msqid_ds);
1786 break;
e5d73d77 1787#endif
fe14fcc3 1788#ifdef HAS_SHM
79072805 1789 case OP_SHMCTL:
c2ab57d4
LW
1790 if (cmd == IPC_STAT || cmd == IPC_SET)
1791 infosize = sizeof(struct shmid_ds);
1792 break;
e5d73d77 1793#endif
fe14fcc3 1794#ifdef HAS_SEM
79072805 1795 case OP_SEMCTL:
39398f3f 1796#ifdef Semctl
c2ab57d4
LW
1797 if (cmd == IPC_STAT || cmd == IPC_SET)
1798 infosize = sizeof(struct semid_ds);
1799 else if (cmd == GETALL || cmd == SETALL)
1800 {
8e591e46 1801 struct semid_ds semds;
bd89102f 1802 union semun semun;
e6f0bdd6
GS
1803#ifdef EXTRA_F_IN_SEMUN_BUF
1804 semun.buff = &semds;
1805#else
84902520 1806 semun.buf = &semds;
e6f0bdd6 1807#endif
c2ab57d4 1808 getinfo = (cmd == GETALL);
9b89d93d
GB
1809 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1810 return -1;
6e21c824
LW
1811 infosize = semds.sem_nsems * sizeof(short);
1812 /* "short" is technically wrong but much more portable
1813 than guessing about u_?short(_t)? */
c2ab57d4 1814 }
39398f3f 1815#else
cea2e8a9 1816 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1817#endif
c2ab57d4 1818 break;
e5d73d77 1819#endif
fe14fcc3 1820#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1821 default:
cea2e8a9 1822 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1823#endif
c2ab57d4
LW
1824 }
1825
1826 if (infosize)
1827 {
a0d0e21e 1828 STRLEN len;
c2ab57d4
LW
1829 if (getinfo)
1830 {
a0d0e21e
LW
1831 SvPV_force(astr, len);
1832 a = SvGROW(astr, infosize+1);
c2ab57d4
LW
1833 }
1834 else
1835 {
463ee0b2
LW
1836 a = SvPV(astr, len);
1837 if (len != infosize)
cea2e8a9 1838 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091
JH
1839 PL_op_desc[optype],
1840 (unsigned long)len,
1841 (long)infosize);
c2ab57d4
LW
1842 }
1843 }
1844 else
1845 {
c030ccd9 1846 IV i = SvIV(astr);
56431972 1847 a = INT2PTR(char *,i); /* ouch */
c2ab57d4 1848 }
748a9306 1849 SETERRNO(0,0);
c2ab57d4
LW
1850 switch (optype)
1851 {
fe14fcc3 1852#ifdef HAS_MSG
79072805 1853 case OP_MSGCTL:
bee1dbe2 1854 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1855 break;
e5d73d77 1856#endif
fe14fcc3 1857#ifdef HAS_SEM
bd89102f 1858 case OP_SEMCTL: {
39398f3f 1859#ifdef Semctl
bd89102f
AD
1860 union semun unsemds;
1861
e6f0bdd6
GS
1862#ifdef EXTRA_F_IN_SEMUN_BUF
1863 unsemds.buff = (struct semid_ds *)a;
1864#else
bd89102f 1865 unsemds.buf = (struct semid_ds *)a;
e6f0bdd6 1866#endif
bd89102f 1867 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1868#else
cea2e8a9 1869 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1870#endif
bd89102f 1871 }
c2ab57d4 1872 break;
e5d73d77 1873#endif
fe14fcc3 1874#ifdef HAS_SHM
79072805 1875 case OP_SHMCTL:
bee1dbe2 1876 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1877 break;
e5d73d77 1878#endif
c2ab57d4
LW
1879 }
1880 if (getinfo && ret >= 0) {
79072805
LW
1881 SvCUR_set(astr, infosize);
1882 *SvEND(astr) = '\0';
a0d0e21e 1883 SvSETMAGIC(astr);
c2ab57d4
LW
1884 }
1885 return ret;
1886}
1887
79072805 1888I32
864dbfa3 1889Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 1890{
fe14fcc3 1891#ifdef HAS_MSG
11343788 1892 dTHR;
79072805 1893 SV *mstr;
c2ab57d4 1894 char *mbuf;
79072805 1895 I32 id, msize, flags;
463ee0b2 1896 STRLEN len;
c2ab57d4 1897
463ee0b2 1898 id = SvIVx(*++mark);
79072805 1899 mstr = *++mark;
463ee0b2
LW
1900 flags = SvIVx(*++mark);
1901 mbuf = SvPV(mstr, len);
1902 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 1903 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 1904 SETERRNO(0,0);
bee1dbe2 1905 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1906#else
cea2e8a9 1907 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 1908#endif
c2ab57d4
LW
1909}
1910
79072805 1911I32
864dbfa3 1912Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 1913{
fe14fcc3 1914#ifdef HAS_MSG
11343788 1915 dTHR;
79072805 1916 SV *mstr;
c2ab57d4
LW
1917 char *mbuf;
1918 long mtype;
79072805 1919 I32 id, msize, flags, ret;
463ee0b2 1920 STRLEN len;
79072805 1921
463ee0b2 1922 id = SvIVx(*++mark);
79072805 1923 mstr = *++mark;
463ee0b2
LW
1924 msize = SvIVx(*++mark);
1925 mtype = (long)SvIVx(*++mark);
1926 flags = SvIVx(*++mark);
a0d0e21e
LW
1927 SvPV_force(mstr, len);
1928 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1929
748a9306 1930 SETERRNO(0,0);
bee1dbe2 1931 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1932 if (ret >= 0) {
79072805
LW
1933 SvCUR_set(mstr, sizeof(long)+ret);
1934 *SvEND(mstr) = '\0';
41d6edb2
JH
1935#ifndef INCOMPLETE_TAINTS
1936 /* who knows who has been playing with this message? */
1937 SvTAINTED_on(mstr);
1938#endif
c2ab57d4
LW
1939 }
1940 return ret;
e5d73d77 1941#else
cea2e8a9 1942 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 1943#endif
c2ab57d4
LW
1944}
1945
79072805 1946I32
864dbfa3 1947Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 1948{
fe14fcc3 1949#ifdef HAS_SEM
11343788 1950 dTHR;
79072805 1951 SV *opstr;
c2ab57d4 1952 char *opbuf;
463ee0b2
LW
1953 I32 id;
1954 STRLEN opsize;
c2ab57d4 1955
463ee0b2 1956 id = SvIVx(*++mark);
79072805 1957 opstr = *++mark;
463ee0b2 1958 opbuf = SvPV(opstr, opsize);
c2ab57d4
LW
1959 if (opsize < sizeof(struct sembuf)
1960 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 1961 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4
LW
1962 return -1;
1963 }
748a9306 1964 SETERRNO(0,0);
6e21c824 1965 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1966#else
cea2e8a9 1967 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 1968#endif
c2ab57d4
LW
1969}
1970
79072805 1971I32
864dbfa3 1972Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1973{
fe14fcc3 1974#ifdef HAS_SHM
11343788 1975 dTHR;
79072805 1976 SV *mstr;
c2ab57d4 1977 char *mbuf, *shm;
79072805 1978 I32 id, mpos, msize;
463ee0b2 1979 STRLEN len;
c2ab57d4 1980 struct shmid_ds shmds;
c2ab57d4 1981
463ee0b2 1982 id = SvIVx(*++mark);
79072805 1983 mstr = *++mark;
463ee0b2
LW
1984 mpos = SvIVx(*++mark);
1985 msize = SvIVx(*++mark);
748a9306 1986 SETERRNO(0,0);
c2ab57d4
LW
1987 if (shmctl(id, IPC_STAT, &shmds) == -1)
1988 return -1;
1989 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 1990 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4
LW
1991 return -1;
1992 }
8ac85365 1993 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4
LW
1994 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1995 return -1;
79072805 1996 if (optype == OP_SHMREAD) {
9f538c04
GS
1997 /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
1998 if (! SvOK(mstr))
1999 sv_setpvn(mstr, "", 0);
a0d0e21e
LW
2000 SvPV_force(mstr, len);
2001 mbuf = SvGROW(mstr, msize+1);
2002
bee1dbe2 2003 Copy(shm + mpos, mbuf, msize, char);
79072805
LW
2004 SvCUR_set(mstr, msize);
2005 *SvEND(mstr) = '\0';
a0d0e21e 2006 SvSETMAGIC(mstr);
d929ce6f
JH
2007#ifndef INCOMPLETE_TAINTS
2008 /* who knows who has been playing with this shared memory? */
2009 SvTAINTED_on(mstr);
2010#endif
c2ab57d4
LW
2011 }
2012 else {
79072805 2013 I32 n;
c2ab57d4 2014
a0d0e21e 2015 mbuf = SvPV(mstr, len);
463ee0b2 2016 if ((n = len) > msize)
c2ab57d4 2017 n = msize;
bee1dbe2 2018 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 2019 if (n < msize)
bee1dbe2 2020 memzero(shm + mpos + n, msize - n);
c2ab57d4
LW
2021 }
2022 return shmdt(shm);
e5d73d77 2023#else
cea2e8a9 2024 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 2025#endif
c2ab57d4
LW
2026}
2027
fe14fcc3 2028#endif /* SYSV IPC */
4e35701f 2029