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