more SWI upgrades

This commit is contained in:
Vitor Santos Costa 2010-02-22 17:59:23 +00:00
parent 42b07768d6
commit 1012f1e1b6
1 changed files with 115 additions and 77 deletions

View File

@ -3,9 +3,9 @@
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: wielemak@science.uva.nl
E-mail: J.Wielemaker@uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2007, University of Amsterdam
Copyright (C): 1985-2009, University of Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@ -222,6 +222,7 @@ S__setbuf(IOSTREAM *s, char *buffer, size_t size)
free(newunbuf);
errno = oldeno;
S__seterror(s);
return -1;
}
}
@ -246,8 +247,8 @@ S__setbuf(IOSTREAM *s, char *buffer, size_t size)
void
Ssetbuffer(IOSTREAM *s, char *buffer, size_t size)
{ S__setbuf(s, buffer, size);
s->flags &= ~SIO_USERBUF;
{ if ( S__setbuf(s, buffer, size) != (size_t)-1 )
s->flags &= ~SIO_USERBUF;
}
@ -339,8 +340,8 @@ StryLock(IOSTREAM *s)
}
static int
S__unlock(IOSTREAM *s)
int
Sunlock(IOSTREAM *s)
{ int rval = 0;
#ifdef DEBUG_IO_LOCKS
@ -359,15 +360,7 @@ S__unlock(IOSTREAM *s)
{ assert(0);
}
return rval;
}
int
Sunlock(IOSTREAM *s)
{ int rval = S__unlock(s);
SUNLOCK(s);
return rval;
}
@ -431,14 +424,13 @@ S__flushbufc(int c, IOSTREAM *s)
{ char chr = (char)c;
if ( (*s->functions->write)(s->handle, &chr, 1) != 1 )
{ s->flags |= SIO_FERR;
{ S__seterror(s);
c = -1;
}
} else
{ if ( S__setbuf(s, NULL, 0) == (size_t)-1 )
{ s->flags |= SIO_FERR;
c = -1;
} else
else
*s->bufp++ = (char)c;
}
}
@ -557,6 +549,7 @@ S__fillbuf(IOSTREAM *s)
} else if ( errno == EWOULDBLOCK )
{ s->bufp = s->buffer;
s->limitp = s->buffer;
S__seterror(s);
return -1;
#endif
} else
@ -1410,7 +1403,10 @@ Sfeof(IOSTREAM *s)
static int
S__seterror(IOSTREAM *s)
{ if ( s->functions->control )
{ s->io_errno = errno;
if ( !(s->flags&SIO_CLOSING) && /* s->handle is already invalid */
s->functions->control )
{ char *msg;
if ( (*s->functions->control)(s->handle,
@ -1441,6 +1437,7 @@ Sfpasteof(IOSTREAM *s)
void
Sclearerr(IOSTREAM *s)
{ s->flags &= ~(SIO_FEOF|SIO_WARN|SIO_FERR|SIO_FEOF2|SIO_TIMEOUT|SIO_CLEARERR);
s->io_errno = 0;
Sseterr(s, 0, NULL);
}
@ -1563,6 +1560,7 @@ Ssize(IOSTREAM *s)
}
errno = ESPIPE;
S__seterror(s);
return -1;
}
@ -1608,6 +1606,7 @@ Sseek64(IOSTREAM *s, int64_t pos, int whence)
if ( !s->functions->seek && !s->functions->seek64 )
{ errno = ESPIPE;
S__seterror(s);
return -1;
}
@ -1628,11 +1627,12 @@ Sseek64(IOSTREAM *s, int64_t pos, int whence)
pos = (*s->functions->seek)(s->handle, (long)pos, whence);
else
{ errno = EINVAL;
S__seterror(s);
return -1;
}
if ( pos < 0 )
{ errno = EINVAL;
{ S__seterror(s);
return -1;
}
@ -1684,6 +1684,7 @@ Stell64(IOSTREAM *s)
return pos;
} else
{ errno = EINVAL;
S__seterror(s);
return -1;
}
}
@ -1693,10 +1694,13 @@ long
Stell(IOSTREAM *s)
{ int64_t pos = Stell64(s);
if ( pos == -1 )
return -1;
if ( pos <= LONG_MAX )
return (long) pos;
errno = EINVAL;
S__seterror(s);
return -1;
}
@ -1717,7 +1721,7 @@ Sclose(IOSTREAM *s)
{ int rval = 0;
if ( s->magic != SIO_MAGIC ) /* already closed!? */
{ errno = EINVAL;
{ s->io_errno = errno = EINVAL;
return -1;
}
@ -1747,11 +1751,12 @@ Sclose(IOSTREAM *s)
}
#endif
if ( s->functions->close && (*s->functions->close)(s->handle) < 0 )
{ s->flags |= SIO_FERR;
{ S__seterror(s);
rval = -1;
}
while(s->locks > 0) /* remove buffer-locks */
{ int rc = S__unlock(s);
{ int rc = Sunlock(s);
if ( rval == 0 )
rval = rc;
@ -1759,7 +1764,6 @@ Sclose(IOSTREAM *s)
if ( rval < 0 )
reportStreamError(s);
run_close_hooks(s); /* deletes Prolog registration */
SUNLOCK(s);
#ifdef O_PLMT
@ -1771,6 +1775,8 @@ Sclose(IOSTREAM *s)
#endif
s->magic = SIO_CMAGIC;
if ( s->message )
free(s->message);
if ( !(s->flags & SIO_STATIC) )
free(s);
@ -1826,8 +1832,7 @@ Sgets(char *buf)
int
Sfputs(const char *q, IOSTREAM *s)
{
for( ; *q; q++)
{ for( ; *q; q++)
{ if ( Sputcode(*q&0xff, s) < 0 )
return EOF;
}
@ -2003,7 +2008,7 @@ Svfprintf(IOSTREAM *s, const char *fm, va_list args)
v = va_arg(args, int);
break;
case 1:
v = va_arg(args, intptr_t);
v = va_arg(args, long);
break;
case 2:
vl = va_arg(args, int64_t);
@ -2660,6 +2665,15 @@ IOFUNCTIONS Sttyfunctions =
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(*) Windows isatty() is totally broken since VC9; crashing the
application instead of returning EINVAL on wrong values of fd. As we
provide the socket-id through Sfileno, this code crashes on
tcp_open_socket(). As ttys and its detection is of no value on Windows
anyway, we skip this. Second, Windows doesn't have fork(), so FD_CLOEXEC
is of no value.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
IOSTREAM *
Snew(void *handle, int flags, IOFUNCTIONS *functions)
{ IOSTREAM *s;
@ -2692,8 +2706,16 @@ Snew(void *handle, int flags, IOFUNCTIONS *functions)
recursiveMutexInit(s->mutex);
}
#endif
if ( (fd = Sfileno(s)) >= 0 && isatty(fd) )
s->flags |= SIO_ISATTY;
#ifndef __WINDOWS__ /* (*) */
if ( (fd = Sfileno(s)) >= 0 )
{ if ( isatty(fd) )
s->flags |= SIO_ISATTY;
#if defined(F_SETFD) && defined(FD_CLOEXEC)
fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif
}
#endif
return s;
}
@ -2718,7 +2740,7 @@ IOSTREAM *
Sopen_file(const char *path, const char *how)
{ int fd;
int oflags = O_BINARY;
int flags = SIO_FILE|SIO_TEXT|SIO_RECORDPOS;
int flags = SIO_FILE|SIO_TEXT|SIO_RECORDPOS|SIO_FBUF;
int op = *how++;
intptr_t lfd;
enum {lnone=0,lread,lwrite} lock = lnone;
@ -2830,8 +2852,8 @@ Sopen_file(const char *path, const char *how)
IOSTREAM *
Sfdopen(int fd, const char *type)
{ int flags;
intptr_t lfd;
{ intptr_t lfd;
int flags = SIO_FILE|SIO_RECORDPOS|SIO_FBUF;
if ( fd < 0 )
{ errno = EINVAL;
@ -2843,9 +2865,15 @@ Sfdopen(int fd, const char *type)
#endif
if ( *type == 'r' )
flags = SIO_FILE|SIO_INPUT|SIO_RECORDPOS;
else
flags = SIO_FILE|SIO_OUTPUT|SIO_RECORDPOS;
{ flags |= SIO_INPUT;
} else if ( *type == 'w' )
{ flags |= SIO_OUTPUT;
} else
{ errno = EINVAL;
return NULL;
}
if ( type[1] != 'b' )
flags |= SIO_TEXT;
lfd = (intptr_t)fd;
@ -2948,9 +2976,9 @@ Sopen_pipe(const char *command, const char *type)
{ int flags;
if ( *type == 'r' )
flags = SIO_PIPE|SIO_INPUT;
flags = SIO_PIPE|SIO_INPUT|SIO_FBUF;
else
flags = SIO_PIPE|SIO_OUTPUT;
flags = SIO_PIPE|SIO_OUTPUT|SIO_FBUF;
return Snew((void *)fd, flags, &Spipefunctions);
}
@ -3004,7 +3032,7 @@ Swrite_memfile(void *handle, char *buf, size_t size)
{ memfile *mf = handle;
if ( mf->here + size + 1 >= mf->allocated )
{ intptr_t ns = S__memfile_nextsize(mf->here + size + 1);
{ size_t ns = S__memfile_nextsize(mf->here + size + 1);
char *nb;
if ( mf->allocated == 0 || !mf->malloced )
@ -3416,7 +3444,17 @@ Scleanup(void)
s->bufp = s->buffer; /* avoid actual flush */
S__removebuf(s);
#ifdef O_PLMT
if ( S__iob[i].mutex )
{ recursiveMutex *m = S__iob[i].mutex;
S__iob[i].mutex = NULL;
recursiveMutexDelete(m);
free(m);
}
#endif
*s = S__iob0[i]; /* re-initialise */
}
}