Commit 5ccdea07 5ccdea07bf2b7f97d015b8010774c567a22edfc7 by Sergey Poznyakoff

(stream_seek): New function.

1 parent 100ab145
......@@ -139,6 +139,7 @@ extern int stream_sequential_readline __P((stream_t stream,
extern int stream_sequential_write __P((stream_t stream,
char *buf, size_t size));
extern int stream_seek __P((stream_t stream, off_t off, int whence));
extern int stream_strerror __P((stream_t stream, char **p));
......
......@@ -110,6 +110,9 @@ stream_close (stream_t stream)
{
if (stream == NULL)
return EINVAL;
if (stream->state == MU_STREAM_STATE_CLOSE)
return 0;
stream->state = MU_STREAM_STATE_CLOSE;
/* Clear the buffer of any residue left. */
if (stream->rbuffer.base)
......@@ -704,6 +707,41 @@ stream_sequential_write (stream_t stream, char *buf, size_t size)
}
int
stream_seek (stream_t stream, off_t off, int whence)
{
size_t size = 0;
size_t pos;
int rc;
if ((rc = stream_size (stream, &size)))
return rc;
switch (whence)
{
case SEEK_SET:
pos = off;
break;
case SEEK_CUR:
pos = off + stream->offset;
break;
case SEEK_END:
pos = size + off;
break;
default:
return EINVAL;
}
if (pos > size)
return EIO;
stream->offset = pos;
return 0;
}
int
stream_strerror (stream_t stream, char **p)
{
if (stream == NULL)
......