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