3 * Copyright (c) 1991-1999, 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)
29 # ifndef HAS_SHMAT_PROTOTYPE
30 extern Shmat_t shmat _((int, char *, int));
36 # if defined(_MSC_VER) || defined(__MINGW32__)
37 # include <sys/utime.h>
50 # define OPEN_EXCL O_EXCL
55 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
59 /* XXX If this causes problems, set i_unistd=undef in the hint file. */
64 #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
65 # include <sys/socket.h>
69 # include <net/errno.h>
74 /* Put this after #includes because <unistd.h> defines _XOPEN_*. */
76 # if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
77 # define Sock_size_t Size_t
79 # define Sock_size_t int
84 do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawperm, PerlIO *supplied_fp)
86 register IO *io = GvIOn(gv);
87 PerlIO *saveifp = Nullfp;
88 PerlIO *saveofp = Nullfp;
94 bool was_fdopen = FALSE;
96 PL_forkprocess = 1; /* assume true if no fork */
99 fd = PerlIO_fileno(IoIFP(io));
100 if (IoTYPE(io) == '-')
102 else if (fd <= PL_maxsysfd) {
105 savetype = IoTYPE(io);
108 else if (IoTYPE(io) == '|')
109 result = PerlProc_pclose(IoIFP(io));
110 else if (IoIFP(io) != IoOFP(io)) {
112 result = PerlIO_close(IoOFP(io));
113 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
116 result = PerlIO_close(IoIFP(io));
119 result = PerlIO_close(IoIFP(io));
120 if (result == EOF && fd > PL_maxsysfd)
121 PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n",
123 IoOFP(io) = IoIFP(io) = Nullfp;
128 #define O_ACCMODE 3 /* Assume traditional implementation */
130 switch (result = rawmode & O_ACCMODE) {
143 writing = (result > 0);
144 fd = PerlLIO_open3(name, rawmode, rawperm);
150 if (result == O_RDONLY)
153 else if (rawmode & O_APPEND)
154 fpmode = (result == O_WRONLY) ? "a" : "a+";
157 fpmode = (result == O_WRONLY) ? "w" : "r+";
158 fp = PerlIO_fdopen(fd, fpmode);
165 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
168 myname = savepvn(name, len);
171 while (len && isSPACE(name[len-1]))
174 mode[0] = mode[1] = mode[2] = '\0';
176 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
184 for (name++; isSPACE(*name); name++) ;
187 TAINT_PROPER("piped open");
188 if (name[strlen(name)-1] == '|') {
189 name[strlen(name)-1] = '\0' ;
191 warn("Can't do bidirectional pipe");
193 fp = PerlProc_popen(name,"w");
196 else if (*name == '>') {
197 TAINT_PROPER("open");
200 mode[0] = IoTYPE(io) = 'a';
215 if (!*name && supplied_fp)
219 for (; isSPACE(*name); name++) ;
224 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
228 SETERRNO(EINVAL,SS$_IVCHAN);
233 fd = PerlIO_fileno(IoIFP(thatio));
234 if (IoTYPE(thatio) == 's')
241 fd = PerlLIO_dup(fd);
244 if (!(fp = PerlIO_fdopen(fd,mode))) {
252 for (; isSPACE(*name); name++) ;
253 if (strEQ(name,"-")) {
254 fp = PerlIO_stdout();
258 fp = PerlIO_open(name,mode);
262 else if (*name == '<') {
264 for (name++; isSPACE(*name); name++) ;
268 if (strEQ(name,"-")) {
273 fp = PerlIO_open(name,mode);
275 else if (len > 1 && name[len-1] == '|') {
277 while (len && isSPACE(name[len-1]))
280 for (; isSPACE(*name); name++) ;
283 TAINT_PROPER("piped open");
284 fp = PerlProc_popen(name,"r");
290 for (; isSPACE(*name); name++) ;
291 if (strEQ(name,"-")) {
296 fp = PerlIO_open(name,"r");
300 if (PL_dowarn && IoTYPE(io) == '<' && strchr(name, '\n'))
301 warn(warn_nl, "open");
305 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
307 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
308 (void)PerlIO_close(fp);
311 if (S_ISSOCK(PL_statbuf.st_mode))
312 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
316 !(PL_statbuf.st_mode & S_IFMT)
322 Sock_size_t buflen = sizeof tmpbuf;
323 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
325 || errno != ENOTSOCK)
326 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
327 /* but some return 0 for streams too, sigh */
331 if (saveifp) { /* must use old fp? */
332 fd = PerlIO_fileno(saveifp);
334 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
335 if (saveofp != saveifp) { /* was a socket? */
336 PerlIO_close(saveofp);
341 if (fd != PerlIO_fileno(fp)) {
345 PerlLIO_dup2(PerlIO_fileno(fp), fd);
346 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
347 (void)SvUPGRADE(sv, SVt_IV);
350 sv = *av_fetch(PL_fdpid,fd,TRUE);
351 (void)SvUPGRADE(sv, SVt_IV);
360 #if defined(HAS_FCNTL) && defined(F_SETFD)
362 int save_errno = errno;
363 fd = PerlIO_fileno(fp);
364 fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */
371 if (IoTYPE(io) == 's'
372 || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) {
373 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
387 IoTYPE(io) = savetype;
392 nextargv(register GV *gv)
395 #ifndef FLEXFILENAMES
403 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
404 if (PL_filemode & (S_ISUID|S_ISGID)) {
405 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
407 (void)fchmod(PL_lastfd,PL_filemode);
409 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
413 while (av_len(GvAV(gv)) >= 0) {
416 sv = av_shift(GvAV(gv));
418 sv_setsv(GvSV(gv),sv);
419 SvSETMAGIC(GvSV(gv));
420 PL_oldname = SvPVx(GvSV(gv), oldlen);
421 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
423 TAINT_PROPER("inplace open");
424 if (oldlen == 1 && *PL_oldname == '-') {
425 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
426 return IoIFP(GvIOp(gv));
428 #ifndef FLEXFILENAMES
429 filedev = PL_statbuf.st_dev;
430 fileino = PL_statbuf.st_ino;
432 PL_filemode = PL_statbuf.st_mode;
433 fileuid = PL_statbuf.st_uid;
434 filegid = PL_statbuf.st_gid;
435 if (!S_ISREG(PL_filemode)) {
436 warn("Can't do inplace edit: %s is not a regular file",
442 char *star = strchr(PL_inplace, '*');
444 char *begin = PL_inplace;
445 sv_setpvn(sv, "", 0);
447 sv_catpvn(sv, begin, star - begin);
448 sv_catpvn(sv, PL_oldname, oldlen);
450 } while ((star = strchr(begin, '*')));
455 sv_catpv(sv,PL_inplace);
457 #ifndef FLEXFILENAMES
458 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
459 && PL_statbuf.st_dev == filedev
460 && PL_statbuf.st_ino == fileino
462 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
465 warn("Can't do inplace edit: %s would not be uniq",
473 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
474 warn("Can't rename %s to %s: %s, skipping file",
475 PL_oldname, SvPVX(sv), Strerror(errno) );
481 (void)PerlLIO_unlink(SvPVX(sv));
482 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
483 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
486 (void)UNLINK(SvPVX(sv));
487 if (link(PL_oldname,SvPVX(sv)) < 0) {
488 warn("Can't rename %s to %s: %s, skipping file",
489 PL_oldname, SvPVX(sv), Strerror(errno) );
493 (void)UNLINK(PL_oldname);
497 #if !defined(DOSISH) && !defined(AMIGAOS)
498 # ifndef VMS /* Don't delete; use automatic file versioning */
499 if (UNLINK(PL_oldname) < 0) {
500 warn("Can't remove %s: %s, skipping file",
501 PL_oldname, Strerror(errno) );
507 croak("Can't do inplace edit without backup");
511 sv_setpvn(sv,">",!PL_inplace);
512 sv_catpvn(sv,PL_oldname,oldlen);
513 SETERRNO(0,0); /* in case sprintf set errno */
515 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
516 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) {
518 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
519 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
521 warn("Can't do inplace edit on %s: %s",
522 PL_oldname, Strerror(errno) );
526 setdefout(PL_argvoutgv);
527 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
528 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
530 (void)fchmod(PL_lastfd,PL_filemode);
532 # if !(defined(WIN32) && defined(__BORLANDC__))
533 /* Borland runtime creates a readonly file! */
534 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
537 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
539 (void)fchown(PL_lastfd,fileuid,filegid);
542 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
547 return IoIFP(GvIOp(gv));
550 PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
551 SvPV(sv, oldlen), Strerror(errno));
554 (void)do_close(PL_argvoutgv,FALSE);
555 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
562 do_pipe(SV *sv, GV *rgv, GV *wgv)
581 if (PerlProc_pipe(fd) < 0)
583 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
584 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
585 IoIFP(wstio) = IoOFP(wstio);
588 if (!IoIFP(rstio) || !IoOFP(wstio)) {
589 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
590 else PerlLIO_close(fd[0]);
591 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
592 else PerlLIO_close(fd[1]);
596 sv_setsv(sv,&PL_sv_yes);
600 sv_setsv(sv,&PL_sv_undef);
605 /* explicit renamed to avoid C++ conflict -- kja */
607 do_close(GV *gv, bool not_implicit)
614 if (!gv || SvTYPE(gv) != SVt_PVGV) {
616 SETERRNO(EBADF,SS$_IVCHAN);
620 if (!io) { /* never opened */
623 warn("Close on unopened file <%s>",GvENAME(gv));
624 SETERRNO(EBADF,SS$_IVCHAN);
628 retval = io_close(io);
632 IoLINES_LEFT(io) = IoPAGE_LEN(io);
645 if (IoTYPE(io) == '|') {
646 status = PerlProc_pclose(IoIFP(io));
647 STATUS_NATIVE_SET(status);
648 retval = (STATUS_POSIX == 0);
650 else if (IoTYPE(io) == '-')
653 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
654 retval = (PerlIO_close(IoOFP(io)) != EOF);
655 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
658 retval = (PerlIO_close(IoIFP(io)) != EOF);
660 IoOFP(io) = IoIFP(io) = Nullfp;
663 SETERRNO(EBADF,SS$_IVCHAN);
683 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
684 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
685 return FALSE; /* this is the most usual case */
688 ch = PerlIO_getc(IoIFP(io));
690 (void)PerlIO_ungetc(IoIFP(io),ch);
693 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
694 if (PerlIO_get_cnt(IoIFP(io)) < -1)
695 PerlIO_set_cnt(IoIFP(io),-1);
697 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
698 if (!nextargv(PL_argvgv)) /* get another fp handy */
702 return TRUE; /* normal fp, definitely end of file */
713 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
714 #ifdef ULTRIX_STDIO_BOTCH
716 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
718 return PerlIO_tell(fp);
721 warn("tell() on unopened file");
722 SETERRNO(EBADF,RMS$_IFI);
727 do_seek(GV *gv, long int pos, int whence)
732 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
733 #ifdef ULTRIX_STDIO_BOTCH
735 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
737 return PerlIO_seek(fp, pos, whence) >= 0;
740 warn("seek() on unopened file");
741 SETERRNO(EBADF,RMS$_IFI);
746 do_sysseek(GV *gv, long int pos, int whence)
751 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
752 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
754 warn("sysseek() on unopened file");
755 SETERRNO(EBADF,RMS$_IFI);
760 do_binmode(PerlIO *fp, int iotype, int flag)
763 croak("panic: unsetting binmode"); /* Not implemented yet */
765 #if defined(atarist) || defined(__MINT__)
766 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
771 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
772 #if defined(WIN32) && defined(__BORLANDC__)
773 /* The translation mode of the stream is maintained independent
774 * of the translation mode of the fd in the Borland RTL (heavy
775 * digging through their runtime sources reveal). User has to
776 * set the mode explicitly for the stream (though they don't
777 * document this anywhere). GSAR 97-5-24
779 PerlIO_seek(fp,0L,0);
780 ((FILE*)fp)->flags |= _F_BIN;
788 #if defined(USEMYBINMODE)
789 if (my_binmode(fp,iotype) != NULL)
799 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
800 /* code courtesy of William Kucharski */
803 I32 my_chsize(fd, length)
804 I32 fd; /* file descriptor */
805 Off_t length; /* length to set file to */
810 if (PerlLIO_fstat(fd, &filebuf) < 0)
813 if (filebuf.st_size < length) {
815 /* extend file length */
817 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
820 /* write a "0" byte */
822 if ((PerlLIO_write(fd, "", 1)) != 1)
826 /* truncate length */
831 fl.l_type = F_WRLCK; /* write lock on file space */
834 * This relies on the UNDOCUMENTED F_FREESP argument to
835 * fcntl(2), which truncates the file so that it ends at the
836 * position indicated by fl.l_start.
838 * Will minor miracles never cease?
841 if (fcntl(fd, F_FREESP, &fl) < 0)
848 #endif /* F_FREESP */
851 do_print(register SV *sv, PerlIO *fp)
856 /* assuming fp is checked earlier */
862 if (SvIOK(sv) && SvIVX(sv) != 0) {
863 PerlIO_printf(fp, PL_ofmt, (double)SvIVX(sv));
864 return !PerlIO_error(fp);
866 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
867 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
868 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
869 return !PerlIO_error(fp);
872 switch (SvTYPE(sv)) {
881 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
882 return !PerlIO_error(fp);
886 tmps = SvPV(sv, len);
889 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
891 return !PerlIO_error(fp);
901 if (PL_op->op_flags & OPf_REF) {
903 tmpgv = cGVOP->op_gv;
906 if (io && IoIFP(io)) {
908 sv_setpv(PL_statname,"");
909 PL_laststype = OP_STAT;
910 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
913 if (tmpgv == PL_defgv)
914 return PL_laststatval;
916 warn("Stat on unopened file <%s>",
919 sv_setpv(PL_statname,"");
920 return (PL_laststatval = -1);
928 if (SvTYPE(sv) == SVt_PVGV) {
932 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
933 tmpgv = (GV*)SvRV(sv);
939 sv_setpv(PL_statname, s);
940 PL_laststype = OP_STAT;
941 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
942 if (PL_laststatval < 0 && PL_dowarn && strchr(s, '\n'))
943 warn(warn_nl, "stat");
944 return PL_laststatval;
954 if (PL_op->op_flags & OPf_REF) {
956 if (cGVOP->op_gv == PL_defgv) {
957 if (PL_laststype != OP_LSTAT)
958 croak("The stat preceding -l _ wasn't an lstat");
959 return PL_laststatval;
961 croak("You can't use -l on a filehandle");
964 PL_laststype = OP_LSTAT;
968 sv_setpv(PL_statname,SvPV(sv, n_a));
970 PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache);
972 PL_laststatval = PerlLIO_stat(SvPV(sv, n_a),&PL_statcache);
974 if (PL_laststatval < 0 && PL_dowarn && strchr(SvPV(sv, n_a), '\n'))
975 warn(warn_nl, "lstat");
976 return PL_laststatval;
980 do_aexec(SV *really, register SV **mark, register SV **sp)
988 New(401,PL_Argv, sp - mark + 1, char*);
990 while (++mark <= sp) {
992 *a++ = SvPVx(*mark, n_a);
997 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
998 TAINT_ENV(); /* testing IFS here is overkill, probably */
999 if (really && *(tmps = SvPV(really, n_a)))
1000 PerlProc_execvp(tmps,PL_Argv);
1002 PerlProc_execvp(PL_Argv[0],PL_Argv);
1004 warn("Can't exec \"%s\": %s", PL_Argv[0], Strerror(errno));
1015 PL_Argv = Null(char **);
1023 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
1032 while (*cmd && isSPACE(*cmd))
1035 /* save an extra exec if possible */
1038 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1040 s = cmd+PL_cshlen+3;
1054 if (s[-1] == '\'') {
1056 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1064 /* see if there are shell metacharacters in it */
1066 if (*cmd == '.' && isSPACE(cmd[1]))
1069 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1072 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1076 for (s = cmd; *s; s++) {
1077 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1078 if (*s == '\n' && !s[1]) {
1083 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1088 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1089 PL_Cmd = savepvn(cmd, s-cmd);
1091 for (s = PL_Cmd; *s;) {
1092 while (*s && isSPACE(*s)) s++;
1095 while (*s && !isSPACE(*s)) s++;
1101 PerlProc_execvp(PL_Argv[0],PL_Argv);
1102 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1107 warn("Can't exec \"%s\": %s", PL_Argv[0], Strerror(errno));
1113 #endif /* OS2 || WIN32 */
1116 apply(I32 type, register SV **mark, register SV **sp)
1121 register I32 tot = 0;
1124 SV **oldmark = mark;
1127 #define APPLY_TAINT_PROPER() \
1129 if (PL_tainted) { TAINT_PROPER(what); } \
1132 /* This is a first heuristic; it doesn't catch tainting magic. */
1134 while (++mark <= sp) {
1135 if (SvTAINTED(*mark)) {
1145 APPLY_TAINT_PROPER();
1148 APPLY_TAINT_PROPER();
1150 while (++mark <= sp) {
1151 char *name = SvPVx(*mark, n_a);
1152 APPLY_TAINT_PROPER();
1153 if (PerlLIO_chmod(name, val))
1161 APPLY_TAINT_PROPER();
1162 if (sp - mark > 2) {
1163 val = SvIVx(*++mark);
1164 val2 = SvIVx(*++mark);
1165 APPLY_TAINT_PROPER();
1167 while (++mark <= sp) {
1168 char *name = SvPVx(*mark, n_a);
1169 APPLY_TAINT_PROPER();
1170 if (PerlLIO_chown(name, val, val2))
1177 XXX Should we make lchown() directly available from perl?
1178 For now, we'll let Configure test for HAS_LCHOWN, but do
1179 nothing in the core.
1185 APPLY_TAINT_PROPER();
1188 s = SvPVx(*++mark, n_a);
1190 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1192 if (!(val = whichsig(s)))
1193 croak("Unrecognized signal name \"%s\"",s);
1197 APPLY_TAINT_PROPER();
1200 /* kill() doesn't do process groups (job trees?) under VMS */
1201 if (val < 0) val = -val;
1202 if (val == SIGKILL) {
1203 # include <starlet.h>
1204 /* Use native sys$delprc() to insure that target process is
1205 * deleted; supervisor-mode images don't pay attention to
1206 * CRTL's emulation of Unix-style signals and kill()
1208 while (++mark <= sp) {
1209 I32 proc = SvIVx(*mark);
1210 register unsigned long int __vmssts;
1211 APPLY_TAINT_PROPER();
1212 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1216 case SS$_NOSUCHNODE:
1217 SETERRNO(ESRCH,__vmssts);
1220 SETERRNO(EPERM,__vmssts);
1223 SETERRNO(EVMSERR,__vmssts);
1232 while (++mark <= sp) {
1233 I32 proc = SvIVx(*mark);
1234 APPLY_TAINT_PROPER();
1236 if (PerlProc_killpg(proc,val)) /* BSD */
1238 if (PerlProc_kill(-proc,val)) /* SYSV */
1244 while (++mark <= sp) {
1245 I32 proc = SvIVx(*mark);
1246 APPLY_TAINT_PROPER();
1247 if (PerlProc_kill(proc, val))
1255 APPLY_TAINT_PROPER();
1257 while (++mark <= sp) {
1258 s = SvPVx(*mark, n_a);
1259 APPLY_TAINT_PROPER();
1260 if (PL_euid || PL_unsafe) {
1264 else { /* don't let root wipe out directories without -U */
1266 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1268 if (PerlLIO_stat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1281 APPLY_TAINT_PROPER();
1282 if (sp - mark > 2) {
1283 #if defined(I_UTIME) || defined(VMS)
1284 struct utimbuf utbuf;
1292 Zero(&utbuf, sizeof utbuf, char);
1294 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1295 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1297 utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */
1298 utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */
1300 APPLY_TAINT_PROPER();
1302 while (++mark <= sp) {
1303 char *name = SvPVx(*mark, n_a);
1304 APPLY_TAINT_PROPER();
1305 if (PerlLIO_utime(name, &utbuf))
1316 #undef APPLY_TAINT_PROPER
1319 /* Do the permissions allow some operation? Assumes statcache already set. */
1320 #ifndef VMS /* VMS' cando is in vms.c */
1322 cando(I32 bit, I32 effective, register struct stat *statbufp)
1325 /* [Comments and code from Len Reed]
1326 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1327 * to write-protected files. The execute permission bit is set
1328 * by the Miscrosoft C library stat() function for the following:
1333 * All files and directories are readable.
1334 * Directories and special files, e.g. "CON", cannot be
1336 * [Comment by Tom Dinger -- a directory can have the write-protect
1337 * bit set in the file system, but DOS permits changes to
1338 * the directory anyway. In addition, all bets are off
1339 * here for networked software, such as Novell and
1343 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1344 * too so it will actually look into the files for magic numbers
1346 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1348 #else /* ! DOSISH */
1349 if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */
1350 if (bit == S_IXUSR) {
1351 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1355 return TRUE; /* root reads and writes anything */
1358 if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) {
1359 if (statbufp->st_mode & bit)
1360 return TRUE; /* ok as "user" */
1362 else if (ingroup((I32)statbufp->st_gid,effective)) {
1363 if (statbufp->st_mode & bit >> 3)
1364 return TRUE; /* ok as "group" */
1366 else if (statbufp->st_mode & bit >> 6)
1367 return TRUE; /* ok as "other" */
1369 #endif /* ! DOSISH */
1374 ingroup(I32 testgid, I32 effective)
1376 if (testgid == (effective ? PL_egid : PL_gid))
1378 #ifdef HAS_GETGROUPS
1383 Groups_t gary[NGROUPS];
1386 anum = getgroups(NGROUPS,gary);
1388 if (gary[anum] == testgid)
1395 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1398 do_ipcget(I32 optype, SV **mark, SV **sp)
1404 key = (key_t)SvNVx(*++mark);
1405 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1406 flags = SvIVx(*++mark);
1412 return msgget(key, flags);
1416 return semget(key, n, flags);
1420 return shmget(key, n, flags);
1422 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1424 croak("%s not implemented", op_desc[optype]);
1427 return -1; /* should never happen */
1431 do_ipcctl(I32 optype, SV **mark, SV **sp)
1436 I32 id, n, cmd, infosize, getinfo;
1439 id = SvIVx(*++mark);
1440 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1441 cmd = SvIVx(*++mark);
1444 getinfo = (cmd == IPC_STAT);
1450 if (cmd == IPC_STAT || cmd == IPC_SET)
1451 infosize = sizeof(struct msqid_ds);
1456 if (cmd == IPC_STAT || cmd == IPC_SET)
1457 infosize = sizeof(struct shmid_ds);
1462 if (cmd == IPC_STAT || cmd == IPC_SET)
1463 infosize = sizeof(struct semid_ds);
1464 else if (cmd == GETALL || cmd == SETALL)
1466 struct semid_ds semds;
1470 getinfo = (cmd == GETALL);
1471 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1473 infosize = semds.sem_nsems * sizeof(short);
1474 /* "short" is technically wrong but much more portable
1475 than guessing about u_?short(_t)? */
1479 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1481 croak("%s not implemented", op_desc[optype]);
1490 SvPV_force(astr, len);
1491 a = SvGROW(astr, infosize+1);
1495 a = SvPV(astr, len);
1496 if (len != infosize)
1497 croak("Bad arg length for %s, is %lu, should be %ld",
1498 op_desc[optype], (unsigned long)len, (long)infosize);
1504 a = (char *)i; /* ouch */
1511 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1516 union semun unsemds;
1518 unsemds.buf = (struct semid_ds *)a;
1519 ret = Semctl(id, n, cmd, unsemds);
1525 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1529 if (getinfo && ret >= 0) {
1530 SvCUR_set(astr, infosize);
1531 *SvEND(astr) = '\0';
1538 do_msgsnd(SV **mark, SV **sp)
1544 I32 id, msize, flags;
1547 id = SvIVx(*++mark);
1549 flags = SvIVx(*++mark);
1550 mbuf = SvPV(mstr, len);
1551 if ((msize = len - sizeof(long)) < 0)
1552 croak("Arg too short for msgsnd");
1554 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1556 croak("msgsnd not implemented");
1561 do_msgrcv(SV **mark, SV **sp)
1568 I32 id, msize, flags, ret;
1571 id = SvIVx(*++mark);
1573 msize = SvIVx(*++mark);
1574 mtype = (long)SvIVx(*++mark);
1575 flags = SvIVx(*++mark);
1576 if (SvTHINKFIRST(mstr)) {
1577 if (SvREADONLY(mstr))
1578 croak("Can't msgrcv to readonly var");
1582 SvPV_force(mstr, len);
1583 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1586 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1588 SvCUR_set(mstr, sizeof(long)+ret);
1589 *SvEND(mstr) = '\0';
1593 croak("msgrcv not implemented");
1598 do_semop(SV **mark, SV **sp)
1607 id = SvIVx(*++mark);
1609 opbuf = SvPV(opstr, opsize);
1610 if (opsize < sizeof(struct sembuf)
1611 || (opsize % sizeof(struct sembuf)) != 0) {
1612 SETERRNO(EINVAL,LIB$_INVARG);
1616 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1618 croak("semop not implemented");
1623 do_shmio(I32 optype, SV **mark, SV **sp)
1629 I32 id, mpos, msize;
1631 struct shmid_ds shmds;
1633 id = SvIVx(*++mark);
1635 mpos = SvIVx(*++mark);
1636 msize = SvIVx(*++mark);
1638 if (shmctl(id, IPC_STAT, &shmds) == -1)
1640 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1641 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1644 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1645 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1647 if (optype == OP_SHMREAD) {
1648 SvPV_force(mstr, len);
1649 mbuf = SvGROW(mstr, msize+1);
1651 Copy(shm + mpos, mbuf, msize, char);
1652 SvCUR_set(mstr, msize);
1653 *SvEND(mstr) = '\0';
1659 mbuf = SvPV(mstr, len);
1660 if ((n = len) > msize)
1662 Copy(mbuf, shm + mpos, n, char);
1664 memzero(shm + mpos + n, msize - n);
1668 croak("shm I/O not implemented");
1672 #endif /* SYSV IPC */