3 * Copyright (c) 1991-1997, Larry Wall
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.
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."
20 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 # ifndef HAS_SHMAT_PROTOTYPE
31 extern Shmat_t shmat _((int, char *, int));
37 # if defined(_MSC_VER) || defined(__MINGW32__)
38 # include <sys/utime.h>
51 # define OPEN_EXCL O_EXCL
56 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
60 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
65 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
66 # include <sys/socket.h>
70 # include <net/errno.h>
75 /* Put this after #includes because <unistd.h> defines _XOPEN_*. */
77 # if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
78 # define Sock_size_t Size_t
80 # define Sock_size_t int
85 do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawperm, PerlIO *supplied_fp)
87 register IO *io = GvIOn(gv);
88 PerlIO *saveifp = Nullfp;
89 PerlIO *saveofp = Nullfp;
95 bool was_fdopen = FALSE;
97 PL_forkprocess = 1; /* assume true if no fork */
100 fd = PerlIO_fileno(IoIFP(io));
101 if (IoTYPE(io) == '-')
103 else if (fd <= PL_maxsysfd) {
106 savetype = IoTYPE(io);
109 else if (IoTYPE(io) == '|')
110 result = PerlProc_pclose(IoIFP(io));
111 else if (IoIFP(io) != IoOFP(io)) {
113 result = PerlIO_close(IoOFP(io));
114 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
117 result = PerlIO_close(IoIFP(io));
120 result = PerlIO_close(IoIFP(io));
121 if (result == EOF && fd > PL_maxsysfd)
122 PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n",
124 IoOFP(io) = IoIFP(io) = Nullfp;
128 #if defined(O_LARGEFILE)
129 rawmode |= O_LARGEFILE;
133 #define O_ACCMODE 3 /* Assume traditional implementation */
136 switch (result = rawmode & O_ACCMODE) {
149 writing = (result > 0);
150 fd = PerlLIO_open3(name, rawmode, rawperm);
156 if (result == O_RDONLY)
159 else if (rawmode & O_APPEND)
160 fpmode = (result == O_WRONLY) ? "a" : "a+";
163 fpmode = (result == O_WRONLY) ? "w" : "r+";
164 fp = PerlIO_fdopen(fd, fpmode);
171 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
174 myname = savepvn(name, len);
177 while (len && isSPACE(name[len-1]))
180 mode[0] = mode[1] = mode[2] = '\0';
182 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
190 for (name++; isSPACE(*name); name++) ;
191 if (*name == '\0') { /* command is missing 19990114 */
193 if (ckWARN(WARN_PIPE))
194 warner(WARN_PIPE, "Missing command in piped open");
200 TAINT_PROPER("piped open");
201 if (name[strlen(name)-1] == '|') {
203 name[strlen(name)-1] = '\0' ;
204 if (ckWARN(WARN_PIPE))
205 warner(WARN_PIPE, "Can't do bidirectional pipe");
207 fp = PerlProc_popen(name,"w");
210 else if (*name == '>') {
211 TAINT_PROPER("open");
214 mode[0] = IoTYPE(io) = 'a';
229 if (!*name && supplied_fp)
233 for (; isSPACE(*name); name++) ;
238 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
242 SETERRNO(EINVAL,SS$_IVCHAN);
247 fd = PerlIO_fileno(IoIFP(thatio));
248 if (IoTYPE(thatio) == 's')
255 fd = PerlLIO_dup(fd);
258 if (!(fp = PerlIO_fdopen(fd,mode))) {
266 for (; isSPACE(*name); name++) ;
267 if (strEQ(name,"-")) {
268 fp = PerlIO_stdout();
272 fp = PerlIO_open(name,mode);
276 else if (*name == '<') {
278 for (name++; isSPACE(*name); name++) ;
282 if (strEQ(name,"-")) {
287 fp = PerlIO_open(name,mode);
289 else if (len > 1 && name[len-1] == '|') {
291 while (len && isSPACE(name[len-1]))
294 for (; isSPACE(*name); name++) ;
295 if (*name == '\0') { /* command is missing 19990114 */
297 if (ckWARN(WARN_PIPE))
298 warner(WARN_PIPE, "Missing command in piped open");
304 TAINT_PROPER("piped open");
305 fp = PerlProc_popen(name,"r");
311 for (; isSPACE(*name); name++) ;
312 if (strEQ(name,"-")) {
317 fp = PerlIO_open(name,"r");
322 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
323 warner(WARN_NEWLINE, PL_warn_nl, "open");
327 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
329 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
330 (void)PerlIO_close(fp);
333 if (S_ISSOCK(PL_statbuf.st_mode))
334 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
338 !(PL_statbuf.st_mode & S_IFMT)
344 Sock_size_t buflen = sizeof tmpbuf;
345 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
347 || errno != ENOTSOCK)
348 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
349 /* but some return 0 for streams too, sigh */
353 if (saveifp) { /* must use old fp? */
354 fd = PerlIO_fileno(saveifp);
356 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
357 if (saveofp != saveifp) { /* was a socket? */
358 PerlIO_close(saveofp);
363 if (fd != PerlIO_fileno(fp)) {
367 PerlLIO_dup2(PerlIO_fileno(fp), fd);
368 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
369 (void)SvUPGRADE(sv, SVt_IV);
372 sv = *av_fetch(PL_fdpid,fd,TRUE);
373 (void)SvUPGRADE(sv, SVt_IV);
382 #if defined(HAS_FCNTL) && defined(F_SETFD)
384 int save_errno = errno;
385 fd = PerlIO_fileno(fp);
386 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
393 if (IoTYPE(io) == 's'
394 || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) {
395 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
409 IoTYPE(io) = savetype;
414 nextargv(register GV *gv)
417 #ifndef FLEXFILENAMES
425 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
426 if (PL_filemode & (S_ISUID|S_ISGID)) {
427 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
429 (void)fchmod(PL_lastfd,PL_filemode);
431 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
435 while (av_len(GvAV(gv)) >= 0) {
438 sv = av_shift(GvAV(gv));
440 sv_setsv(GvSV(gv),sv);
441 SvSETMAGIC(GvSV(gv));
442 PL_oldname = SvPVx(GvSV(gv), oldlen);
443 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
445 TAINT_PROPER("inplace open");
446 if (oldlen == 1 && *PL_oldname == '-') {
447 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
448 return IoIFP(GvIOp(gv));
450 #ifndef FLEXFILENAMES
451 filedev = PL_statbuf.st_dev;
452 fileino = PL_statbuf.st_ino;
454 PL_filemode = PL_statbuf.st_mode;
455 fileuid = PL_statbuf.st_uid;
456 filegid = PL_statbuf.st_gid;
457 if (!S_ISREG(PL_filemode)) {
458 warn("Can't do inplace edit: %s is not a regular file",
464 char *star = strchr(PL_inplace, '*');
466 char *begin = PL_inplace;
467 sv_setpvn(sv, "", 0);
469 sv_catpvn(sv, begin, star - begin);
470 sv_catpvn(sv, PL_oldname, oldlen);
472 } while ((star = strchr(begin, '*')));
477 sv_catpv(sv,PL_inplace);
479 #ifndef FLEXFILENAMES
480 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
481 && PL_statbuf.st_dev == filedev
482 && PL_statbuf.st_ino == fileino
484 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
487 warn("Can't do inplace edit: %s would not be unique",
495 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
496 warn("Can't rename %s to %s: %s, skipping file",
497 PL_oldname, SvPVX(sv), Strerror(errno) );
503 (void)PerlLIO_unlink(SvPVX(sv));
504 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
505 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
508 (void)UNLINK(SvPVX(sv));
509 if (link(PL_oldname,SvPVX(sv)) < 0) {
510 warn("Can't rename %s to %s: %s, skipping file",
511 PL_oldname, SvPVX(sv), Strerror(errno) );
515 (void)UNLINK(PL_oldname);
519 #if !defined(DOSISH) && !defined(AMIGAOS)
520 # ifndef VMS /* Don't delete; use automatic file versioning */
521 if (UNLINK(PL_oldname) < 0) {
522 warn("Can't remove %s: %s, skipping file",
523 PL_oldname, Strerror(errno) );
529 croak("Can't do inplace edit without backup");
533 sv_setpvn(sv,">",!PL_inplace);
534 sv_catpvn(sv,PL_oldname,oldlen);
535 SETERRNO(0,0); /* in case sprintf set errno */
537 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
538 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) {
540 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
541 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
543 warn("Can't do inplace edit on %s: %s",
544 PL_oldname, Strerror(errno) );
548 setdefout(PL_argvoutgv);
549 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
550 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
552 (void)fchmod(PL_lastfd,PL_filemode);
554 # if !(defined(WIN32) && defined(__BORLANDC__))
555 /* Borland runtime creates a readonly file! */
556 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
559 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
561 (void)fchown(PL_lastfd,fileuid,filegid);
564 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
569 return IoIFP(GvIOp(gv));
572 PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
573 SvPV(sv, oldlen), Strerror(errno));
576 (void)do_close(PL_argvoutgv,FALSE);
577 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
584 do_pipe(SV *sv, GV *rgv, GV *wgv)
603 if (PerlProc_pipe(fd) < 0)
605 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
606 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
607 IoIFP(wstio) = IoOFP(wstio);
610 if (!IoIFP(rstio) || !IoOFP(wstio)) {
611 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
612 else PerlLIO_close(fd[0]);
613 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
614 else PerlLIO_close(fd[1]);
618 sv_setsv(sv,&PL_sv_yes);
622 sv_setsv(sv,&PL_sv_undef);
627 /* explicit renamed to avoid C++ conflict -- kja */
629 do_close(GV *gv, bool not_implicit)
636 if (!gv || SvTYPE(gv) != SVt_PVGV) {
638 SETERRNO(EBADF,SS$_IVCHAN);
642 if (!io) { /* never opened */
645 if (ckWARN(WARN_UNOPENED))
646 warner(WARN_UNOPENED,
647 "Close on unopened file <%s>",GvENAME(gv));
648 SETERRNO(EBADF,SS$_IVCHAN);
652 retval = io_close(io);
656 IoLINES_LEFT(io) = IoPAGE_LEN(io);
669 if (IoTYPE(io) == '|') {
670 status = PerlProc_pclose(IoIFP(io));
671 STATUS_NATIVE_SET(status);
672 retval = (STATUS_POSIX == 0);
674 else if (IoTYPE(io) == '-')
677 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
678 retval = (PerlIO_close(IoOFP(io)) != EOF);
679 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
682 retval = (PerlIO_close(IoIFP(io)) != EOF);
684 IoOFP(io) = IoIFP(io) = Nullfp;
687 SETERRNO(EBADF,SS$_IVCHAN);
707 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
708 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
709 return FALSE; /* this is the most usual case */
712 ch = PerlIO_getc(IoIFP(io));
714 (void)PerlIO_ungetc(IoIFP(io),ch);
717 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
718 if (PerlIO_get_cnt(IoIFP(io)) < -1)
719 PerlIO_set_cnt(IoIFP(io),-1);
721 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
722 if (!nextargv(PL_argvgv)) /* get another fp handy */
726 return TRUE; /* normal fp, definitely end of file */
737 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
738 #ifdef ULTRIX_STDIO_BOTCH
740 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
742 return PerlIO_tell(fp);
746 if (ckWARN(WARN_UNOPENED))
747 warner(WARN_UNOPENED, "tell() on unopened file");
749 SETERRNO(EBADF,RMS$_IFI);
754 do_seek(GV *gv, Off_t pos, int whence)
759 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
760 #ifdef ULTRIX_STDIO_BOTCH
762 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
764 return PerlIO_seek(fp, pos, whence) >= 0;
768 if (ckWARN(WARN_UNOPENED))
769 warner(WARN_UNOPENED, "seek() on unopened file");
771 SETERRNO(EBADF,RMS$_IFI);
776 do_sysseek(GV *gv, Off_t pos, int whence)
781 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
782 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
785 if (ckWARN(WARN_UNOPENED))
786 warner(WARN_UNOPENED, "sysseek() on unopened file");
788 SETERRNO(EBADF,RMS$_IFI);
793 do_binmode(PerlIO *fp, int iotype, int flag)
796 croak("panic: unsetting binmode"); /* Not implemented yet */
798 #if defined(atarist) || defined(__MINT__)
799 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
804 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
805 #if defined(WIN32) && defined(__BORLANDC__)
806 /* The translation mode of the stream is maintained independent
807 * of the translation mode of the fd in the Borland RTL (heavy
808 * digging through their runtime sources reveal). User has to
809 * set the mode explicitly for the stream (though they don't
810 * document this anywhere). GSAR 97-5-24
812 PerlIO_seek(fp,0L,0);
813 ((FILE*)fp)->flags |= _F_BIN;
821 #if defined(USEMYBINMODE)
822 if (my_binmode(fp,iotype) != NULL)
832 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
833 /* code courtesy of William Kucharski */
836 I32 my_chsize(fd, length)
837 I32 fd; /* file descriptor */
838 Off_t length; /* length to set file to */
843 if (PerlLIO_fstat(fd, &filebuf) < 0)
846 if (filebuf.st_size < length) {
848 /* extend file length */
850 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
853 /* write a "0" byte */
855 if ((PerlLIO_write(fd, "", 1)) != 1)
859 /* truncate length */
864 fl.l_type = F_WRLCK; /* write lock on file space */
867 * This relies on the UNDOCUMENTED F_FREESP argument to
868 * fcntl(2), which truncates the file so that it ends at the
869 * position indicated by fl.l_start.
871 * Will minor miracles never cease?
874 if (fcntl(fd, F_FREESP, &fl) < 0)
881 #endif /* F_FREESP */
884 do_print(register SV *sv, PerlIO *fp)
889 /* assuming fp is checked earlier */
895 if (SvIOK(sv) && SvIVX(sv) != 0) {
896 PerlIO_printf(fp, PL_ofmt, (double)SvIVX(sv));
897 return !PerlIO_error(fp);
899 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
900 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
901 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
902 return !PerlIO_error(fp);
905 switch (SvTYPE(sv)) {
909 if (ckWARN(WARN_UNINITIALIZED))
910 warner(WARN_UNINITIALIZED, PL_warn_uninit);
917 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
918 return !PerlIO_error(fp);
922 tmps = SvPV(sv, len);
925 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
927 return !PerlIO_error(fp);
937 if (PL_op->op_flags & OPf_REF) {
939 tmpgv = cGVOP->op_gv;
942 if (io && IoIFP(io)) {
944 sv_setpv(PL_statname,"");
945 PL_laststype = OP_STAT;
946 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
949 if (tmpgv == PL_defgv)
950 return PL_laststatval;
951 if (ckWARN(WARN_UNOPENED))
952 warner(WARN_UNOPENED, "Stat on unopened file <%s>",
955 sv_setpv(PL_statname,"");
956 return (PL_laststatval = -1);
964 if (SvTYPE(sv) == SVt_PVGV) {
968 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
969 tmpgv = (GV*)SvRV(sv);
975 sv_setpv(PL_statname, s);
976 PL_laststype = OP_STAT;
977 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
978 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
979 warner(WARN_NEWLINE, PL_warn_nl, "stat");
980 return PL_laststatval;
990 if (PL_op->op_flags & OPf_REF) {
992 if (cGVOP->op_gv == PL_defgv) {
993 if (PL_laststype != OP_LSTAT)
994 croak("The stat preceding -l _ wasn't an lstat");
995 return PL_laststatval;
997 croak("You can't use -l on a filehandle");
1000 PL_laststype = OP_LSTAT;
1004 sv_setpv(PL_statname,SvPV(sv, n_a));
1006 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
1008 PL_laststatval = PerlLIO_stat(SvPV(sv, n_a),&PL_statcache);
1010 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n'))
1011 warner(WARN_NEWLINE, PL_warn_nl, "lstat");
1012 return PL_laststatval;
1016 do_aexec(SV *really, register SV **mark, register SV **sp)
1024 New(401,PL_Argv, sp - mark + 1, char*);
1026 while (++mark <= sp) {
1028 *a++ = SvPVx(*mark, n_a);
1033 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
1034 TAINT_ENV(); /* testing IFS here is overkill, probably */
1035 if (really && *(tmps = SvPV(really, n_a)))
1036 PerlProc_execvp(tmps,PL_Argv);
1038 PerlProc_execvp(PL_Argv[0],PL_Argv);
1039 if (ckWARN(WARN_EXEC))
1040 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1041 PL_Argv[0], Strerror(errno));
1052 PL_Argv = Null(char **);
1060 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
1069 while (*cmd && isSPACE(*cmd))
1072 /* save an extra exec if possible */
1075 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1077 s = cmd+PL_cshlen+3;
1091 if (s[-1] == '\'') {
1093 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1101 /* see if there are shell metacharacters in it */
1103 if (*cmd == '.' && isSPACE(cmd[1]))
1106 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1109 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1113 for (s = cmd; *s; s++) {
1114 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1115 if (*s == '\n' && !s[1]) {
1120 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1125 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1126 PL_Cmd = savepvn(cmd, s-cmd);
1128 for (s = PL_Cmd; *s;) {
1129 while (*s && isSPACE(*s)) s++;
1132 while (*s && !isSPACE(*s)) s++;
1138 PerlProc_execvp(PL_Argv[0],PL_Argv);
1139 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1145 if (ckWARN(WARN_EXEC))
1146 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1147 PL_Argv[0], Strerror(errno));
1154 #endif /* OS2 || WIN32 */
1157 apply(I32 type, register SV **mark, register SV **sp)
1162 register I32 tot = 0;
1165 SV **oldmark = mark;
1168 #define APPLY_TAINT_PROPER() \
1170 if (PL_tainted) { TAINT_PROPER(what); } \
1173 /* This is a first heuristic; it doesn't catch tainting magic. */
1175 while (++mark <= sp) {
1176 if (SvTAINTED(*mark)) {
1186 APPLY_TAINT_PROPER();
1189 APPLY_TAINT_PROPER();
1191 while (++mark <= sp) {
1192 char *name = SvPVx(*mark, n_a);
1193 APPLY_TAINT_PROPER();
1194 if (PerlLIO_chmod(name, val))
1202 APPLY_TAINT_PROPER();
1203 if (sp - mark > 2) {
1204 val = SvIVx(*++mark);
1205 val2 = SvIVx(*++mark);
1206 APPLY_TAINT_PROPER();
1208 while (++mark <= sp) {
1209 char *name = SvPVx(*mark, n_a);
1210 APPLY_TAINT_PROPER();
1211 if (PerlLIO_chown(name, val, val2))
1218 XXX Should we make lchown() directly available from perl?
1219 For now, we'll let Configure test for HAS_LCHOWN, but do
1220 nothing in the core.
1226 APPLY_TAINT_PROPER();
1229 s = SvPVx(*++mark, n_a);
1231 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1233 if (!(val = whichsig(s)))
1234 croak("Unrecognized signal name \"%s\"",s);
1238 APPLY_TAINT_PROPER();
1241 /* kill() doesn't do process groups (job trees?) under VMS */
1242 if (val < 0) val = -val;
1243 if (val == SIGKILL) {
1244 # include <starlet.h>
1245 /* Use native sys$delprc() to insure that target process is
1246 * deleted; supervisor-mode images don't pay attention to
1247 * CRTL's emulation of Unix-style signals and kill()
1249 while (++mark <= sp) {
1250 I32 proc = SvIVx(*mark);
1251 register unsigned long int __vmssts;
1252 APPLY_TAINT_PROPER();
1253 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1257 case SS$_NOSUCHNODE:
1258 SETERRNO(ESRCH,__vmssts);
1261 SETERRNO(EPERM,__vmssts);
1264 SETERRNO(EVMSERR,__vmssts);
1273 while (++mark <= sp) {
1274 I32 proc = SvIVx(*mark);
1275 APPLY_TAINT_PROPER();
1277 if (PerlProc_killpg(proc,val)) /* BSD */
1279 if (PerlProc_kill(-proc,val)) /* SYSV */
1285 while (++mark <= sp) {
1286 I32 proc = SvIVx(*mark);
1287 APPLY_TAINT_PROPER();
1288 if (PerlProc_kill(proc, val))
1296 APPLY_TAINT_PROPER();
1298 while (++mark <= sp) {
1299 s = SvPVx(*mark, n_a);
1300 APPLY_TAINT_PROPER();
1301 if (PL_euid || PL_unsafe) {
1305 else { /* don't let root wipe out directories without -U */
1307 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1309 if (PerlLIO_stat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1322 APPLY_TAINT_PROPER();
1323 if (sp - mark > 2) {
1324 #if defined(I_UTIME) || defined(VMS)
1325 struct utimbuf utbuf;
1333 Zero(&utbuf, sizeof utbuf, char);
1335 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1336 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1338 utbuf.actime = SvIVx(*++mark); /* time accessed */
1339 utbuf.modtime = SvIVx(*++mark); /* time modified */
1341 APPLY_TAINT_PROPER();
1343 while (++mark <= sp) {
1344 char *name = SvPVx(*mark, n_a);
1345 APPLY_TAINT_PROPER();
1346 if (PerlLIO_utime(name, &utbuf))
1357 #undef APPLY_TAINT_PROPER
1360 /* Do the permissions allow some operation? Assumes statcache already set. */
1361 #ifndef VMS /* VMS' cando is in vms.c */
1363 cando(I32 bit, I32 effective, register struct stat *statbufp)
1366 /* [Comments and code from Len Reed]
1367 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1368 * to write-protected files. The execute permission bit is set
1369 * by the Miscrosoft C library stat() function for the following:
1374 * All files and directories are readable.
1375 * Directories and special files, e.g. "CON", cannot be
1377 * [Comment by Tom Dinger -- a directory can have the write-protect
1378 * bit set in the file system, but DOS permits changes to
1379 * the directory anyway. In addition, all bets are off
1380 * here for networked software, such as Novell and
1384 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1385 * too so it will actually look into the files for magic numbers
1387 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1389 #else /* ! DOSISH */
1390 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1391 if (bit == S_IXUSR) {
1392 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1396 return TRUE; /* root reads and writes anything */
1399 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1400 if (statbufp->st_mode & bit)
1401 return TRUE; /* ok as "user" */
1403 else if (ingroup((I32)statbufp->st_gid,effective)) {
1404 if (statbufp->st_mode & bit >> 3)
1405 return TRUE; /* ok as "group" */
1407 else if (statbufp->st_mode & bit >> 6)
1408 return TRUE; /* ok as "other" */
1410 #endif /* ! DOSISH */
1415 ingroup(I32 testgid, I32 effective)
1417 if (testgid == (effective ? PL_egid : PL_gid))
1419 #ifdef HAS_GETGROUPS
1424 Groups_t gary[NGROUPS];
1427 anum = getgroups(NGROUPS,gary);
1429 if (gary[anum] == testgid)
1436 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1439 do_ipcget(I32 optype, SV **mark, SV **sp)
1445 key = (key_t)SvNVx(*++mark);
1446 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1447 flags = SvIVx(*++mark);
1453 return msgget(key, flags);
1457 return semget(key, n, flags);
1461 return shmget(key, n, flags);
1463 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1465 croak("%s not implemented", PL_op_desc[optype]);
1468 return -1; /* should never happen */
1472 do_ipcctl(I32 optype, SV **mark, SV **sp)
1477 I32 id, n, cmd, infosize, getinfo;
1480 id = SvIVx(*++mark);
1481 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1482 cmd = SvIVx(*++mark);
1485 getinfo = (cmd == IPC_STAT);
1491 if (cmd == IPC_STAT || cmd == IPC_SET)
1492 infosize = sizeof(struct msqid_ds);
1497 if (cmd == IPC_STAT || cmd == IPC_SET)
1498 infosize = sizeof(struct shmid_ds);
1503 if (cmd == IPC_STAT || cmd == IPC_SET)
1504 infosize = sizeof(struct semid_ds);
1505 else if (cmd == GETALL || cmd == SETALL)
1507 struct semid_ds semds;
1511 getinfo = (cmd == GETALL);
1512 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1514 infosize = semds.sem_nsems * sizeof(short);
1515 /* "short" is technically wrong but much more portable
1516 than guessing about u_?short(_t)? */
1520 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1522 croak("%s not implemented", PL_op_desc[optype]);
1531 SvPV_force(astr, len);
1532 a = SvGROW(astr, infosize+1);
1536 a = SvPV(astr, len);
1537 if (len != infosize)
1538 croak("Bad arg length for %s, is %lu, should be %ld",
1547 a = (char *)i; /* ouch */
1554 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1559 union semun unsemds;
1561 unsemds.buf = (struct semid_ds *)a;
1562 ret = Semctl(id, n, cmd, unsemds);
1568 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1572 if (getinfo && ret >= 0) {
1573 SvCUR_set(astr, infosize);
1574 *SvEND(astr) = '\0';
1581 do_msgsnd(SV **mark, SV **sp)
1587 I32 id, msize, flags;
1590 id = SvIVx(*++mark);
1592 flags = SvIVx(*++mark);
1593 mbuf = SvPV(mstr, len);
1594 if ((msize = len - sizeof(long)) < 0)
1595 croak("Arg too short for msgsnd");
1597 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1599 croak("msgsnd not implemented");
1604 do_msgrcv(SV **mark, SV **sp)
1611 I32 id, msize, flags, ret;
1614 id = SvIVx(*++mark);
1616 msize = SvIVx(*++mark);
1617 mtype = (long)SvIVx(*++mark);
1618 flags = SvIVx(*++mark);
1619 if (SvTHINKFIRST(mstr)) {
1620 if (SvREADONLY(mstr))
1621 croak("Can't msgrcv to readonly var");
1625 SvPV_force(mstr, len);
1626 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1629 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1631 SvCUR_set(mstr, sizeof(long)+ret);
1632 *SvEND(mstr) = '\0';
1636 croak("msgrcv not implemented");
1641 do_semop(SV **mark, SV **sp)
1650 id = SvIVx(*++mark);
1652 opbuf = SvPV(opstr, opsize);
1653 if (opsize < sizeof(struct sembuf)
1654 || (opsize % sizeof(struct sembuf)) != 0) {
1655 SETERRNO(EINVAL,LIB$_INVARG);
1659 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1661 croak("semop not implemented");
1666 do_shmio(I32 optype, SV **mark, SV **sp)
1672 I32 id, mpos, msize;
1674 struct shmid_ds shmds;
1676 id = SvIVx(*++mark);
1678 mpos = SvIVx(*++mark);
1679 msize = SvIVx(*++mark);
1681 if (shmctl(id, IPC_STAT, &shmds) == -1)
1683 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1684 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1687 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1688 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1690 if (optype == OP_SHMREAD) {
1691 SvPV_force(mstr, len);
1692 mbuf = SvGROW(mstr, msize+1);
1694 Copy(shm + mpos, mbuf, msize, char);
1695 SvCUR_set(mstr, msize);
1696 *SvEND(mstr) = '\0';
1702 mbuf = SvPV(mstr, len);
1703 if ((n = len) > msize)
1705 Copy(mbuf, shm + mpos, n, char);
1707 memzero(shm + mpos + n, msize - n);
1711 croak("shm I/O not implemented");
1715 #endif /* SYSV IPC */