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