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 (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",
425 add_suffix(sv,inplace);
427 sv_catpv(sv,inplace);
429 #ifndef FLEXFILENAMES
430 if (PerlLIO_stat(SvPVX(sv),&statbuf) >= 0
431 && statbuf.st_dev == filedev
432 && statbuf.st_ino == fileino
434 || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0
437 warn("Can't do inplace edit: %s would not be uniq",
445 if (PerlLIO_rename(oldname,SvPVX(sv)) < 0) {
446 warn("Can't rename %s to %s: %s, skipping file",
447 oldname, SvPVX(sv), Strerror(errno) );
453 (void)PerlLIO_unlink(SvPVX(sv));
454 (void)PerlLIO_rename(oldname,SvPVX(sv));
455 do_open(gv,SvPVX(sv),SvCUR(sv),inplace!=0,0,0,Nullfp);
458 (void)UNLINK(SvPVX(sv));
459 if (link(oldname,SvPVX(sv)) < 0) {
460 warn("Can't rename %s to %s: %s, skipping file",
461 oldname, SvPVX(sv), Strerror(errno) );
465 (void)UNLINK(oldname);
469 #if !defined(DOSISH) && !defined(AMIGAOS)
470 # ifndef VMS /* Don't delete; use automatic file versioning */
471 if (UNLINK(oldname) < 0) {
472 warn("Can't remove %s: %s, skipping file",
473 oldname, Strerror(errno) );
479 croak("Can't do inplace edit without backup");
483 sv_setpvn(sv,">",!inplace);
484 sv_catpvn(sv,oldname,oldlen);
485 SETERRNO(0,0); /* in case sprintf set errno */
486 if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),inplace!=0,
487 O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) {
488 warn("Can't do inplace edit on %s: %s",
489 oldname, Strerror(errno) );
493 setdefout(argvoutgv);
494 lastfd = PerlIO_fileno(IoIFP(GvIOp(argvoutgv)));
495 (void)PerlLIO_fstat(lastfd,&statbuf);
497 (void)fchmod(lastfd,filemode);
499 # if !(defined(WIN32) && defined(__BORLANDC__))
500 /* Borland runtime creates a readonly file! */
501 (void)PerlLIO_chmod(oldname,filemode);
504 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
506 (void)fchown(lastfd,fileuid,filegid);
509 (void)PerlLIO_chown(oldname,fileuid,filegid);
514 return IoIFP(GvIOp(gv));
517 PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",
518 SvPV(sv, na), Strerror(errno));
521 (void)do_close(argvoutgv,FALSE);
522 setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO));
529 do_pipe(SV *sv, GV *rgv, GV *wgv)
548 if (PerlProc_pipe(fd) < 0)
550 IoIFP(rstio) = PerlIO_fdopen(fd[0], "r");
551 IoOFP(wstio) = PerlIO_fdopen(fd[1], "w");
552 IoIFP(wstio) = IoOFP(wstio);
555 if (!IoIFP(rstio) || !IoOFP(wstio)) {
556 if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio));
557 else PerlLIO_close(fd[0]);
558 if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio));
559 else PerlLIO_close(fd[1]);
563 sv_setsv(sv,&sv_yes);
567 sv_setsv(sv,&sv_undef);
572 /* explicit renamed to avoid C++ conflict -- kja */
574 do_close(GV *gv, bool not_implicit)
581 if (!gv || SvTYPE(gv) != SVt_PVGV) {
583 SETERRNO(EBADF,SS$_IVCHAN);
587 if (!io) { /* never opened */
590 warn("Close on unopened file <%s>",GvENAME(gv));
591 SETERRNO(EBADF,SS$_IVCHAN);
595 retval = io_close(io);
599 IoLINES_LEFT(io) = IoPAGE_LEN(io);
612 if (IoTYPE(io) == '|') {
613 status = PerlProc_pclose(IoIFP(io));
614 STATUS_NATIVE_SET(status);
615 retval = (STATUS_POSIX == 0);
617 else if (IoTYPE(io) == '-')
620 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */
621 retval = (PerlIO_close(IoOFP(io)) != EOF);
622 PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
625 retval = (PerlIO_close(IoIFP(io)) != EOF);
627 IoOFP(io) = IoIFP(io) = Nullfp;
630 SETERRNO(EBADF,SS$_IVCHAN);
650 if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */
651 if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */
652 return FALSE; /* this is the most usual case */
655 ch = PerlIO_getc(IoIFP(io));
657 (void)PerlIO_ungetc(IoIFP(io),ch);
660 if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
661 if (PerlIO_get_cnt(IoIFP(io)) < -1)
662 PerlIO_set_cnt(IoIFP(io),-1);
664 if (op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
665 if (!nextargv(argvgv)) /* get another fp handy */
669 return TRUE; /* normal fp, definitely end of file */
680 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
681 #ifdef ULTRIX_STDIO_BOTCH
683 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
685 return PerlIO_tell(fp);
688 warn("tell() on unopened file");
689 SETERRNO(EBADF,RMS$_IFI);
694 do_seek(GV *gv, long int pos, int whence)
699 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
700 #ifdef ULTRIX_STDIO_BOTCH
702 (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
704 return PerlIO_seek(fp, pos, whence) >= 0;
707 warn("seek() on unopened file");
708 SETERRNO(EBADF,RMS$_IFI);
713 do_sysseek(GV *gv, long int pos, int whence)
718 if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
719 return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence);
721 warn("sysseek() on unopened file");
722 SETERRNO(EBADF,RMS$_IFI);
727 do_binmode(PerlIO *fp, int iotype, int flag)
730 croak("panic: unsetting binmode"); /* Not implemented yet */
733 if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN))
738 if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) {
739 #if defined(WIN32) && defined(__BORLANDC__)
740 /* The translation mode of the stream is maintained independent
741 * of the translation mode of the fd in the Borland RTL (heavy
742 * digging through their runtime sources reveal). User has to
743 * set the mode explicitly for the stream (though they don't
744 * document this anywhere). GSAR 97-5-24
746 PerlIO_seek(fp,0L,0);
747 ((FILE*)fp)->flags |= _F_BIN;
755 #if defined(USEMYBINMODE)
756 if (my_binmode(fp,iotype) != NULL)
766 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
767 /* code courtesy of William Kucharski */
770 I32 my_chsize(fd, length)
771 I32 fd; /* file descriptor */
772 Off_t length; /* length to set file to */
777 if (PerlLIO_fstat(fd, &filebuf) < 0)
780 if (filebuf.st_size < length) {
782 /* extend file length */
784 if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
787 /* write a "0" byte */
789 if ((PerlLIO_write(fd, "", 1)) != 1)
793 /* truncate length */
798 fl.l_type = F_WRLCK; /* write lock on file space */
801 * This relies on the UNDOCUMENTED F_FREESP argument to
802 * fcntl(2), which truncates the file so that it ends at the
803 * position indicated by fl.l_start.
805 * Will minor miracles never cease?
808 if (fcntl(fd, F_FREESP, &fl) < 0)
815 #endif /* F_FREESP */
818 do_print(register SV *sv, PerlIO *fp)
823 /* assuming fp is checked earlier */
829 if (SvIOK(sv) && SvIVX(sv) != 0) {
830 PerlIO_printf(fp, ofmt, (double)SvIVX(sv));
831 return !PerlIO_error(fp);
833 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
834 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
835 PerlIO_printf(fp, ofmt, SvNVX(sv));
836 return !PerlIO_error(fp);
839 switch (SvTYPE(sv)) {
848 PerlIO_printf(fp, "%ld", (long)SvIVX(sv));
849 return !PerlIO_error(fp);
853 tmps = SvPV(sv, len);
856 if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp)))
858 return !PerlIO_error(fp);
868 if (op->op_flags & OPf_REF) {
870 tmpgv = cGVOP->op_gv;
873 if (io && IoIFP(io)) {
875 sv_setpv(statname,"");
877 return (laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &statcache));
883 warn("Stat on unopened file <%s>",
886 sv_setpv(statname,"");
887 return (laststatval = -1);
894 if (SvTYPE(sv) == SVt_PVGV) {
898 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) {
899 tmpgv = (GV*)SvRV(sv);
905 sv_setpv(statname, s);
907 laststatval = PerlLIO_stat(s, &statcache);
908 if (laststatval < 0 && dowarn && strchr(s, '\n'))
909 warn(warn_nl, "stat");
919 if (op->op_flags & OPf_REF) {
921 if (cGVOP->op_gv == defgv) {
922 if (laststype != OP_LSTAT)
923 croak("The stat preceding -l _ wasn't an lstat");
926 croak("You can't use -l on a filehandle");
929 laststype = OP_LSTAT;
933 sv_setpv(statname,SvPV(sv, na));
935 laststatval = PerlLIO_lstat(SvPV(sv, na),&statcache);
937 laststatval = PerlLIO_stat(SvPV(sv, na),&statcache);
939 if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n'))
940 warn(warn_nl, "lstat");
945 do_aexec(SV *really, register SV **mark, register SV **sp)
952 New(401,Argv, sp - mark + 1, char*);
954 while (++mark <= sp) {
956 *a++ = SvPVx(*mark, na);
961 if (*Argv[0] != '/') /* will execvp use PATH? */
962 TAINT_ENV(); /* testing IFS here is overkill, probably */
963 if (really && *(tmps = SvPV(really, na)))
964 PerlProc_execvp(tmps,Argv);
966 PerlProc_execvp(Argv[0],Argv);
968 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
979 Argv = Null(char **);
987 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP)
996 while (*cmd && isSPACE(*cmd))
999 /* save an extra exec if possible */
1002 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
1018 if (s[-1] == '\'') {
1020 PerlProc_execl(cshname,"csh", flags,ncmd,(char*)0);
1028 /* see if there are shell metacharacters in it */
1030 if (*cmd == '.' && isSPACE(cmd[1]))
1033 if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1036 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
1040 for (s = cmd; *s; s++) {
1041 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1042 if (*s == '\n' && !s[1]) {
1047 PerlProc_execl(sh_path, "sh", "-c", cmd, (char*)0);
1052 New(402,Argv, (s - cmd) / 2 + 2, char*);
1053 Cmd = savepvn(cmd, s-cmd);
1055 for (s = Cmd; *s;) {
1056 while (*s && isSPACE(*s)) s++;
1059 while (*s && !isSPACE(*s)) s++;
1065 PerlProc_execvp(Argv[0],Argv);
1066 if (errno == ENOEXEC) { /* for system V NIH syndrome */
1071 warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno));
1077 #endif /* OS2 || WIN32 */
1080 apply(I32 type, register SV **mark, register SV **sp)
1085 register I32 tot = 0;
1088 SV **oldmark = mark;
1090 #define APPLY_TAINT_PROPER() \
1092 if (tainting && tainted) { goto taint_proper_label; } \
1095 /* This is a first heuristic; it doesn't catch tainting magic. */
1097 while (++mark <= sp) {
1098 if (SvTAINTED(*mark)) {
1108 APPLY_TAINT_PROPER();
1111 APPLY_TAINT_PROPER();
1113 while (++mark <= sp) {
1114 char *name = SvPVx(*mark, na);
1115 APPLY_TAINT_PROPER();
1116 if (PerlLIO_chmod(name, val))
1124 APPLY_TAINT_PROPER();
1125 if (sp - mark > 2) {
1126 val = SvIVx(*++mark);
1127 val2 = SvIVx(*++mark);
1128 APPLY_TAINT_PROPER();
1130 while (++mark <= sp) {
1131 char *name = SvPVx(*mark, na);
1132 APPLY_TAINT_PROPER();
1133 if (PerlLIO_chown(name, val, val2))
1140 XXX Should we make lchown() directly available from perl?
1141 For now, we'll let Configure test for HAS_LCHOWN, but do
1142 nothing in the core.
1148 APPLY_TAINT_PROPER();
1151 s = SvPVx(*++mark, na);
1153 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1155 if (!(val = whichsig(s)))
1156 croak("Unrecognized signal name \"%s\"",s);
1160 APPLY_TAINT_PROPER();
1163 /* kill() doesn't do process groups (job trees?) under VMS */
1164 if (val < 0) val = -val;
1165 if (val == SIGKILL) {
1166 # include <starlet.h>
1167 /* Use native sys$delprc() to insure that target process is
1168 * deleted; supervisor-mode images don't pay attention to
1169 * CRTL's emulation of Unix-style signals and kill()
1171 while (++mark <= sp) {
1172 I32 proc = SvIVx(*mark);
1173 register unsigned long int __vmssts;
1174 APPLY_TAINT_PROPER();
1175 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1179 case SS$_NOSUCHNODE:
1180 SETERRNO(ESRCH,__vmssts);
1183 SETERRNO(EPERM,__vmssts);
1186 SETERRNO(EVMSERR,__vmssts);
1195 while (++mark <= sp) {
1196 I32 proc = SvIVx(*mark);
1197 APPLY_TAINT_PROPER();
1199 if (PerlProc_killpg(proc,val)) /* BSD */
1201 if (PerlProc_kill(-proc,val)) /* SYSV */
1207 while (++mark <= sp) {
1208 I32 proc = SvIVx(*mark);
1209 APPLY_TAINT_PROPER();
1210 if (PerlProc_kill(proc, val))
1218 APPLY_TAINT_PROPER();
1220 while (++mark <= sp) {
1221 s = SvPVx(*mark, na);
1222 APPLY_TAINT_PROPER();
1223 if (euid || unsafe) {
1227 else { /* don't let root wipe out directories without -U */
1229 if (PerlLIO_lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1231 if (PerlLIO_stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
1244 APPLY_TAINT_PROPER();
1245 if (sp - mark > 2) {
1246 #if defined(I_UTIME) || defined(VMS)
1247 struct utimbuf utbuf;
1255 Zero(&utbuf, sizeof utbuf, char);
1257 utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */
1258 utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */
1260 utbuf.actime = SvIVx(*++mark); /* time accessed */
1261 utbuf.modtime = SvIVx(*++mark); /* time modified */
1263 APPLY_TAINT_PROPER();
1265 while (++mark <= sp) {
1266 char *name = SvPVx(*mark, na);
1267 APPLY_TAINT_PROPER();
1268 if (PerlLIO_utime(name, &utbuf))
1281 return 0; /* this should never happen */
1283 #undef APPLY_TAINT_PROPER
1286 /* Do the permissions allow some operation? Assumes statcache already set. */
1287 #ifndef VMS /* VMS' cando is in vms.c */
1289 cando(I32 bit, I32 effective, register struct stat *statbufp)
1292 /* [Comments and code from Len Reed]
1293 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1294 * to write-protected files. The execute permission bit is set
1295 * by the Miscrosoft C library stat() function for the following:
1300 * All files and directories are readable.
1301 * Directories and special files, e.g. "CON", cannot be
1303 * [Comment by Tom Dinger -- a directory can have the write-protect
1304 * bit set in the file system, but DOS permits changes to
1305 * the directory anyway. In addition, all bets are off
1306 * here for networked software, such as Novell and
1310 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1311 * too so it will actually look into the files for magic numbers
1313 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1315 #else /* ! DOSISH */
1316 if ((effective ? euid : uid) == 0) { /* root is special */
1317 if (bit == S_IXUSR) {
1318 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
1322 return TRUE; /* root reads and writes anything */
1325 if (statbufp->st_uid == (effective ? euid : uid) ) {
1326 if (statbufp->st_mode & bit)
1327 return TRUE; /* ok as "user" */
1329 else if (ingroup((I32)statbufp->st_gid,effective)) {
1330 if (statbufp->st_mode & bit >> 3)
1331 return TRUE; /* ok as "group" */
1333 else if (statbufp->st_mode & bit >> 6)
1334 return TRUE; /* ok as "other" */
1336 #endif /* ! DOSISH */
1341 ingroup(I32 testgid, I32 effective)
1343 if (testgid == (effective ? egid : gid))
1345 #ifdef HAS_GETGROUPS
1350 Groups_t gary[NGROUPS];
1353 anum = getgroups(NGROUPS,gary);
1355 if (gary[anum] == testgid)
1362 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1365 do_ipcget(I32 optype, SV **mark, SV **sp)
1371 key = (key_t)SvNVx(*++mark);
1372 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1373 flags = SvIVx(*++mark);
1379 return msgget(key, flags);
1383 return semget(key, n, flags);
1387 return shmget(key, n, flags);
1389 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1391 croak("%s not implemented", op_desc[optype]);
1394 return -1; /* should never happen */
1398 do_ipcctl(I32 optype, SV **mark, SV **sp)
1403 I32 id, n, cmd, infosize, getinfo;
1406 id = SvIVx(*++mark);
1407 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1408 cmd = SvIVx(*++mark);
1411 getinfo = (cmd == IPC_STAT);
1417 if (cmd == IPC_STAT || cmd == IPC_SET)
1418 infosize = sizeof(struct msqid_ds);
1423 if (cmd == IPC_STAT || cmd == IPC_SET)
1424 infosize = sizeof(struct shmid_ds);
1429 if (cmd == IPC_STAT || cmd == IPC_SET)
1430 infosize = sizeof(struct semid_ds);
1431 else if (cmd == GETALL || cmd == SETALL)
1433 struct semid_ds semds;
1437 getinfo = (cmd == GETALL);
1438 if (Semctl(id, 0, IPC_STAT, semun) == -1)
1440 infosize = semds.sem_nsems * sizeof(short);
1441 /* "short" is technically wrong but much more portable
1442 than guessing about u_?short(_t)? */
1446 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
1448 croak("%s not implemented", op_desc[optype]);
1457 SvPV_force(astr, len);
1458 a = SvGROW(astr, infosize+1);
1462 a = SvPV(astr, len);
1463 if (len != infosize)
1464 croak("Bad arg length for %s, is %lu, should be %ld",
1465 op_desc[optype], (unsigned long)len, (long)infosize);
1471 a = (char *)i; /* ouch */
1478 ret = msgctl(id, cmd, (struct msqid_ds *)a);
1483 union semun unsemds;
1485 unsemds.buf = (struct semid_ds *)a;
1486 ret = Semctl(id, n, cmd, unsemds);
1492 ret = shmctl(id, cmd, (struct shmid_ds *)a);
1496 if (getinfo && ret >= 0) {
1497 SvCUR_set(astr, infosize);
1498 *SvEND(astr) = '\0';
1505 do_msgsnd(SV **mark, SV **sp)
1511 I32 id, msize, flags;
1514 id = SvIVx(*++mark);
1516 flags = SvIVx(*++mark);
1517 mbuf = SvPV(mstr, len);
1518 if ((msize = len - sizeof(long)) < 0)
1519 croak("Arg too short for msgsnd");
1521 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
1523 croak("msgsnd not implemented");
1528 do_msgrcv(SV **mark, SV **sp)
1535 I32 id, msize, flags, ret;
1538 id = SvIVx(*++mark);
1540 msize = SvIVx(*++mark);
1541 mtype = (long)SvIVx(*++mark);
1542 flags = SvIVx(*++mark);
1543 if (SvTHINKFIRST(mstr)) {
1544 if (SvREADONLY(mstr))
1545 croak("Can't msgrcv to readonly var");
1549 SvPV_force(mstr, len);
1550 mbuf = SvGROW(mstr, sizeof(long)+msize+1);
1553 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
1555 SvCUR_set(mstr, sizeof(long)+ret);
1556 *SvEND(mstr) = '\0';
1560 croak("msgrcv not implemented");
1565 do_semop(SV **mark, SV **sp)
1574 id = SvIVx(*++mark);
1576 opbuf = SvPV(opstr, opsize);
1577 if (opsize < sizeof(struct sembuf)
1578 || (opsize % sizeof(struct sembuf)) != 0) {
1579 SETERRNO(EINVAL,LIB$_INVARG);
1583 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
1585 croak("semop not implemented");
1590 do_shmio(I32 optype, SV **mark, SV **sp)
1596 I32 id, mpos, msize;
1598 struct shmid_ds shmds;
1600 id = SvIVx(*++mark);
1602 mpos = SvIVx(*++mark);
1603 msize = SvIVx(*++mark);
1605 if (shmctl(id, IPC_STAT, &shmds) == -1)
1607 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1608 SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */
1611 shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
1612 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1614 if (optype == OP_SHMREAD) {
1615 SvPV_force(mstr, len);
1616 mbuf = SvGROW(mstr, msize+1);
1618 Copy(shm + mpos, mbuf, msize, char);
1619 SvCUR_set(mstr, msize);
1620 *SvEND(mstr) = '\0';
1626 mbuf = SvPV(mstr, len);
1627 if ((n = len) > msize)
1629 Copy(mbuf, shm + mpos, n, char);
1631 memzero(shm + mpos + n, msize - n);
1635 croak("shm I/O not implemented");
1639 #endif /* SYSV IPC */