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, ...@@ -139,6 +139,7 @@ extern int stream_sequential_readline __P((stream_t stream,
139 139
140 extern int stream_sequential_write __P((stream_t stream, 140 extern int stream_sequential_write __P((stream_t stream,
141 char *buf, size_t size)); 141 char *buf, size_t size));
142 extern int stream_seek __P((stream_t stream, off_t off, int whence));
142 143
143 extern int stream_strerror __P((stream_t stream, char **p)); 144 extern int stream_strerror __P((stream_t stream, char **p));
144 145
......
...@@ -110,6 +110,9 @@ stream_close (stream_t stream) ...@@ -110,6 +110,9 @@ stream_close (stream_t stream)
110 { 110 {
111 if (stream == NULL) 111 if (stream == NULL)
112 return EINVAL; 112 return EINVAL;
113 if (stream->state == MU_STREAM_STATE_CLOSE)
114 return 0;
115
113 stream->state = MU_STREAM_STATE_CLOSE; 116 stream->state = MU_STREAM_STATE_CLOSE;
114 /* Clear the buffer of any residue left. */ 117 /* Clear the buffer of any residue left. */
115 if (stream->rbuffer.base) 118 if (stream->rbuffer.base)
...@@ -704,6 +707,41 @@ stream_sequential_write (stream_t stream, char *buf, size_t size) ...@@ -704,6 +707,41 @@ stream_sequential_write (stream_t stream, char *buf, size_t size)
704 } 707 }
705 708
706 int 709 int
710 stream_seek (stream_t stream, off_t off, int whence)
711 {
712 size_t size = 0;
713 size_t pos;
714 int rc;
715
716 if ((rc = stream_size (stream, &size)))
717 return rc;
718
719 switch (whence)
720 {
721 case SEEK_SET:
722 pos = off;
723 break;
724
725 case SEEK_CUR:
726 pos = off + stream->offset;
727 break;
728
729 case SEEK_END:
730 pos = size + off;
731 break;
732
733 default:
734 return EINVAL;
735 }
736
737 if (pos > size)
738 return EIO;
739
740 stream->offset = pos;
741 return 0;
742 }
743
744 int
707 stream_strerror (stream_t stream, char **p) 745 stream_strerror (stream_t stream, char **p)
708 { 746 {
709 if (stream == NULL) 747 if (stream == NULL)
......