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