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