/* doio.c
*
- * Copyright (c) 1991-1994, Larry Wall
+ * Copyright (c) 1991-1997, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
#endif
#ifdef I_UTIME
-#include <utime.h>
+# ifdef _MSC_VER
+# include <sys/utime.h>
+# else
+# include <utime.h>
+# endif
#endif
#ifdef I_FCNTL
#include <fcntl.h>
#include <sys/file.h>
#endif
+#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
+#include <signal.h>
+#endif
+
+/* XXX If this causes problems, set i_unistd=undef in the hint file. */
+#ifdef I_UNISTD
+# include <unistd.h>
+#endif
+
#if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */
# include <sys/socket.h>
# include <netdb.h>
# endif
#endif
+/* Put this after #includes because <unistd.h> defines _XOPEN_*. */
+#ifndef Sock_size_t
+# if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__)
+# define Sock_size_t Size_t
+# else
+# define Sock_size_t int
+# endif
+#endif
+
bool
do_open(gv,name,len,as_raw,rawmode,rawperm,supplied_fp)
GV *gv;
if (fd == -1)
fp = NULL;
else {
- fp = PerlIO_fdopen(fd, ((result == 0) ? "r"
- : (result == 1) ? "w"
- : "r+"));
+ char *fpmode;
+ if (result == 0)
+ fpmode = "r";
+#ifdef O_APPEND
+ else if (rawmode & O_APPEND)
+ fpmode = (result == 1) ? "a" : "a+";
+#endif
+ else
+ fpmode = (result == 1) ? "w" : "r+";
+ fp = PerlIO_fdopen(fd, fpmode);
if (!fp)
close(fd);
}
!statbuf.st_mode
#endif
) {
- int buflen = sizeof tokenbuf;
- if (getsockname(PerlIO_fileno(fp), (struct sockaddr *)tokenbuf, &buflen) >= 0
- || errno != ENOTSOCK)
+ Sock_size_t buflen = sizeof tokenbuf;
+ if (getsockname(PerlIO_fileno(fp), (struct sockaddr *)tokenbuf,
+ &buflen) >= 0
+ || errno != ENOTSOCK)
IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */
/* but some return 0 for streams too, sigh */
}
(void)unlink(SvPVX(sv));
(void)rename(oldname,SvPVX(sv));
do_open(gv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp);
-#endif /* MSDOS */
+#endif /* DOSISH */
#else
(void)UNLINK(SvPVX(sv));
if (link(oldname,SvPVX(sv)) < 0) {
#endif
}
else {
-#ifndef DOSISH
+#if !defined(DOSISH) && !defined(AMIGAOS)
+# ifndef VMS /* Don't delete; use automatic file versioning */
if (UNLINK(oldname) < 0) {
warn("Can't rename %s to %s: %s, skipping file",
oldname, SvPVX(sv), Strerror(errno) );
do_close(gv,FALSE);
continue;
}
+# endif
#else
croak("Can't do inplace edit without backup");
#endif
#ifdef HAS_FCHMOD
(void)fchmod(lastfd,filemode);
#else
+# if !(defined(WIN32) && defined(__BORLANDC__))
+ /* Borland runtime creates a readonly file! */
(void)chmod(oldname,filemode);
+# endif
#endif
if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
#ifdef HAS_FCHOWN
if (IoIFP(io)) {
if (IoTYPE(io) == '|') {
status = my_pclose(IoIFP(io));
- retval = (status == 0);
- statusvalue = FIXSTATUS(status);
+ STATUS_NATIVE_SET(status);
+ retval = (STATUS_POSIX == 0);
}
else if (IoTYPE(io) == '-')
retval = TRUE;
GV *gv;
{
register IO *io;
+ register PerlIO *fp;
- if (!gv)
- goto phooey;
-
- io = GvIO(gv);
- if (!io || !IoIFP(io))
- goto phooey;
-
+ if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
#ifdef ULTRIX_STDIO_BOTCH
- if (PerlIO_eof(IoIFP(io)))
- (void)PerlIO_seek (IoIFP(io), 0L, 2); /* ultrix 1.2 workaround */
+ if (PerlIO_eof(fp))
+ (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
#endif
-
- return PerlIO_tell(IoIFP(io));
-
-phooey:
+ return PerlIO_tell(fp);
+ }
if (dowarn)
warn("tell() on unopened file");
SETERRNO(EBADF,RMS$_IFI);
int whence;
{
register IO *io;
+ register PerlIO *fp;
- if (!gv)
- goto nuts;
-
- io = GvIO(gv);
- if (!io || !IoIFP(io))
- goto nuts;
-
+ if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) {
#ifdef ULTRIX_STDIO_BOTCH
- if (PerlIO_eof(IoIFP(io)))
- (void)PerlIO_seek (IoIFP(io), 0L, 2); /* ultrix 1.2 workaround */
+ if (PerlIO_eof(fp))
+ (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */
#endif
-
- return PerlIO_seek(IoIFP(io), pos, whence) >= 0;
-
-nuts:
+ return PerlIO_seek(fp, pos, whence) >= 0;
+ }
if (dowarn)
warn("seek() on unopened file");
SETERRNO(EBADF,RMS$_IFI);
return FALSE;
}
+long
+do_sysseek(gv, pos, whence)
+GV *gv;
+long pos;
+int whence;
+{
+ register IO *io;
+ register PerlIO *fp;
+
+ if (gv && (io = GvIO(gv)) && (fp = IoIFP(io)))
+ return lseek(PerlIO_fileno(fp), pos, whence);
+ if (dowarn)
+ warn("sysseek() on unopened file");
+ SETERRNO(EBADF,RMS$_IFI);
+ return -1L;
+}
+
#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP)
/* code courtesy of William Kucharski */
#define HAS_CHSIZE
I32 fd; /* file descriptor */
Off_t length; /* length to set file to */
{
- extern long lseek();
struct flock fl;
struct stat filebuf;
}
#endif /* F_FREESP */
-I32
-looks_like_number(sv)
-SV *sv;
-{
- register char *s;
- register char *send;
-
- if (!SvPOK(sv)) {
- STRLEN len;
- if (!SvPOKp(sv))
- return TRUE;
- s = SvPV(sv, len);
- send = s + len;
- }
- else {
- s = SvPVX(sv);
- send = s + SvCUR(sv);
- }
- while (isSPACE(*s))
- s++;
- if (s >= send)
- return FALSE;
- if (*s == '+' || *s == '-')
- s++;
- while (isDIGIT(*s))
- s++;
- if (s == send)
- return TRUE;
- if (*s == '.')
- s++;
- else if (s == SvPVX(sv))
- return FALSE;
- while (isDIGIT(*s))
- s++;
- if (s == send)
- return TRUE;
- if (*s == 'e' || *s == 'E') {
- s++;
- if (*s == '+' || *s == '-')
- s++;
- while (isDIGIT(*s))
- s++;
- }
- while (isSPACE(*s))
- s++;
- if (s >= send)
- return TRUE;
- return FALSE;
-}
-
bool
do_print(sv,fp)
register SV *sv;
}
}
-#ifndef OS2
+#if !defined(OS2) && !defined(WIN32)
bool
do_exec(cmd)
break;
}
doshell:
- execl(SH_PATH, "sh", "-c", cmd, (char*)0);
+ execl(sh_path, "sh", "-c", cmd, (char*)0);
return FALSE;
}
}
return FALSE;
}
-#endif
+#endif /* OS2 || WIN32 */
I32
apply(type,mark,sp)
if (tainting) {
while (++mark <= sp) {
- MAGIC *mg;
- if (SvMAGICAL(*mark) && (mg = mg_find(*mark, 't')) && mg->mg_len & 1)
- tainted = TRUE;
+ if (SvTAINTED(*mark)) {
+ TAINT;
+ break;
+ }
}
mark = oldmark;
}
#ifdef HAS_KILL
case OP_KILL:
TAINT_PROPER("kill");
+ if (mark == sp)
+ break;
s = SvPVx(*++mark, na);
tot = sp - mark;
if (isUPPER(*s)) {
*/
return (bit & statbufp->st_mode) ? TRUE : FALSE;
-#else /* ! MSDOS */
+#else /* ! DOSISH */
if ((effective ? euid : uid) == 0) { /* root is special */
if (bit == S_IXUSR) {
if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
else if (statbufp->st_mode & bit >> 6)
return TRUE; /* ok as "other" */
return FALSE;
-#endif /* ! MSDOS */
+#endif /* ! DOSISH */
}
#endif /* ! VMS */
char *a;
I32 id, n, cmd, infosize, getinfo;
I32 ret = -1;
+#ifdef __linux__ /* XXX Need metaconfig test */
+ union semun unsemds;
+#endif
id = SvIVx(*++mark);
n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
else if (cmd == GETALL || cmd == SETALL)
{
struct semid_ds semds;
+#ifdef __linux__ /* XXX Need metaconfig test */
+/* linux (and Solaris2?) uses :
+ int semctl (int semid, int semnum, int cmd, union semun arg)
+ union semun {
+ int val;
+ struct semid_ds *buf;
+ ushort *array;
+ };
+*/
+ union semun semun;
+ semun.buf = &semds;
+ if (semctl(id, 0, IPC_STAT, semun) == -1)
+#else
if (semctl(id, 0, IPC_STAT, &semds) == -1)
+#endif
return -1;
getinfo = (cmd == GETALL);
infosize = semds.sem_nsems * sizeof(short);
{
a = SvPV(astr, len);
if (len != infosize)
- croak("Bad arg length for %s, is %d, should be %d",
- op_desc[optype], len, infosize);
+ croak("Bad arg length for %s, is %lu, should be %ld",
+ op_desc[optype], (unsigned long)len, (long)infosize);
}
}
else
{
- I32 i = SvIV(astr);
+ IV i = SvIV(astr);
a = (char *)i; /* ouch */
}
SETERRNO(0,0);
#endif
#ifdef HAS_SEM
case OP_SEMCTL:
+#ifdef __linux__ /* XXX Need metaconfig test */
+ unsemds.buf = (struct semid_ds *)a;
+ ret = semctl(id, n, cmd, unsemds);
+#else
ret = semctl(id, n, cmd, (struct semid_ds *)a);
+#endif
break;
#endif
#ifdef HAS_SHM