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