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