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