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