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