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