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;
129 #define O_ACCMODE 3 /* Assume traditional implementation */
131 switch (result = rawmode & O_ACCMODE) {
144 writing = (result > 0);
145 fd = PerlLIO_open3(name, rawmode, rawperm);
151 if (result == O_RDONLY)
154 else if (rawmode & O_APPEND)
155 fpmode = (result == O_WRONLY) ? "a" : "a+";
158 fpmode = (result == O_WRONLY) ? "w" : "r+";
159 fp = PerlIO_fdopen(fd, fpmode);
166 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
169 myname = savepvn(name, len);
172 while (len && isSPACE(name[len-1]))
175 mode[0] = mode[1] = mode[2] = '\0';
177 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
185 for (name++; isSPACE(*name); name++) ;
188 TAINT_PROPER("piped open");
189 if (name[strlen(name)-1] == '|') {
190 name[strlen(name)-1] = '\0' ;
191 if (ckWARN(WARN_PIPE))
192 warner(WARN_PIPE, "Can't do bidirectional pipe");
194 fp = PerlProc_popen(name,"w");
197 else if (*name == '>') {
198 TAINT_PROPER("open");
201 mode[0] = IoTYPE(io) = 'a';
216 if (!*name && supplied_fp)
220 for (; isSPACE(*name); name++) ;
225 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
229 SETERRNO(EINVAL,SS$_IVCHAN);
234 fd = PerlIO_fileno(IoIFP(thatio));
235 if (IoTYPE(thatio) == 's')
242 fd = PerlLIO_dup(fd);
245 if (!(fp = PerlIO_fdopen(fd,mode))) {
253 for (; isSPACE(*name); name++) ;
254 if (strEQ(name,"-")) {
255 fp = PerlIO_stdout();
259 fp = PerlIO_open(name,mode);
263 else if (*name == '<') {
265 for (name++; isSPACE(*name); name++) ;
269 if (strEQ(name,"-")) {
274 fp = PerlIO_open(name,mode);
276 else if (len > 1 && name[len-1] == '|') {
278 while (len && isSPACE(name[len-1]))
281 for (; isSPACE(*name); name++) ;
284 TAINT_PROPER("piped open");
285 fp = PerlProc_popen(name,"r");
291 for (; isSPACE(*name); name++) ;
292 if (strEQ(name,"-")) {
297 fp = PerlIO_open(name,"r");
301 if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n'))
302 warner(WARN_NEWLINE, warn_nl, "open");
306 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
308 if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
309 (void)PerlIO_close(fp);
312 if (S_ISSOCK(PL_statbuf.st_mode))
313 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
317 !(PL_statbuf.st_mode & S_IFMT)
323 Sock_size_t buflen = sizeof tmpbuf;
324 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
326 || errno != ENOTSOCK)
327 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
328 /* but some return 0 for streams too, sigh */
332 if (saveifp) { /* must use old fp? */
333 fd = PerlIO_fileno(saveifp);
335 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
336 if (saveofp != saveifp) { /* was a socket? */
337 PerlIO_close(saveofp);
342 if (fd != PerlIO_fileno(fp)) {
346 PerlLIO_dup2(PerlIO_fileno(fp), fd);
347 sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
348 (void)SvUPGRADE(sv, SVt_IV);
351 sv = *av_fetch(PL_fdpid,fd,TRUE);
352 (void)SvUPGRADE(sv, SVt_IV);
361 #if defined(HAS_FCNTL) && defined(F_SETFD)
362 fd = PerlIO_fileno(fp);
363 fcntl(fd,F_SETFD,fd > PL_maxsysfd);
368 if (IoTYPE(io) == 's'
369 || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) {
370 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
384 IoTYPE(io) = savetype;
389 nextargv(register GV *gv)
392 #ifndef FLEXFILENAMES
400 PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
401 if (PL_filemode & (S_ISUID|S_ISGID)) {
402 PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */
404 (void)fchmod(PL_lastfd,PL_filemode);
406 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
410 while (av_len(GvAV(gv)) >= 0) {
413 sv = av_shift(GvAV(gv));
415 sv_setsv(GvSV(gv),sv);
416 SvSETMAGIC(GvSV(gv));
417 PL_oldname = SvPVx(GvSV(gv), oldlen);
418 if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) {
420 TAINT_PROPER("inplace open");
421 if (oldlen == 1 && *PL_oldname == '-') {
422 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
423 return IoIFP(GvIOp(gv));
425 #ifndef FLEXFILENAMES
426 filedev = PL_statbuf.st_dev;
427 fileino = PL_statbuf.st_ino;
429 PL_filemode = PL_statbuf.st_mode;
430 fileuid = PL_statbuf.st_uid;
431 filegid = PL_statbuf.st_gid;
432 if (!S_ISREG(PL_filemode)) {
433 warn("Can't do inplace edit: %s is not a regular file",
439 char *star = strchr(PL_inplace, '*');
441 char *begin = PL_inplace;
442 sv_setpvn(sv, "", 0);
444 sv_catpvn(sv, begin, star - begin);
445 sv_catpvn(sv, PL_oldname, oldlen);
447 } while ((star = strchr(begin, '*')));
452 sv_catpv(sv,PL_inplace);
454 #ifndef FLEXFILENAMES
455 if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0
456 && PL_statbuf.st_dev == filedev
457 && PL_statbuf.st_ino == fileino
459 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
462 warn("Can't do inplace edit: %s would not be uniq",
470 if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) {
471 warn("Can't rename %s to %s: %s, skipping file",
472 PL_oldname, SvPVX(sv), Strerror(errno) );
478 (void)PerlLIO_unlink(SvPVX(sv));
479 (void)PerlLIO_rename(PL_oldname,SvPVX(sv));
480 do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp);
483 (void)UNLINK(SvPVX(sv));
484 if (link(PL_oldname,SvPVX(sv)) < 0) {
485 warn("Can't rename %s to %s: %s, skipping file",
486 PL_oldname, SvPVX(sv), Strerror(errno) );
490 (void)UNLINK(PL_oldname);
494 #if !defined(DOSISH) && !defined(AMIGAOS)
495 # ifndef VMS /* Don't delete; use automatic file versioning */
496 if (UNLINK(PL_oldname) < 0) {
497 warn("Can't remove %s: %s, skipping file",
498 PL_oldname, Strerror(errno) );
504 croak("Can't do inplace edit without backup");
508 sv_setpvn(sv,">",!PL_inplace);
509 sv_catpvn(sv,PL_oldname,oldlen);
510 SETERRNO(0,0); /* in case sprintf set errno */
512 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
513 O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) {
515 if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,
516 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
518 warn("Can't do inplace edit on %s: %s",
519 PL_oldname, Strerror(errno) );
523 setdefout(PL_argvoutgv);
524 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
525 (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
527 (void)fchmod(PL_lastfd,PL_filemode);
529 # if !(defined(WIN32) && defined(__BORLANDC__))
530 /* Borland runtime creates a readonly file! */
531 (void)PerlLIO_chmod(PL_oldname,PL_filemode);
534 if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
536 (void)fchown(PL_lastfd,fileuid,filegid);
539 (void)PerlLIO_chown(PL_oldname,fileuid,filegid);
544 return IoIFP(GvIOp(gv));
547 PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
548 SvPV(sv, PL_na), Strerror(errno));
551 (void)do_close(PL_argvoutgv,FALSE);
552 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
559 do_pipe(SV *sv, GV *rgv, GV *wgv)
578 if (PerlProc_pipe(fd) < 0)
580 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
581 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
582 IoIFP(wstio) = IoOFP(wstio);
585 if (!IoIFP(rstio) || !IoOFP(wstio)) {
586 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
587 else PerlLIO_close(fd[0]);
588 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
589 else PerlLIO_close(fd[1]);
593 sv_setsv(sv,&PL_sv_yes);
597 sv_setsv(sv,&PL_sv_undef);
602 /* explicit renamed to avoid C++ conflict -- kja */
604 do_close(GV *gv, bool not_implicit)
611 if (!gv || SvTYPE(gv) != SVt_PVGV) {
613 SETERRNO(EBADF,SS$_IVCHAN);
617 if (!io) { /* never opened */
619 if (ckWARN(WARN_UNOPENED))
620 warner(WARN_UNOPENED,
621 "Close on unopened file <%s>",GvENAME(gv));
622 SETERRNO(EBADF,SS$_IVCHAN);
626 retval = io_close(io);
630 IoLINES_LEFT(io) = IoPAGE_LEN(io);
643 if (IoTYPE(io) == '|') {
644 status = PerlProc_pclose(IoIFP(io));
645 STATUS_NATIVE_SET(status);
646 retval = (STATUS_POSIX == 0);
648 else if (IoTYPE(io) == '-')
651 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
652 retval = (PerlIO_close(IoOFP(io)) != EOF);
653 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
656 retval = (PerlIO_close(IoIFP(io)) != EOF);
658 IoOFP(io) = IoIFP(io) = Nullfp;
661 SETERRNO(EBADF,SS$_IVCHAN);
681 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
682 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
683 return FALSE; /* this is the most usual case */
686 ch = PerlIO_getc(IoIFP(io));
688 (void)PerlIO_ungetc(IoIFP(io),ch);
691 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
692 if (PerlIO_get_cnt(IoIFP(io)) < -1)
693 PerlIO_set_cnt(IoIFP(io),-1);
695 if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
696 if (!nextargv(PL_argvgv)) /* get another fp handy */
700 return TRUE; /* normal fp, definitely end of file */
711 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
712 #ifdef ULTRIX_STDIO_BOTCH
714 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
716 return PerlIO_tell(fp);
718 if (ckWARN(WARN_UNOPENED))
719 warner(WARN_UNOPENED, "tell() on unopened file");
720 SETERRNO(EBADF,RMS$_IFI);
725 do_seek(GV *gv, long int pos, int whence)
730 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
731 #ifdef ULTRIX_STDIO_BOTCH
733 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
735 return PerlIO_seek(fp, pos, whence) >= 0;
737 if (ckWARN(WARN_UNOPENED))
738 warner(WARN_UNOPENED, "seek() on unopened file");
739 SETERRNO(EBADF,RMS$_IFI);
744 do_sysseek(GV *gv, long int pos, int whence)
749 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
750 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
751 if (ckWARN(WARN_UNOPENED))
752 warner(WARN_UNOPENED, "sysseek() on unopened file");
753 SETERRNO(EBADF,RMS$_IFI);
758 do_binmode(PerlIO *fp, int iotype, int flag)
761 croak("panic: unsetting binmode"); /* Not implemented yet */
764 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
769 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
770 #if defined(WIN32) && defined(__BORLANDC__)
771 /* The translation mode of the stream is maintained independent
772 * of the translation mode of the fd in the Borland RTL (heavy
773 * digging through their runtime sources reveal). User has to
774 * set the mode explicitly for the stream (though they don't
775 * document this anywhere). GSAR 97-5-24
777 PerlIO_seek(fp,0L,0);
778 ((FILE*)fp)->flags |= _F_BIN;
786 #if defined(USEMYBINMODE)
787 if (my_binmode(fp,iotype) != NULL)
797 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
798 /* code courtesy of William Kucharski */
801 I32 my_chsize(fd, length)
802 I32 fd; /* file descriptor */
803 Off_t length; /* length to set file to */
808 if (PerlLIO_fstat(fd, &filebuf) < 0)
811 if (filebuf.st_size < length) {
813 /* extend file length */
815 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
818 /* write a "0" byte */
820 if ((PerlLIO_write(fd, "", 1)) != 1)
824 /* truncate length */
829 fl.l_type = F_WRLCK; /* write lock on file space */
832 * This relies on the UNDOCUMENTED F_FREESP argument to
833 * fcntl(2), which truncates the file so that it ends at the
834 * position indicated by fl.l_start.
836 * Will minor miracles never cease?
839 if (fcntl(fd, F_FREESP, &fl) < 0)
846 #endif /* F_FREESP */
849 do_print(register SV *sv, PerlIO *fp)
854 /* assuming fp is checked earlier */
860 if (SvIOK(sv) && SvIVX(sv) != 0) {
861 PerlIO_printf(fp, PL_ofmt, (double)SvIVX(sv));
862 return !PerlIO_error(fp);
864 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
865 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
866 PerlIO_printf(fp, PL_ofmt, SvNVX(sv));
867 return !PerlIO_error(fp);
870 switch (SvTYPE(sv)) {
872 if (ckWARN(WARN_UNINITIALIZED))
873 warner(WARN_UNINITIALIZED, warn_uninit);
879 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
880 return !PerlIO_error(fp);
884 tmps = SvPV(sv, len);
887 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
889 return !PerlIO_error(fp);
899 if (PL_op->op_flags & OPf_REF) {
901 tmpgv = cGVOP->op_gv;
904 if (io && IoIFP(io)) {
906 sv_setpv(PL_statname,"");
907 PL_laststype = OP_STAT;
908 return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache));
911 if (tmpgv == PL_defgv)
912 return PL_laststatval;
913 if (ckWARN(WARN_UNOPENED))
914 warner(WARN_UNOPENED, "Stat on unopened file <%s>",
917 sv_setpv(PL_statname,"");
918 return (PL_laststatval = -1);
925 if (SvTYPE(sv) == SVt_PVGV) {
929 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
930 tmpgv = (GV*)SvRV(sv);
936 sv_setpv(PL_statname, s);
937 PL_laststype = OP_STAT;
938 PL_laststatval = PerlLIO_stat(s, &PL_statcache);
939 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n'))
940 warner(WARN_NEWLINE, warn_nl, "stat");
941 return PL_laststatval;
950 if (PL_op->op_flags & OPf_REF) {
952 if (cGVOP->op_gv == PL_defgv) {
953 if (PL_laststype != OP_LSTAT)
954 croak("The stat preceding -l _ wasn't an lstat");
955 return PL_laststatval;
957 croak("You can't use -l on a filehandle");
960 PL_laststype = OP_LSTAT;
964 sv_setpv(PL_statname,SvPV(sv, PL_na));
966 PL_laststatval = PerlLIO_lstat(SvPV(sv, PL_na),&PL_statcache);
968 PL_laststatval = PerlLIO_stat(SvPV(sv, PL_na),&PL_statcache);
970 if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, PL_na), '\n'))
971 warner(WARN_NEWLINE, warn_nl, "lstat");
972 return PL_laststatval;
976 do_aexec(SV *really, register SV **mark, register SV **sp)
983 New(401,PL_Argv, sp - mark + 1, char*);
985 while (++mark <= sp) {
987 *a++ = SvPVx(*mark, PL_na);
992 if (*PL_Argv[0] != '/') /* will execvp use PATH? */
993 TAINT_ENV(); /* testing IFS here is overkill, probably */
994 if (really && *(tmps = SvPV(really, PL_na)))
995 PerlProc_execvp(tmps,PL_Argv);
997 PerlProc_execvp(PL_Argv[0],PL_Argv);
998 if (ckWARN(WARN_EXEC))
999 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1000 PL_Argv[0], Strerror(errno));
1011 PL_Argv = Null(char **);
1019 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
1028 while (*cmd && isSPACE(*cmd))
1031 /* save an extra exec if possible */
1034 if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) {
1036 s = cmd+PL_cshlen+3;
1050 if (s[-1] == '\'') {
1052 PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0);
1060 /* see if there are shell metacharacters in it */
1062 if (*cmd == '.' && isSPACE(cmd[1]))
1065 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1068 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1072 for (s = cmd; *s; s++) {
1073 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1074 if (*s == '\n' && !s[1]) {
1079 PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0);
1084 New(402,PL_Argv, (s - cmd) / 2 + 2, char*);
1085 PL_Cmd = savepvn(cmd, s-cmd);
1087 for (s = PL_Cmd; *s;) {
1088 while (*s && isSPACE(*s)) s++;
1091 while (*s && !isSPACE(*s)) s++;
1097 PerlProc_execvp(PL_Argv[0],PL_Argv);
1098 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1102 if (ckWARN(WARN_EXEC))
1103 warner(WARN_EXEC, "Can't exec \"%s\": %s",
1104 PL_Argv[0], Strerror(errno));
1110 #endif /* OS2 || WIN32 */
1113 apply(I32 type, register SV **mark, register SV **sp)
1118 register I32 tot = 0;
1121 SV **oldmark = mark;
1123 #define APPLY_TAINT_PROPER() \
1125 if (PL_tainting && PL_tainted) { goto taint_proper_label; } \
1128 /* This is a first heuristic; it doesn't catch tainting magic. */
1130 while (++mark <= sp) {
1131 if (SvTAINTED(*mark)) {
1141 APPLY_TAINT_PROPER();
1144 APPLY_TAINT_PROPER();
1146 while (++mark <= sp) {
1147 char *name = SvPVx(*mark, PL_na);
1148 APPLY_TAINT_PROPER();
1149 if (PerlLIO_chmod(name, val))
1157 APPLY_TAINT_PROPER();
1158 if (sp - mark > 2) {
1159 val = SvIVx(*++mark);
1160 val2 = SvIVx(*++mark);
1161 APPLY_TAINT_PROPER();
1163 while (++mark <= sp) {
1164 char *name = SvPVx(*mark, PL_na);
1165 APPLY_TAINT_PROPER();
1166 if (PerlLIO_chown(name, val, val2))
1173 XXX Should we make lchown() directly available from perl?
1174 For now, we'll let Configure test for HAS_LCHOWN, but do
1175 nothing in the core.
1181 APPLY_TAINT_PROPER();
1184 s = SvPVx(*++mark, PL_na);
1186 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1188 if (!(val = whichsig(s)))
1189 croak("Unrecognized signal name \"%s\"",s);
1193 APPLY_TAINT_PROPER();
1196 /* kill() doesn't do process groups (job trees?) under VMS */
1197 if (val < 0) val = -val;
1198 if (val == SIGKILL) {
1199 # include <starlet.h>
1200 /* Use native sys$delprc() to insure that target process is
1201 * deleted; supervisor-mode images don't pay attention to
1202 * CRTL's emulation of Unix-style signals and kill()
1204 while (++mark <= sp) {
1205 I32 proc = SvIVx(*mark);
1206 register unsigned long int __vmssts;
1207 APPLY_TAINT_PROPER();
1208 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1212 case SS$_NOSUCHNODE:
1213 SETERRNO(ESRCH,__vmssts);
1216 SETERRNO(EPERM,__vmssts);
1219 SETERRNO(EVMSERR,__vmssts);
1228 while (++mark <= sp) {
1229 I32 proc = SvIVx(*mark);
1230 APPLY_TAINT_PROPER();
1232 if (PerlProc_killpg(proc,val)) /* BSD */
1234 if (PerlProc_kill(-proc,val)) /* SYSV */
1240 while (++mark <= sp) {
1241 I32 proc = SvIVx(*mark);
1242 APPLY_TAINT_PROPER();
1243 if (PerlProc_kill(proc, val))
1251 APPLY_TAINT_PROPER();
1253 while (++mark <= sp) {
1254 s = SvPVx(*mark, PL_na);
1255 APPLY_TAINT_PROPER();
1256 if (PL_euid || PL_unsafe) {
1260 else { /* don't let root wipe out directories without -U */
1262 if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1264 if (PerlLIO_stat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode))
1277 APPLY_TAINT_PROPER();
1278 if (sp - mark > 2) {
1279 #if defined(I_UTIME) || defined(VMS)
1280 struct utimbuf utbuf;
1288 Zero(&utbuf, sizeof utbuf, char);
1290 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1291 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1293 utbuf.actime = SvIVx(*++mark); /* time accessed */
1294 utbuf.modtime = SvIVx(*++mark); /* time modified */
1296 APPLY_TAINT_PROPER();
1298 while (++mark <= sp) {
1299 char *name = SvPVx(*mark, PL_na);
1300 APPLY_TAINT_PROPER();
1301 if (PerlLIO_utime(name, &utbuf))
1314 return 0; /* this should never happen */
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 */