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