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