Catch the case of filesize limits.
[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);
286 /* Flush stdio buffer before dup */
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 }
760ac839 1025 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
a687059c 1026 return FALSE;
760ac839 1027 return !PerlIO_error(fp);
a687059c 1028}
1029
79072805 1030I32
cea2e8a9 1031Perl_my_stat(pTHX)
a687059c 1032{
4e35701f 1033 djSP;
79072805 1034 IO *io;
748a9306 1035 GV* tmpgv;
79072805 1036
533c011a 1037 if (PL_op->op_flags & OPf_REF) {
924508f0 1038 EXTEND(SP,1);
748a9306 1039 tmpgv = cGVOP->op_gv;
1040 do_fstat:
1041 io = GvIO(tmpgv);
8990e307 1042 if (io && IoIFP(io)) {
3280af22 1043 PL_statgv = tmpgv;
1044 sv_setpv(PL_statname,"");
1045 PL_laststype = OP_STAT;
1046 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
a687059c 1047 }
1048 else {
3280af22 1049 if (tmpgv == PL_defgv)
1050 return PL_laststatval;
599cee73 1051 if (ckWARN(WARN_UNOPENED))
cea2e8a9 1052 Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>",
748a9306 1053 GvENAME(tmpgv));
3280af22 1054 PL_statgv = Nullgv;
1055 sv_setpv(PL_statname,"");
1056 return (PL_laststatval = -1);
a687059c 1057 }
1058 }
1059 else {
748a9306 1060 SV* sv = POPs;
4b74e3fb 1061 char *s;
2d8e6c8d 1062 STRLEN n_a;
79072805 1063 PUTBACK;
748a9306 1064 if (SvTYPE(sv) == SVt_PVGV) {
1065 tmpgv = (GV*)sv;
1066 goto do_fstat;
1067 }
1068 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
1069 tmpgv = (GV*)SvRV(sv);
1070 goto do_fstat;
1071 }
1072
2d8e6c8d 1073 s = SvPV(sv, n_a);
3280af22 1074 PL_statgv = Nullgv;
1075 sv_setpv(PL_statname, s);
1076 PL_laststype = OP_STAT;
1077 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
599cee73 1078 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
cea2e8a9 1079 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat");
3280af22 1080 return PL_laststatval;
a687059c 1081 }
1082}
1083
79072805 1084I32
cea2e8a9 1085Perl_my_lstat(pTHX)
c623bd54 1086{
4e35701f 1087 djSP;
79072805 1088 SV *sv;
2d8e6c8d 1089 STRLEN n_a;
533c011a 1090 if (PL_op->op_flags & OPf_REF) {
924508f0 1091 EXTEND(SP,1);
3280af22 1092 if (cGVOP->op_gv == PL_defgv) {
1093 if (PL_laststype != OP_LSTAT)
cea2e8a9 1094 Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat");
3280af22 1095 return PL_laststatval;
fe14fcc3 1096 }
cea2e8a9 1097 Perl_croak(aTHX_ "You can't use -l on a filehandle");
fe14fcc3 1098 }
c623bd54 1099
3280af22 1100 PL_laststype = OP_LSTAT;
1101 PL_statgv = Nullgv;
79072805 1102 sv = POPs;
1103 PUTBACK;
2d8e6c8d 1104 sv_setpv(PL_statname,SvPV(sv, n_a));
2d8e6c8d 1105 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
2d8e6c8d 1106 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
cea2e8a9 1107 Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat");
3280af22 1108 return PL_laststatval;
c623bd54 1109}
1110
a687059c 1111bool
864dbfa3 1112Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp)
a687059c 1113{
d5a9bfb0 1114 return do_aexec5(really, mark, sp, 0, 0);
1115}
1116
1117bool
2aa1486d 1118Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp,
1119 int fd, int do_report)
d5a9bfb0 1120{
a687059c 1121 register char **a;
a687059c 1122 char *tmps;
2d8e6c8d 1123 STRLEN n_a;
a687059c 1124
79072805 1125 if (sp > mark) {
11343788 1126 dTHR;
3280af22 1127 New(401,PL_Argv, sp - mark + 1, char*);
1128 a = PL_Argv;
79072805 1129 while (++mark <= sp) {
1130 if (*mark)
2d8e6c8d 1131 *a++ = SvPVx(*mark, n_a);
a687059c 1132 else
1133 *a++ = "";
1134 }
1135 *a = Nullch;
3280af22 1136 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
79072805 1137 TAINT_ENV(); /* testing IFS here is overkill, probably */
2d8e6c8d 1138 if (really && *(tmps = SvPV(really, n_a)))
3280af22 1139 PerlProc_execvp(tmps,PL_Argv);
a687059c 1140 else
3280af22 1141 PerlProc_execvp(PL_Argv[0],PL_Argv);
599cee73 1142 if (ckWARN(WARN_EXEC))
cea2e8a9 1143 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
599cee73 1144 PL_Argv[0], Strerror(errno));
d5a9bfb0 1145 if (do_report) {
1146 int e = errno;
1147
1148 PerlLIO_write(fd, (void*)&e, sizeof(int));
1149 PerlLIO_close(fd);
1150 }
a687059c 1151 }
bee1dbe2 1152 do_execfree();
a687059c 1153 return FALSE;
1154}
1155
fe14fcc3 1156void
864dbfa3 1157Perl_do_execfree(pTHX)
ff8e2863 1158{
3280af22 1159 if (PL_Argv) {
1160 Safefree(PL_Argv);
1161 PL_Argv = Null(char **);
ff8e2863 1162 }
3280af22 1163 if (PL_Cmd) {
1164 Safefree(PL_Cmd);
1165 PL_Cmd = Nullch;
ff8e2863 1166 }
1167}
1168
4d2c4e07 1169#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC)
760ac839 1170
a687059c 1171bool
864dbfa3 1172Perl_do_exec(pTHX_ char *cmd)
a687059c 1173{
e446cec8 1174 return do_exec3(cmd,0,0);
1175}
1176
1177bool
864dbfa3 1178Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report)
e446cec8 1179{
a687059c 1180 register char **a;
1181 register char *s;
a687059c 1182 char flags[10];
1183
748a9306 1184 while (*cmd && isSPACE(*cmd))
1185 cmd++;
1186
a687059c 1187 /* save an extra exec if possible */
1188
bf38876a 1189#ifdef CSH
3280af22 1190 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
a687059c 1191 strcpy(flags,"-c");
3280af22 1192 s = cmd+PL_cshlen+3;
a687059c 1193 if (*s == 'f') {
1194 s++;
1195 strcat(flags,"f");
1196 }
1197 if (*s == ' ')
1198 s++;
1199 if (*s++ == '\'') {
1200 char *ncmd = s;
1201
1202 while (*s)
1203 s++;
1204 if (s[-1] == '\n')
1205 *--s = '\0';
1206 if (s[-1] == '\'') {
1207 *--s = '\0';
3280af22 1208 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
a687059c 1209 *s = '\'';
1210 return FALSE;
1211 }
1212 }
1213 }
bf38876a 1214#endif /* CSH */
a687059c 1215
1216 /* see if there are shell metacharacters in it */
1217
748a9306 1218 if (*cmd == '.' && isSPACE(cmd[1]))
1219 goto doshell;
1220
1221 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1222 goto doshell;
1223
99b89507 1224 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1 1225 if (*s == '=')
1226 goto doshell;
748a9306 1227
a687059c 1228 for (s = cmd; *s; s++) {
93a17b20 1229 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c 1230 if (*s == '\n' && !s[1]) {
1231 *s = '\0';
1232 break;
1233 }
603a98b0 1234 /* handle the 2>&1 construct at the end */
1235 if (*s == '>' && s[1] == '&' && s[2] == '1'
1236 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1237 && (!s[3] || isSPACE(s[3])))
1238 {
1239 char *t = s + 3;
1240
1241 while (*t && isSPACE(*t))
1242 ++t;
1243 if (!*t && (dup2(1,2) != -1)) {
1244 s[-2] = '\0';
1245 break;
1246 }
1247 }
a687059c 1248 doshell:
3280af22 1249 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
a687059c 1250 return FALSE;
1251 }
1252 }
748a9306 1253
3280af22 1254 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1255 PL_Cmd = savepvn(cmd, s-cmd);
1256 a = PL_Argv;
1257 for (s = PL_Cmd; *s;) {
99b89507 1258 while (*s && isSPACE(*s)) s++;
a687059c 1259 if (*s)
1260 *(a++) = s;
99b89507 1261 while (*s && !isSPACE(*s)) s++;
a687059c 1262 if (*s)
1263 *s++ = '\0';
1264 }
1265 *a = Nullch;
3280af22 1266 if (PL_Argv[0]) {
1267 PerlProc_execvp(PL_Argv[0],PL_Argv);
b1248f16 1268 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1269 do_execfree();
a687059c 1270 goto doshell;
b1248f16 1271 }
d008e5eb 1272 {
1273 dTHR;
e446cec8 1274 int e = errno;
1275
d008e5eb 1276 if (ckWARN(WARN_EXEC))
cea2e8a9 1277 Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
d008e5eb 1278 PL_Argv[0], Strerror(errno));
e446cec8 1279 if (do_report) {
1280 PerlLIO_write(fd, (void*)&e, sizeof(int));
1281 PerlLIO_close(fd);
1282 }
d008e5eb 1283 }
a687059c 1284 }
ff8e2863 1285 do_execfree();
a687059c 1286 return FALSE;
1287}
1288
6890e559 1289#endif /* OS2 || WIN32 */
760ac839 1290
79072805 1291I32
864dbfa3 1292Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp)
a687059c 1293{
11343788 1294 dTHR;
79072805 1295 register I32 val;
1296 register I32 val2;
1297 register I32 tot = 0;
20408e3c 1298 char *what;
a687059c 1299 char *s;
79072805 1300 SV **oldmark = mark;
2d8e6c8d 1301 STRLEN n_a;
a687059c 1302
20408e3c 1303#define APPLY_TAINT_PROPER() \
3280af22 1304 STMT_START { \
17406bd6 1305 if (PL_tainted) { TAINT_PROPER(what); } \
873ef191 1306 } STMT_END
20408e3c 1307
1308 /* This is a first heuristic; it doesn't catch tainting magic. */
3280af22 1309 if (PL_tainting) {
463ee0b2 1310 while (++mark <= sp) {
bbce6d69 1311 if (SvTAINTED(*mark)) {
1312 TAINT;
1313 break;
1314 }
463ee0b2 1315 }
1316 mark = oldmark;
1317 }
a687059c 1318 switch (type) {
79072805 1319 case OP_CHMOD:
20408e3c 1320 what = "chmod";
1321 APPLY_TAINT_PROPER();
79072805 1322 if (++mark <= sp) {
463ee0b2 1323 val = SvIVx(*mark);
20408e3c 1324 APPLY_TAINT_PROPER();
1325 tot = sp - mark;
79072805 1326 while (++mark <= sp) {
2d8e6c8d 1327 char *name = SvPVx(*mark, n_a);
20408e3c 1328 APPLY_TAINT_PROPER();
1329 if (PerlLIO_chmod(name, val))
a687059c 1330 tot--;
1331 }
1332 }
1333 break;
fe14fcc3 1334#ifdef HAS_CHOWN
79072805 1335 case OP_CHOWN:
20408e3c 1336 what = "chown";
1337 APPLY_TAINT_PROPER();
79072805 1338 if (sp - mark > 2) {
463ee0b2 1339 val = SvIVx(*++mark);
1340 val2 = SvIVx(*++mark);
20408e3c 1341 APPLY_TAINT_PROPER();
a0d0e21e 1342 tot = sp - mark;
79072805 1343 while (++mark <= sp) {
2d8e6c8d 1344 char *name = SvPVx(*mark, n_a);
20408e3c 1345 APPLY_TAINT_PROPER();
36660982 1346 if (PerlLIO_chown(name, val, val2))
a687059c 1347 tot--;
1348 }
1349 }
1350 break;
b1248f16 1351#endif
dd64f1c3 1352/*
1353XXX Should we make lchown() directly available from perl?
1354For now, we'll let Configure test for HAS_LCHOWN, but do
1355nothing in the core.
1356 --AD 5/1998
1357*/
fe14fcc3 1358#ifdef HAS_KILL
79072805 1359 case OP_KILL:
20408e3c 1360 what = "kill";
1361 APPLY_TAINT_PROPER();
55497cff 1362 if (mark == sp)
1363 break;
2d8e6c8d 1364 s = SvPVx(*++mark, n_a);
79072805 1365 if (isUPPER(*s)) {
1366 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1367 s += 3;
1368 if (!(val = whichsig(s)))
cea2e8a9 1369 Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s);
79072805 1370 }
1371 else
463ee0b2 1372 val = SvIVx(*mark);
20408e3c 1373 APPLY_TAINT_PROPER();
1374 tot = sp - mark;
3595fcef 1375#ifdef VMS
1376 /* kill() doesn't do process groups (job trees?) under VMS */
1377 if (val < 0) val = -val;
1378 if (val == SIGKILL) {
1379# include <starlet.h>
1380 /* Use native sys$delprc() to insure that target process is
1381 * deleted; supervisor-mode images don't pay attention to
1382 * CRTL's emulation of Unix-style signals and kill()
1383 */
1384 while (++mark <= sp) {
1385 I32 proc = SvIVx(*mark);
1386 register unsigned long int __vmssts;
20408e3c 1387 APPLY_TAINT_PROPER();
3595fcef 1388 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1389 tot--;
1390 switch (__vmssts) {
1391 case SS$_NONEXPR:
1392 case SS$_NOSUCHNODE:
1393 SETERRNO(ESRCH,__vmssts);
1394 break;
1395 case SS$_NOPRIV:
1396 SETERRNO(EPERM,__vmssts);
1397 break;
1398 default:
1399 SETERRNO(EVMSERR,__vmssts);
1400 }
1401 }
1402 }
1403 break;
1404 }
1405#endif
79072805 1406 if (val < 0) {
1407 val = -val;
1408 while (++mark <= sp) {
463ee0b2 1409 I32 proc = SvIVx(*mark);
20408e3c 1410 APPLY_TAINT_PROPER();
fe14fcc3 1411#ifdef HAS_KILLPG
3028581b 1412 if (PerlProc_killpg(proc,val)) /* BSD */
a687059c 1413#else
3028581b 1414 if (PerlProc_kill(-proc,val)) /* SYSV */
a687059c 1415#endif
79072805 1416 tot--;
a687059c 1417 }
79072805 1418 }
1419 else {
1420 while (++mark <= sp) {
20408e3c 1421 I32 proc = SvIVx(*mark);
1422 APPLY_TAINT_PROPER();
1423 if (PerlProc_kill(proc, val))
79072805 1424 tot--;
a687059c 1425 }
1426 }
1427 break;
b1248f16 1428#endif
79072805 1429 case OP_UNLINK:
20408e3c 1430 what = "unlink";
1431 APPLY_TAINT_PROPER();
79072805 1432 tot = sp - mark;
1433 while (++mark <= sp) {
2d8e6c8d 1434 s = SvPVx(*mark, n_a);
20408e3c 1435 APPLY_TAINT_PROPER();
3280af22 1436 if (PL_euid || PL_unsafe) {
a687059c 1437 if (UNLINK(s))
1438 tot--;
1439 }
1440 else { /* don't let root wipe out directories without -U */
3280af22 1441 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
a687059c 1442 tot--;
1443 else {
1444 if (UNLINK(s))
1445 tot--;
1446 }
1447 }
1448 }
1449 break;
a0d0e21e 1450#ifdef HAS_UTIME
79072805 1451 case OP_UTIME:
20408e3c 1452 what = "utime";
1453 APPLY_TAINT_PROPER();
79072805 1454 if (sp - mark > 2) {
748a9306 1455#if defined(I_UTIME) || defined(VMS)
663a0e37 1456 struct utimbuf utbuf;
1457#else
a687059c 1458 struct {
dd2821f6 1459 Time_t actime;
1460 Time_t modtime;
a687059c 1461 } utbuf;
663a0e37 1462#endif
a687059c 1463
afd9f252 1464 Zero(&utbuf, sizeof utbuf, char);
517844ec 1465#ifdef BIG_TIME
dd2821f6 1466 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1467 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
517844ec 1468#else
dd2821f6 1469 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1470 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
517844ec 1471#endif
20408e3c 1472 APPLY_TAINT_PROPER();
79072805 1473 tot = sp - mark;
1474 while (++mark <= sp) {
2d8e6c8d 1475 char *name = SvPVx(*mark, n_a);
20408e3c 1476 APPLY_TAINT_PROPER();
1477 if (PerlLIO_utime(name, &utbuf))
a687059c 1478 tot--;
1479 }
a687059c 1480 }
1481 else
79072805 1482 tot = 0;
a687059c 1483 break;
a0d0e21e 1484#endif
a687059c 1485 }
1486 return tot;
20408e3c 1487
20408e3c 1488#undef APPLY_TAINT_PROPER
a687059c 1489}
1490
1491/* Do the permissions allow some operation? Assumes statcache already set. */
a0d0e21e 1492#ifndef VMS /* VMS' cando is in vms.c */
7f4774ae 1493bool
1494Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp)
1495/* Note: we use `effective' both for uids and gids.
1496 * Here we are betting on Uid_t being equal or wider than Gid_t. */
a687059c 1497{
bee1dbe2 1498#ifdef DOSISH
fe14fcc3 1499 /* [Comments and code from Len Reed]
1500 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1501 * to write-protected files. The execute permission bit is set
1502 * by the Miscrosoft C library stat() function for the following:
1503 * .exe files
1504 * .com files
1505 * .bat files
1506 * directories
1507 * All files and directories are readable.
1508 * Directories and special files, e.g. "CON", cannot be
1509 * write-protected.
1510 * [Comment by Tom Dinger -- a directory can have the write-protect
1511 * bit set in the file system, but DOS permits changes to
1512 * the directory anyway. In addition, all bets are off
1513 * here for networked software, such as Novell and
1514 * Sun's PC-NFS.]
1515 */
1516
bee1dbe2 1517 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1518 * too so it will actually look into the files for magic numbers
1519 */
7f4774ae 1520 return (mode & statbufp->st_mode) ? TRUE : FALSE;
fe14fcc3 1521
55497cff 1522#else /* ! DOSISH */
3280af22 1523 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
7f4774ae 1524 if (mode == S_IXUSR) {
c623bd54 1525 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c 1526 return TRUE;
1527 }
1528 else
1529 return TRUE; /* root reads and writes anything */
1530 return FALSE;
1531 }
3280af22 1532 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
7f4774ae 1533 if (statbufp->st_mode & mode)
a687059c 1534 return TRUE; /* ok as "user" */
1535 }
d8eceb89 1536 else if (ingroup(statbufp->st_gid,effective)) {
7f4774ae 1537 if (statbufp->st_mode & mode >> 3)
a687059c 1538 return TRUE; /* ok as "group" */
1539 }
7f4774ae 1540 else if (statbufp->st_mode & mode >> 6)
a687059c 1541 return TRUE; /* ok as "other" */
1542 return FALSE;
55497cff 1543#endif /* ! DOSISH */
a687059c 1544}
a0d0e21e 1545#endif /* ! VMS */
a687059c 1546
d8eceb89 1547bool
1548Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective)
a687059c 1549{
3280af22 1550 if (testgid == (effective ? PL_egid : PL_gid))
a687059c 1551 return TRUE;
fe14fcc3 1552#ifdef HAS_GETGROUPS
a687059c 1553#ifndef NGROUPS
1554#define NGROUPS 32
1555#endif
1556 {
a0d0e21e 1557 Groups_t gary[NGROUPS];
79072805 1558 I32 anum;
a687059c 1559
1560 anum = getgroups(NGROUPS,gary);
1561 while (--anum >= 0)
1562 if (gary[anum] == testgid)
1563 return TRUE;
1564 }
1565#endif
1566 return FALSE;
1567}
c2ab57d4 1568
fe14fcc3 1569#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1570
79072805 1571I32
864dbfa3 1572Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1573{
11343788 1574 dTHR;
c2ab57d4 1575 key_t key;
79072805 1576 I32 n, flags;
c2ab57d4 1577
463ee0b2 1578 key = (key_t)SvNVx(*++mark);
1579 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1580 flags = SvIVx(*++mark);
748a9306 1581 SETERRNO(0,0);
c2ab57d4 1582 switch (optype)
1583 {
fe14fcc3 1584#ifdef HAS_MSG
79072805 1585 case OP_MSGGET:
c2ab57d4 1586 return msgget(key, flags);
e5d73d77 1587#endif
fe14fcc3 1588#ifdef HAS_SEM
79072805 1589 case OP_SEMGET:
c2ab57d4 1590 return semget(key, n, flags);
e5d73d77 1591#endif
fe14fcc3 1592#ifdef HAS_SHM
79072805 1593 case OP_SHMGET:
c2ab57d4 1594 return shmget(key, n, flags);
e5d73d77 1595#endif
fe14fcc3 1596#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1597 default:
cea2e8a9 1598 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1599#endif
c2ab57d4 1600 }
1601 return -1; /* should never happen */
1602}
1603
79072805 1604I32
864dbfa3 1605Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1606{
11343788 1607 dTHR;
79072805 1608 SV *astr;
c2ab57d4 1609 char *a;
a0d0e21e 1610 I32 id, n, cmd, infosize, getinfo;
1611 I32 ret = -1;
c2ab57d4 1612
463ee0b2 1613 id = SvIVx(*++mark);
1614 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1615 cmd = SvIVx(*++mark);
79072805 1616 astr = *++mark;
c2ab57d4 1617 infosize = 0;
1618 getinfo = (cmd == IPC_STAT);
1619
1620 switch (optype)
1621 {
fe14fcc3 1622#ifdef HAS_MSG
79072805 1623 case OP_MSGCTL:
c2ab57d4 1624 if (cmd == IPC_STAT || cmd == IPC_SET)
1625 infosize = sizeof(struct msqid_ds);
1626 break;
e5d73d77 1627#endif
fe14fcc3 1628#ifdef HAS_SHM
79072805 1629 case OP_SHMCTL:
c2ab57d4 1630 if (cmd == IPC_STAT || cmd == IPC_SET)
1631 infosize = sizeof(struct shmid_ds);
1632 break;
e5d73d77 1633#endif
fe14fcc3 1634#ifdef HAS_SEM
79072805 1635 case OP_SEMCTL:
39398f3f 1636#ifdef Semctl
c2ab57d4 1637 if (cmd == IPC_STAT || cmd == IPC_SET)
1638 infosize = sizeof(struct semid_ds);
1639 else if (cmd == GETALL || cmd == SETALL)
1640 {
8e591e46 1641 struct semid_ds semds;
bd89102f 1642 union semun semun;
1643
84902520 1644 semun.buf = &semds;
c2ab57d4 1645 getinfo = (cmd == GETALL);
9b89d93d 1646 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1647 return -1;
6e21c824 1648 infosize = semds.sem_nsems * sizeof(short);
1649 /* "short" is technically wrong but much more portable
1650 than guessing about u_?short(_t)? */
c2ab57d4 1651 }
39398f3f 1652#else
cea2e8a9 1653 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1654#endif
c2ab57d4 1655 break;
e5d73d77 1656#endif
fe14fcc3 1657#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1658 default:
cea2e8a9 1659 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
e5d73d77 1660#endif
c2ab57d4 1661 }
1662
1663 if (infosize)
1664 {
a0d0e21e 1665 STRLEN len;
c2ab57d4 1666 if (getinfo)
1667 {
a0d0e21e 1668 SvPV_force(astr, len);
1669 a = SvGROW(astr, infosize+1);
c2ab57d4 1670 }
1671 else
1672 {
463ee0b2 1673 a = SvPV(astr, len);
1674 if (len != infosize)
cea2e8a9 1675 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
4ec43091 1676 PL_op_desc[optype],
1677 (unsigned long)len,
1678 (long)infosize);
c2ab57d4 1679 }
1680 }
1681 else
1682 {
c030ccd9 1683 IV i = SvIV(astr);
c2ab57d4 1684 a = (char *)i; /* ouch */
1685 }
748a9306 1686 SETERRNO(0,0);
c2ab57d4 1687 switch (optype)
1688 {
fe14fcc3 1689#ifdef HAS_MSG
79072805 1690 case OP_MSGCTL:
bee1dbe2 1691 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1692 break;
e5d73d77 1693#endif
fe14fcc3 1694#ifdef HAS_SEM
bd89102f 1695 case OP_SEMCTL: {
39398f3f 1696#ifdef Semctl
bd89102f 1697 union semun unsemds;
1698
1699 unsemds.buf = (struct semid_ds *)a;
1700 ret = Semctl(id, n, cmd, unsemds);
39398f3f 1701#else
cea2e8a9 1702 Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
39398f3f 1703#endif
bd89102f 1704 }
c2ab57d4 1705 break;
e5d73d77 1706#endif
fe14fcc3 1707#ifdef HAS_SHM
79072805 1708 case OP_SHMCTL:
bee1dbe2 1709 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1710 break;
e5d73d77 1711#endif
c2ab57d4 1712 }
1713 if (getinfo && ret >= 0) {
79072805 1714 SvCUR_set(astr, infosize);
1715 *SvEND(astr) = '\0';
a0d0e21e 1716 SvSETMAGIC(astr);
c2ab57d4 1717 }
1718 return ret;
1719}
1720
79072805 1721I32
864dbfa3 1722Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
c2ab57d4 1723{
fe14fcc3 1724#ifdef HAS_MSG
11343788 1725 dTHR;
79072805 1726 SV *mstr;
c2ab57d4 1727 char *mbuf;
79072805 1728 I32 id, msize, flags;
463ee0b2 1729 STRLEN len;
c2ab57d4 1730
463ee0b2 1731 id = SvIVx(*++mark);
79072805 1732 mstr = *++mark;
463ee0b2 1733 flags = SvIVx(*++mark);
1734 mbuf = SvPV(mstr, len);
1735 if ((msize = len - sizeof(long)) < 0)
cea2e8a9 1736 Perl_croak(aTHX_ "Arg too short for msgsnd");
748a9306 1737 SETERRNO(0,0);
bee1dbe2 1738 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1739#else
cea2e8a9 1740 Perl_croak(aTHX_ "msgsnd not implemented");
e5d73d77 1741#endif
c2ab57d4 1742}
1743
79072805 1744I32
864dbfa3 1745Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
c2ab57d4 1746{
fe14fcc3 1747#ifdef HAS_MSG
11343788 1748 dTHR;
79072805 1749 SV *mstr;
c2ab57d4 1750 char *mbuf;
1751 long mtype;
79072805 1752 I32 id, msize, flags, ret;
463ee0b2 1753 STRLEN len;
79072805 1754
463ee0b2 1755 id = SvIVx(*++mark);
79072805 1756 mstr = *++mark;
463ee0b2 1757 msize = SvIVx(*++mark);
1758 mtype = (long)SvIVx(*++mark);
1759 flags = SvIVx(*++mark);
a0d0e21e 1760 SvPV_force(mstr, len);
1761 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1762
748a9306 1763 SETERRNO(0,0);
bee1dbe2 1764 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1765 if (ret >= 0) {
79072805 1766 SvCUR_set(mstr, sizeof(long)+ret);
1767 *SvEND(mstr) = '\0';
c2ab57d4 1768 }
1769 return ret;
e5d73d77 1770#else
cea2e8a9 1771 Perl_croak(aTHX_ "msgrcv not implemented");
e5d73d77 1772#endif
c2ab57d4 1773}
1774
79072805 1775I32
864dbfa3 1776Perl_do_semop(pTHX_ SV **mark, SV **sp)
c2ab57d4 1777{
fe14fcc3 1778#ifdef HAS_SEM
11343788 1779 dTHR;
79072805 1780 SV *opstr;
c2ab57d4 1781 char *opbuf;
463ee0b2 1782 I32 id;
1783 STRLEN opsize;
c2ab57d4 1784
463ee0b2 1785 id = SvIVx(*++mark);
79072805 1786 opstr = *++mark;
463ee0b2 1787 opbuf = SvPV(opstr, opsize);
c2ab57d4 1788 if (opsize < sizeof(struct sembuf)
1789 || (opsize % sizeof(struct sembuf)) != 0) {
748a9306 1790 SETERRNO(EINVAL,LIB$_INVARG);
c2ab57d4 1791 return -1;
1792 }
748a9306 1793 SETERRNO(0,0);
6e21c824 1794 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1795#else
cea2e8a9 1796 Perl_croak(aTHX_ "semop not implemented");
e5d73d77 1797#endif
c2ab57d4 1798}
1799
79072805 1800I32
864dbfa3 1801Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
c2ab57d4 1802{
fe14fcc3 1803#ifdef HAS_SHM
11343788 1804 dTHR;
79072805 1805 SV *mstr;
c2ab57d4 1806 char *mbuf, *shm;
79072805 1807 I32 id, mpos, msize;
463ee0b2 1808 STRLEN len;
c2ab57d4 1809 struct shmid_ds shmds;
c2ab57d4 1810
463ee0b2 1811 id = SvIVx(*++mark);
79072805 1812 mstr = *++mark;
463ee0b2 1813 mpos = SvIVx(*++mark);
1814 msize = SvIVx(*++mark);
748a9306 1815 SETERRNO(0,0);
c2ab57d4 1816 if (shmctl(id, IPC_STAT, &shmds) == -1)
1817 return -1;
1818 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
748a9306 1819 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
c2ab57d4 1820 return -1;
1821 }
8ac85365 1822 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4 1823 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1824 return -1;
79072805 1825 if (optype == OP_SHMREAD) {
a0d0e21e 1826 SvPV_force(mstr, len);
1827 mbuf = SvGROW(mstr, msize+1);
1828
bee1dbe2 1829 Copy(shm + mpos, mbuf, msize, char);
79072805 1830 SvCUR_set(mstr, msize);
1831 *SvEND(mstr) = '\0';
a0d0e21e 1832 SvSETMAGIC(mstr);
c2ab57d4 1833 }
1834 else {
79072805 1835 I32 n;
c2ab57d4 1836
a0d0e21e 1837 mbuf = SvPV(mstr, len);
463ee0b2 1838 if ((n = len) > msize)
c2ab57d4 1839 n = msize;
bee1dbe2 1840 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 1841 if (n < msize)
bee1dbe2 1842 memzero(shm + mpos + n, msize - n);
c2ab57d4 1843 }
1844 return shmdt(shm);
e5d73d77 1845#else
cea2e8a9 1846 Perl_croak(aTHX_ "shm I/O not implemented");
e5d73d77 1847#endif
c2ab57d4 1848}
1849
fe14fcc3 1850#endif /* SYSV IPC */
4e35701f 1851