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