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 forkprocess = 1; /* assume true if no fork */
100 fd = PerlIO_fileno(IoIFP(io));
101 if (IoTYPE(io) == '-')
103 else if (fd <= 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 > maxsysfd)
122 PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n",
124 IoOFP(io) = IoIFP(io) = Nullfp;
128 result = rawmode & 3;
129 IoTYPE(io) = "<>++"[result];
130 writing = (result > 0);
131 fd = PerlLIO_open3(name, rawmode, rawperm);
139 else if (rawmode & O_APPEND)
140 fpmode = (result == 1) ? "a" : "a+";
143 fpmode = (result == 1) ? "w" : "r+";
144 fp = PerlIO_fdopen(fd, fpmode);
151 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
154 myname = savepvn(name, len);
157 while (len && isSPACE(name[len-1]))
160 mode[0] = mode[1] = mode[2] = '\0';
162 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
170 for (name++; isSPACE(*name); name++) ;
173 TAINT_PROPER("piped open");
174 if (name[strlen(name)-1] == '|') {
175 name[strlen(name)-1] = '\0' ;
177 warn("Can't do bidirectional pipe");
179 fp = PerlProc_popen(name,"w");
182 else if (*name == '>') {
183 TAINT_PROPER("open");
186 mode[0] = IoTYPE(io) = 'a';
201 if (!*name && supplied_fp)
205 for (; isSPACE(*name); name++) ;
210 gv = gv_fetchpv(name,FALSE,SVt_PVIO);
214 SETERRNO(EINVAL,SS$_IVCHAN);
219 fd = PerlIO_fileno(IoIFP(thatio));
220 if (IoTYPE(thatio) == 's')
227 fd = PerlLIO_dup(fd);
230 if (!(fp = PerlIO_fdopen(fd,mode))) {
238 for (; isSPACE(*name); name++) ;
239 if (strEQ(name,"-")) {
240 fp = PerlIO_stdout();
244 fp = PerlIO_open(name,mode);
248 else if (*name == '<') {
250 for (name++; isSPACE(*name); name++) ;
254 if (strEQ(name,"-")) {
259 fp = PerlIO_open(name,mode);
261 else if (len > 1 && name[len-1] == '|') {
263 while (len && isSPACE(name[len-1]))
266 for (; isSPACE(*name); name++) ;
269 TAINT_PROPER("piped open");
270 fp = PerlProc_popen(name,"r");
276 for (; isSPACE(*name); name++) ;
277 if (strEQ(name,"-")) {
282 fp = PerlIO_open(name,"r");
286 if (dowarn && IoTYPE(io) == '<' && strchr(name, '\n'))
287 warn(warn_nl, "open");
291 IoTYPE(io) != '|' && IoTYPE(io) != '-') {
293 if (PerlLIO_fstat(PerlIO_fileno(fp),&statbuf) < 0) {
294 (void)PerlIO_close(fp);
297 if (S_ISSOCK(statbuf.st_mode))
298 IoTYPE(io) = 's'; /* in case a socket was passed in to us */
302 !(statbuf.st_mode & S_IFMT)
308 Sock_size_t buflen = sizeof tmpbuf;
309 if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf,
311 || errno != ENOTSOCK)
312 IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
313 /* but some return 0 for streams too, sigh */
317 if (saveifp) { /* must use old fp? */
318 fd = PerlIO_fileno(saveifp);
320 PerlIO_flush(saveofp); /* emulate PerlIO_close() */
321 if (saveofp != saveifp) { /* was a socket? */
322 PerlIO_close(saveofp);
327 if (fd != PerlIO_fileno(fp)) {
331 PerlLIO_dup2(PerlIO_fileno(fp), fd);
332 sv = *av_fetch(fdpid,PerlIO_fileno(fp),TRUE);
333 (void)SvUPGRADE(sv, SVt_IV);
336 sv = *av_fetch(fdpid,fd,TRUE);
337 (void)SvUPGRADE(sv, SVt_IV);
346 #if defined(HAS_FCNTL) && defined(F_SETFD)
347 fd = PerlIO_fileno(fp);
348 fcntl(fd,F_SETFD,fd > maxsysfd);
353 if (IoTYPE(io) == 's'
354 || (IoTYPE(io) == '>' && S_ISCHR(statbuf.st_mode)) ) {
355 if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) {
369 IoTYPE(io) = savetype;
374 nextargv(register GV *gv)
377 #ifndef FLEXFILENAMES
385 argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO);
386 if (filemode & (S_ISUID|S_ISGID)) {
387 PerlIO_flush(IoIFP(GvIOn(argvoutgv))); /* chmod must follow last write */
389 (void)fchmod(lastfd,filemode);
391 (void)PerlLIO_chmod(oldname,filemode);
395 while (av_len(GvAV(gv)) >= 0) {
398 sv = av_shift(GvAV(gv));
400 sv_setsv(GvSV(gv),sv);
401 SvSETMAGIC(GvSV(gv));
402 oldname = SvPVx(GvSV(gv), oldlen);
403 if (do_open(gv,oldname,oldlen,inplace!=0,0,0,Nullfp)) {
405 TAINT_PROPER("inplace open");
406 if (oldlen == 1 && *oldname == '-') {
407 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
408 return IoIFP(GvIOp(gv));
410 #ifndef FLEXFILENAMES
411 filedev = statbuf.st_dev;
412 fileino = statbuf.st_ino;
414 filemode = statbuf.st_mode;
415 fileuid = statbuf.st_uid;
416 filegid = statbuf.st_gid;
417 if (!S_ISREG(filemode)) {
418 warn("Can't do inplace edit: %s is not a regular file",
424 char *star = strchr(inplace, '*');
426 char *begin = inplace;
427 sv_setpvn(sv, "", 0);
429 sv_catpvn(sv, begin, star - begin);
430 sv_catpvn(sv, oldname, oldlen);
432 } while ((star = strchr(begin, '*')));
437 sv_catpv(sv,inplace);
439 #ifndef FLEXFILENAMES
440 if (PerlLIO_stat(SvPVX(sv),&statbuf) >= 0
441 && statbuf.st_dev == filedev
442 && statbuf.st_ino == fileino
444 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
447 warn("Can't do inplace edit: %s would not be uniq",
455 if (PerlLIO_rename(oldname,SvPVX(sv)) < 0) {
456 warn("Can't rename %s to %s: %s, skipping file",
457 oldname, SvPVX(sv), Strerror(errno) );
463 (void)PerlLIO_unlink(SvPVX(sv));
464 (void)PerlLIO_rename(oldname,SvPVX(sv));
465 do_open(gv,SvPVX(sv),SvCUR(sv),inplace!=0,0,0,Nullfp);
468 (void)UNLINK(SvPVX(sv));
469 if (link(oldname,SvPVX(sv)) < 0) {
470 warn("Can't rename %s to %s: %s, skipping file",
471 oldname, SvPVX(sv), Strerror(errno) );
475 (void)UNLINK(oldname);
479 #if !defined(DOSISH) && !defined(AMIGAOS)
480 # ifndef VMS /* Don't delete; use automatic file versioning */
481 if (UNLINK(oldname) < 0) {
482 warn("Can't remove %s: %s, skipping file",
483 oldname, Strerror(errno) );
489 croak("Can't do inplace edit without backup");
493 sv_setpvn(sv,">",!inplace);
494 sv_catpvn(sv,oldname,oldlen);
495 SETERRNO(0,0); /* in case sprintf set errno */
496 if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),inplace!=0,
497 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
498 warn("Can't do inplace edit on %s: %s",
499 oldname, Strerror(errno) );
503 setdefout(argvoutgv);
504 lastfd = PerlIO_fileno(IoIFP(GvIOp(argvoutgv)));
505 (void)PerlLIO_fstat(lastfd,&statbuf);
507 (void)fchmod(lastfd,filemode);
509 # if !(defined(WIN32) && defined(__BORLANDC__))
510 /* Borland runtime creates a readonly file! */
511 (void)PerlLIO_chmod(oldname,filemode);
514 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
516 (void)fchown(lastfd,fileuid,filegid);
519 (void)PerlLIO_chown(oldname,fileuid,filegid);
524 return IoIFP(GvIOp(gv));
527 PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
528 SvPV(sv, na), Strerror(errno));
531 (void)do_close(argvoutgv,FALSE);
532 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
539 do_pipe(SV *sv, GV *rgv, GV *wgv)
558 if (PerlProc_pipe(fd) < 0)
560 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
561 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
562 IoIFP(wstio) = IoOFP(wstio);
565 if (!IoIFP(rstio) || !IoOFP(wstio)) {
566 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
567 else PerlLIO_close(fd[0]);
568 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
569 else PerlLIO_close(fd[1]);
573 sv_setsv(sv,&sv_yes);
577 sv_setsv(sv,&sv_undef);
582 /* explicit renamed to avoid C++ conflict -- kja */
584 do_close(GV *gv, bool not_implicit)
591 if (!gv || SvTYPE(gv) != SVt_PVGV) {
593 SETERRNO(EBADF,SS$_IVCHAN);
597 if (!io) { /* never opened */
600 warn("Close on unopened file <%s>",GvENAME(gv));
601 SETERRNO(EBADF,SS$_IVCHAN);
605 retval = io_close(io);
609 IoLINES_LEFT(io) = IoPAGE_LEN(io);
622 if (IoTYPE(io) == '|') {
623 status = PerlProc_pclose(IoIFP(io));
624 STATUS_NATIVE_SET(status);
625 retval = (STATUS_POSIX == 0);
627 else if (IoTYPE(io) == '-')
630 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
631 retval = (PerlIO_close(IoOFP(io)) != EOF);
632 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
635 retval = (PerlIO_close(IoIFP(io)) != EOF);
637 IoOFP(io) = IoIFP(io) = Nullfp;
640 SETERRNO(EBADF,SS$_IVCHAN);
660 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
661 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
662 return FALSE; /* this is the most usual case */
665 ch = PerlIO_getc(IoIFP(io));
667 (void)PerlIO_ungetc(IoIFP(io),ch);
670 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
671 if (PerlIO_get_cnt(IoIFP(io)) < -1)
672 PerlIO_set_cnt(IoIFP(io),-1);
674 if (op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
675 if (!nextargv(argvgv)) /* get another fp handy */
679 return TRUE; /* normal fp, definitely end of file */
690 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
691 #ifdef ULTRIX_STDIO_BOTCH
693 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
695 return PerlIO_tell(fp);
698 warn("tell() on unopened file");
699 SETERRNO(EBADF,RMS$_IFI);
704 do_seek(GV *gv, long int pos, int whence)
709 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
710 #ifdef ULTRIX_STDIO_BOTCH
712 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
714 return PerlIO_seek(fp, pos, whence) >= 0;
717 warn("seek() on unopened file");
718 SETERRNO(EBADF,RMS$_IFI);
723 do_sysseek(GV *gv, long int pos, int whence)
728 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
729 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
731 warn("sysseek() on unopened file");
732 SETERRNO(EBADF,RMS$_IFI);
737 do_binmode(PerlIO *fp, int iotype, int flag)
740 croak("panic: unsetting binmode"); /* Not implemented yet */
743 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
748 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
749 #if defined(WIN32) && defined(__BORLANDC__)
750 /* The translation mode of the stream is maintained independent
751 * of the translation mode of the fd in the Borland RTL (heavy
752 * digging through their runtime sources reveal). User has to
753 * set the mode explicitly for the stream (though they don't
754 * document this anywhere). GSAR 97-5-24
756 PerlIO_seek(fp,0L,0);
757 ((FILE*)fp)->flags |= _F_BIN;
765 #if defined(USEMYBINMODE)
766 if (my_binmode(fp,iotype) != NULL)
776 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
777 /* code courtesy of William Kucharski */
780 I32 my_chsize(fd, length)
781 I32 fd; /* file descriptor */
782 Off_t length; /* length to set file to */
787 if (PerlLIO_fstat(fd, &filebuf) < 0)
790 if (filebuf.st_size < length) {
792 /* extend file length */
794 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
797 /* write a "0" byte */
799 if ((PerlLIO_write(fd, "", 1)) != 1)
803 /* truncate length */
808 fl.l_type = F_WRLCK; /* write lock on file space */
811 * This relies on the UNDOCUMENTED F_FREESP argument to
812 * fcntl(2), which truncates the file so that it ends at the
813 * position indicated by fl.l_start.
815 * Will minor miracles never cease?
818 if (fcntl(fd, F_FREESP, &fl) < 0)
825 #endif /* F_FREESP */
828 do_print(register SV *sv, PerlIO *fp)
833 /* assuming fp is checked earlier */
839 if (SvIOK(sv) && SvIVX(sv) != 0) {
840 PerlIO_printf(fp, ofmt, (double)SvIVX(sv));
841 return !PerlIO_error(fp);
843 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
844 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
845 PerlIO_printf(fp, ofmt, SvNVX(sv));
846 return !PerlIO_error(fp);
849 switch (SvTYPE(sv)) {
858 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
859 return !PerlIO_error(fp);
863 tmps = SvPV(sv, len);
866 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
868 return !PerlIO_error(fp);
878 if (op->op_flags & OPf_REF) {
880 tmpgv = cGVOP->op_gv;
883 if (io && IoIFP(io)) {
885 sv_setpv(statname,"");
887 return (laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &statcache));
893 warn("Stat on unopened file <%s>",
896 sv_setpv(statname,"");
897 return (laststatval = -1);
904 if (SvTYPE(sv) == SVt_PVGV) {
908 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
909 tmpgv = (GV*)SvRV(sv);
915 sv_setpv(statname, s);
917 laststatval = PerlLIO_stat(s, &statcache);
918 if (laststatval < 0 && dowarn && strchr(s, '\n'))
919 warn(warn_nl, "stat");
929 if (op->op_flags & OPf_REF) {
931 if (cGVOP->op_gv == defgv) {
932 if (laststype != OP_LSTAT)
933 croak("The stat preceding -l _ wasn't an lstat");
936 croak("You can't use -l on a filehandle");
939 laststype = OP_LSTAT;
943 sv_setpv(statname,SvPV(sv, na));
945 laststatval = PerlLIO_lstat(SvPV(sv, na),&statcache);
947 laststatval = PerlLIO_stat(SvPV(sv, na),&statcache);
949 if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n'))
950 warn(warn_nl, "lstat");
955 do_aexec(SV *really, register SV **mark, register SV **sp)
962 New(401,Argv, sp - mark + 1, char*);
964 while (++mark <= sp) {
966 *a++ = SvPVx(*mark, na);
971 if (*Argv[0] != '/') /* will execvp use PATH? */
972 TAINT_ENV(); /* testing IFS here is overkill, probably */
973 if (really && *(tmps = SvPV(really, na)))
974 PerlProc_execvp(tmps,Argv);
976 PerlProc_execvp(Argv[0],Argv);
978 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
989 Argv = Null(char **);
997 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
1006 while (*cmd && isSPACE(*cmd))
1009 /* save an extra exec if possible */
1012 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
1028 if (s[-1] == '\'') {
1030 PerlProc_execl(cshname,"csh", flags,ncmd,(char*)0);
1038 /* see if there are shell metacharacters in it */
1040 if (*cmd == '.' && isSPACE(cmd[1]))
1043 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1046 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1050 for (s = cmd; *s; s++) {
1051 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1052 if (*s == '\n' && !s[1]) {
1057 PerlProc_execl(sh_path, "sh", "-c", cmd, (char*)0);
1062 New(402,Argv, (s - cmd) / 2 + 2, char*);
1063 Cmd = savepvn(cmd, s-cmd);
1065 for (s = Cmd; *s;) {
1066 while (*s && isSPACE(*s)) s++;
1069 while (*s && !isSPACE(*s)) s++;
1075 PerlProc_execvp(Argv[0],Argv);
1076 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1081 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
1087 #endif /* OS2 || WIN32 */
1090 apply(I32 type, register SV **mark, register SV **sp)
1095 register I32 tot = 0;
1098 SV **oldmark = mark;
1100 #define APPLY_TAINT_PROPER() \
1102 if (tainting && tainted) { goto taint_proper_label; } \
1105 /* This is a first heuristic; it doesn't catch tainting magic. */
1107 while (++mark <= sp) {
1108 if (SvTAINTED(*mark)) {
1118 APPLY_TAINT_PROPER();
1121 APPLY_TAINT_PROPER();
1123 while (++mark <= sp) {
1124 char *name = SvPVx(*mark, na);
1125 APPLY_TAINT_PROPER();
1126 if (PerlLIO_chmod(name, val))
1134 APPLY_TAINT_PROPER();
1135 if (sp - mark > 2) {
1136 val = SvIVx(*++mark);
1137 val2 = SvIVx(*++mark);
1138 APPLY_TAINT_PROPER();
1140 while (++mark <= sp) {
1141 char *name = SvPVx(*mark, na);
1142 APPLY_TAINT_PROPER();
1143 if (PerlLIO_chown(name, val, val2))
1150 XXX Should we make lchown() directly available from perl?
1151 For now, we'll let Configure test for HAS_LCHOWN, but do
1152 nothing in the core.
1158 APPLY_TAINT_PROPER();
1161 s = SvPVx(*++mark, na);
1163 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1165 if (!(val = whichsig(s)))
1166 croak("Unrecognized signal name \"%s\"",s);
1170 APPLY_TAINT_PROPER();
1173 /* kill() doesn't do process groups (job trees?) under VMS */
1174 if (val < 0) val = -val;
1175 if (val == SIGKILL) {
1176 # include <starlet.h>
1177 /* Use native sys$delprc() to insure that target process is
1178 * deleted; supervisor-mode images don't pay attention to
1179 * CRTL's emulation of Unix-style signals and kill()
1181 while (++mark <= sp) {
1182 I32 proc = SvIVx(*mark);
1183 register unsigned long int __vmssts;
1184 APPLY_TAINT_PROPER();
1185 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1189 case SS$_NOSUCHNODE:
1190 SETERRNO(ESRCH,__vmssts);
1193 SETERRNO(EPERM,__vmssts);
1196 SETERRNO(EVMSERR,__vmssts);
1205 while (++mark <= sp) {
1206 I32 proc = SvIVx(*mark);
1207 APPLY_TAINT_PROPER();
1209 if (PerlProc_killpg(proc,val)) /* BSD */
1211 if (PerlProc_kill(-proc,val)) /* SYSV */
1217 while (++mark <= sp) {
1218 I32 proc = SvIVx(*mark);
1219 APPLY_TAINT_PROPER();
1220 if (PerlProc_kill(proc, val))
1228 APPLY_TAINT_PROPER();
1230 while (++mark <= sp) {
1231 s = SvPVx(*mark, na);
1232 APPLY_TAINT_PROPER();
1233 if (euid || unsafe) {
1237 else { /* don't let root wipe out directories without -U */
1239 if (PerlLIO_lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1241 if (PerlLIO_stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1254 APPLY_TAINT_PROPER();
1255 if (sp - mark > 2) {
1256 #if defined(I_UTIME) || defined(VMS)
1257 struct utimbuf utbuf;
1265 Zero(&utbuf, sizeof utbuf, char);
1267 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1268 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1270 utbuf.actime = SvIVx(*++mark); /* time accessed */
1271 utbuf.modtime = SvIVx(*++mark); /* time modified */
1273 APPLY_TAINT_PROPER();
1275 while (++mark <= sp) {
1276 char *name = SvPVx(*mark, na);
1277 APPLY_TAINT_PROPER();
1278 if (PerlLIO_utime(name, &utbuf))
1291 return 0; /* this should never happen */
1293 #undef APPLY_TAINT_PROPER
1296 /* Do the permissions allow some operation? Assumes statcache already set. */
1297 #ifndef VMS /* VMS' cando is in vms.c */
1299 cando(I32 bit, I32 effective, register struct stat *statbufp)
1302 /* [Comments and code from Len Reed]
1303 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1304 * to write-protected files. The execute permission bit is set
1305 * by the Miscrosoft C library stat() function for the following:
1310 * All files and directories are readable.
1311 * Directories and special files, e.g. "CON", cannot be
1313 * [Comment by Tom Dinger -- a directory can have the write-protect
1314 * bit set in the file system, but DOS permits changes to
1315 * the directory anyway. In addition, all bets are off
1316 * here for networked software, such as Novell and
1320 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1321 * too so it will actually look into the files for magic numbers
1323 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1325 #else /* ! DOSISH */
1326 if ((effective ? euid : uid) == 0) { /* root is special */
1327 if (bit == S_IXUSR) {
1328 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1332 return TRUE; /* root reads and writes anything */
1335 if (statbufp->st_uid == (effective ? euid : uid) ) {
1336 if (statbufp->st_mode & bit)
1337 return TRUE; /* ok as "user" */
1339 else if (ingroup((I32)statbufp->st_gid,effective)) {
1340 if (statbufp->st_mode & bit >> 3)
1341 return TRUE; /* ok as "group" */
1343 else if (statbufp->st_mode & bit >> 6)
1344 return TRUE; /* ok as "other" */
1346 #endif /* ! DOSISH */
1351 ingroup(I32 testgid, I32 effective)
1353 if (testgid == (effective ? egid : gid))
1355 #ifdef HAS_GETGROUPS
1360 Groups_t gary[NGROUPS];
1363 anum = getgroups(NGROUPS,gary);
1365 if (gary[anum] == testgid)
1372 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1375 do_ipcget(I32 optype, SV **mark, SV **sp)
1381 key = (key_t)SvNVx(*++mark);
1382 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1383 flags = SvIVx(*++mark);
1389 return msgget(key, flags);
1393 return semget(key, n, flags);
1397 return shmget(key, n, flags);
1399 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1401 croak("%s not implemented", op_desc[optype]);
1404 return -1; /* should never happen */
1408 do_ipcctl(I32 optype, SV **mark, SV **sp)
1413 I32 id, n, cmd, infosize, getinfo;
1416 id = SvIVx(*++mark);
1417 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1418 cmd = SvIVx(*++mark);
1421 getinfo = (cmd == IPC_STAT);
1427 if (cmd == IPC_STAT || cmd == IPC_SET)
1428 infosize = sizeof(struct msqid_ds);
1433 if (cmd == IPC_STAT || cmd == IPC_SET)
1434 infosize = sizeof(struct shmid_ds);
1439 if (cmd == IPC_STAT || cmd == IPC_SET)
1440 infosize = sizeof(struct semid_ds);
1441 else if (cmd == GETALL || cmd == SETALL)
1443 struct semid_ds semds;
1447 getinfo = (cmd == GETALL);
1448 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1450 infosize = semds.sem_nsems * sizeof(short);
1451 /* "short" is technically wrong but much more portable
1452 than guessing about u_?short(_t)? */
1456 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1458 croak("%s not implemented", op_desc[optype]);
1467 SvPV_force(astr, len);
1468 a = SvGROW(astr, infosize+1);
1472 a = SvPV(astr, len);
1473 if (len != infosize)
1474 croak("Bad arg length for %s, is %lu, should be %ld",
1475 op_desc[optype], (unsigned long)len, (long)infosize);
1481 a = (char *)i; /* ouch */
1488 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1493 union semun unsemds;
1495 unsemds.buf = (struct semid_ds *)a;
1496 ret = Semctl(id, n, cmd, unsemds);
1502 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1506 if (getinfo && ret >= 0) {
1507 SvCUR_set(astr, infosize);
1508 *SvEND(astr) = '\0';
1515 do_msgsnd(SV **mark, SV **sp)
1521 I32 id, msize, flags;
1524 id = SvIVx(*++mark);
1526 flags = SvIVx(*++mark);
1527 mbuf = SvPV(mstr, len);
1528 if ((msize = len - sizeof(long)) < 0)
1529 croak("Arg too short for msgsnd");
1531 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1533 croak("msgsnd not implemented");
1538 do_msgrcv(SV **mark, SV **sp)
1545 I32 id, msize, flags, ret;
1548 id = SvIVx(*++mark);
1550 msize = SvIVx(*++mark);
1551 mtype = (long)SvIVx(*++mark);
1552 flags = SvIVx(*++mark);
1553 if (SvTHINKFIRST(mstr)) {
1554 if (SvREADONLY(mstr))
1555 croak("Can't msgrcv to readonly var");
1559 SvPV_force(mstr, len);
1560 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1563 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1565 SvCUR_set(mstr, sizeof(long)+ret);
1566 *SvEND(mstr) = '\0';
1570 croak("msgrcv not implemented");
1575 do_semop(SV **mark, SV **sp)
1584 id = SvIVx(*++mark);
1586 opbuf = SvPV(opstr, opsize);
1587 if (opsize < sizeof(struct sembuf)
1588 || (opsize % sizeof(struct sembuf)) != 0) {
1589 SETERRNO(EINVAL,LIB$_INVARG);
1593 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1595 croak("semop not implemented");
1600 do_shmio(I32 optype, SV **mark, SV **sp)
1606 I32 id, mpos, msize;
1608 struct shmid_ds shmds;
1610 id = SvIVx(*++mark);
1612 mpos = SvIVx(*++mark);
1613 msize = SvIVx(*++mark);
1615 if (shmctl(id, IPC_STAT, &shmds) == -1)
1617 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1618 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1621 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1622 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1624 if (optype == OP_SHMREAD) {
1625 SvPV_force(mstr, len);
1626 mbuf = SvGROW(mstr, msize+1);
1628 Copy(shm + mpos, mbuf, msize, char);
1629 SvCUR_set(mstr, msize);
1630 *SvEND(mstr) = '\0';
1636 mbuf = SvPV(mstr, len);
1637 if ((n = len) > msize)
1639 Copy(mbuf, shm + mpos, n, char);
1641 memzero(shm + mpos + n, msize - n);
1645 croak("shm I/O not implemented");
1649 #endif /* SYSV IPC */