summaryrefslogtreecommitdiffabout
path: root/lib/stream.c
blob: c217ca69bacfb9ce305b464d5672fba29ff79edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/* This file is part of Smap.
   Copyright (C) 2009-2010, 2014 Sergey Poznyakoff

   Smap is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Smap is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Smap.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#ifndef SIZE_MAX
# define SIZE_MAX (~((size_t)0))
#endif

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <smap/stream.h>
#include <smap/streamdef.h>

static int
_stream_seterror(struct _smap_stream *stream, int code, int perm)
{
	stream->last_err = code;
	switch (code) {
	case 0:
	case EAGAIN:
	case EINPROGRESS:
		break;

	default:
		if (perm)
			stream->flags |= _SMAP_STR_ERR;
	}
	return code;
}

static int
_stream_realloc_buffer(struct _smap_stream *stream, size_t newsize)
{
	char *newbuf;
	size_t d;

	if (stream->bufsize == SIZE_MAX)
		return ENOMEM;

	if (!newsize) {
		newsize = 2 * stream->bufsize;
		if (newsize < stream->bufsize)
			newsize = SIZE_MAX;
	}
	newbuf = realloc(stream->buffer, newsize);
	if (!newbuf)
		return ENOMEM;
	d = stream->cur - stream->buffer;
	stream->buffer = newbuf;
	stream->bufsize = newsize;
	stream->cur = stream->buffer + d;
	return 0;
}

static int
_stream_fill_full_buffered(struct _smap_stream *stream)
{
	int rc;

	stream->level = 0;
	while (1) {
		size_t nrd = 0;

		stream->flags &= ~_SMAP_STR_MORESPC;
		rc = smap_stream_read_unbuffered(stream,
			 stream->buffer + stream->level,
			 stream->bufsize - stream->level,
			 0, &nrd);
		if (rc == ERANGE && (stream->flags & _SMAP_STR_MORESPC)) {
			smap_stream_clearerr(stream);
			rc = _stream_realloc_buffer(stream, nrd);
			if (rc)
				return rc;
			continue;
		}

		stream->level += nrd;
		break;
	}
	return rc;
}

static int
_stream_fill_line_buffered(struct _smap_stream *stream)
{
	int rc;
	size_t rdn;
	size_t n;
	char c;

	for (n = 0;
	     n < stream->bufsize &&
		     (rc = smap_stream_read_unbuffered(stream,
						       &c, 1,
						       0, &rdn)) == 0
		     && rdn; ) {
		stream->buffer[n++] = c;
		if (c == '\n')
			break;

		if (n == stream->bufsize
		    && stream->flags & SMAP_STREAM_EXPBUF) {
			rc = _stream_realloc_buffer(stream, 0);
			if (rc)
				break;
		}
	}
	stream->level = n;
	return rc;
}

static int
_stream_fill_buffer(struct _smap_stream *stream)
{
	int rc = 0;

	switch (stream->buftype) {
	case smap_buffer_none:
		return 0;

	case smap_buffer_full:
		rc = _stream_fill_full_buffered(stream);
		break;

	case smap_buffer_line:
		rc = _stream_fill_line_buffered(stream);
		break;
	}
	stream->cur = stream->buffer;
	return rc;
}

#define BUFFER_FULL_P(s) \
  ((s)->cur + (s)->level == (s)->buffer + (s)->bufsize)

static int
_stream_buffer_full_p(struct _smap_stream *stream)
{
	switch (stream->buftype) {
	case smap_buffer_none:
		break;

	case smap_buffer_line:
		return BUFFER_FULL_P(stream)
			|| memchr(stream->cur, '\n', stream->level) != NULL;

	case smap_buffer_full:
		return BUFFER_FULL_P(stream);
	}
	return 0;
}

static int
_force_flush_buffer(struct _smap_stream *stream)
{
	int rc = smap_stream_write_unbuffered(stream,
					      stream->cur,
					      stream->level,
					      1, NULL);
	if (rc == 0)
		_stream_advance_buffer(stream, stream->level);
	return rc;
}

static int
_stream_flush_buffer(struct _smap_stream *stream, int all)
{
	int rc;
	char *end;

	if (stream->flags & _SMAP_STR_DIRTY) {
		if ((stream->flags & SMAP_STREAM_SEEK)
		    && (rc = smap_stream_seek(stream, stream->offset,
					      SMAP_SEEK_SET, NULL)))
			return rc;

		switch (stream->buftype) {
		case smap_buffer_none:
			abort(); /* should not happen */

		case smap_buffer_full:
			if ((rc = smap_stream_write_unbuffered(stream,
							       stream->cur,
							       stream->level,
							       1, NULL)))
				return rc;
			if (all)
				_stream_advance_buffer(stream, stream->level);
			break;

		case smap_buffer_line:
			if (stream->level == 0)
				break;
			for (end = memchr(stream->cur, '\n', stream->level);
			     end;
			     end = memchr(stream->cur, '\n', stream->level)) {
				size_t size = end - stream->cur + 1;
				rc = smap_stream_write_unbuffered(stream,
								  stream->cur,
								  size, 1,
								  NULL);
				if (rc)
					return rc;
				_stream_advance_buffer(stream, size);
			}
			if (stream->level) {
				if (all) {
					if ((rc = _force_flush_buffer(stream)))
						return rc;
				} else if (BUFFER_FULL_P(stream)) {
					if (stream->flags & SMAP_STREAM_EXPBUF)
						return _stream_realloc_buffer(stream, 0);
					if ((rc = _force_flush_buffer(stream)))
						return rc;
				}
			}
		}
	} else if (all)
		_stream_advance_buffer(stream, stream->level);

	if (stream->level) {
		if (stream->cur > stream->buffer)
			memmove(stream->buffer, stream->cur, stream->level);
	} else {
		stream->flags &= ~_SMAP_STR_DIRTY;
		stream->level = 0;
	}
	stream->cur = stream->buffer;
	return 0;
}


smap_stream_t
_smap_stream_create(size_t size, int flags)
{
	struct _smap_stream *str;
	if (size < sizeof(str))
		abort();
	str = calloc(1, size);
	if (!str)
		return str;
	str->flags = flags;
	smap_stream_ref(str);
	return str;
}

void
smap_stream_destroy(smap_stream_t *pstream)
{
	if (pstream) {
		smap_stream_t str = *pstream;
		if (str && (str->ref_count == 0 || --str->ref_count == 0)) {
			smap_stream_close(str);
			if (str->done)
				str->done(str);
			free(str);
			*pstream = NULL;
		}
	}
}

void
smap_stream_get_flags(smap_stream_t str, int *pflags)
{
	*pflags = str->flags & ~_SMAP_STR_INTERN_MASK;
}

void
smap_stream_ref(smap_stream_t stream)
{
	stream->ref_count++;
}

void
smap_stream_unref(smap_stream_t stream)
{
	smap_stream_destroy(&stream);
}

int
smap_stream_open(smap_stream_t stream)
{
	int rc;

	if (stream->open && (rc = stream->open(stream)))
		return _stream_seterror(stream, rc, 1);
	stream->bytes_in = stream->bytes_out = 0;
	return 0;
}

const char *
smap_stream_strerror(smap_stream_t stream, int rc)
{
	const char *str;

	if (stream->error_string)
		str = stream->error_string(stream, rc);
	else
		str = strerror(rc);
	return str;
}

int
smap_stream_err(smap_stream_t stream)
{
	return stream->flags & _SMAP_STR_ERR;
}

int
smap_stream_last_error(smap_stream_t stream)
{
	return stream->last_err;
}

void
smap_stream_clearerr(smap_stream_t stream)
{
	stream->last_err = 0;
	stream->flags &= ~_SMAP_STR_ERR;
}

int
smap_stream_eof(smap_stream_t stream)
{
	return stream->flags & _SMAP_STR_EOF;
}

int
smap_stream_seek(smap_stream_t stream, smap_off_t offset, int whence,
		 smap_off_t *pres)
{
	int rc;
	smap_off_t size;

	if (!stream->seek)
		return _stream_seterror(stream, ENOSYS, 0);

	if (!(stream->flags & SMAP_STREAM_SEEK))
		return _stream_seterror(stream, EACCES, 1);

	switch (whence) {
	case SMAP_SEEK_SET:
		break;

	case SMAP_SEEK_CUR:
		if (offset == 0) {
			*pres = stream->offset + _stream_buffer_offset(stream);
			return 0;
		}
		offset += stream->offset;
		break;

	case SMAP_SEEK_END:
		rc = smap_stream_size(stream, &size);
		if (rc)
			return _stream_seterror(stream, rc, 1);
		offset += size;
		break;

	default:
		return _stream_seterror(stream, EINVAL, 1);
	}

	if (stream->buftype == smap_buffer_none
	    || offset < stream->offset
	    || offset > stream->offset + _stream_buffer_offset(stream)) {
		if ((rc = _stream_flush_buffer(stream, 1)))
			return rc;
		rc = stream->seek(stream, offset, &stream->offset);
		if (rc)
			return _stream_seterror(stream, rc, 1);
		_stream_cleareof(stream);
	}

	if (pres)
		*pres = stream->offset + _stream_buffer_offset(stream);
	return 0;
}

/* Skip COUNT bytes from the current position in stream by reading from
   it.  Return new offset in PRES.

   Return 0 on success, EACCES if STREAM was not opened for reading.
   Another non-zero exit codes are propagated from the underlying
   input operations.

   This function is designed to help implement seek method in otherwise
   unseekable streams (such as filters).  Do not use it if you absolutely
   have to.  Using it on an unbuffered stream is a terrible waste of CPU. */
int
smap_stream_skip_input_bytes(smap_stream_t stream, smap_off_t count,
			     smap_off_t *pres)
{
	smap_off_t pos;
	int rc;

	if (!(stream->flags & SMAP_STREAM_READ))
		return _stream_seterror(stream, EACCES, 1);

	if (stream->buftype == smap_buffer_none) {
		for (pos = 0; pos < count; pos++) {
			char c;
			size_t nrd;
			rc = smap_stream_read(stream, &c, 1, &nrd);
			if (nrd == 0)
				rc = ESPIPE;
			if (rc)
				break;
		}
	} else {
		for (pos = 0;;)	{
			if ((rc = _stream_flush_buffer(stream, 1)))
				return rc;
			if (stream->level == 0) {
				rc = _stream_fill_buffer(stream);
				if (rc)
					break;
				if (stream->level == 0) {
					rc = ESPIPE;
					break;
				}
			}
			if (pos <= count && count < pos + stream->level) {
				size_t delta = count - pos;
				_stream_advance_buffer(stream, delta);
				pos = count;
				rc = 0;
				break;
			}
			pos += stream->level;
		}
	}

	stream->offset += pos;
	if (pres)
		*pres = stream->offset;
	return rc;
}

int
smap_stream_set_buffer(smap_stream_t stream, enum smap_buffer_type type,
		       size_t size)
{
	if (size == 0)
		type = smap_buffer_none;

	if (stream->buffer) {
		smap_stream_flush(stream);
		free(stream->buffer);
	}

	stream->buftype = type;
	if (type == smap_buffer_none) {
		stream->buffer = NULL;
		return 0;
	}

	stream->buffer = malloc(size);
	if (stream->buffer == NULL) {
		stream->buftype = smap_buffer_none;
		return _stream_seterror(stream, ENOMEM, 1);
	}
	stream->bufsize = size;
	stream->cur = stream->buffer;
	stream->level = 0;

	return 0;
}

int
smap_stream_read_unbuffered(smap_stream_t stream, void *buf, size_t size,
			    int full_read,
			    size_t *pnread)
{
	int rc;
	size_t nread;

	if (!stream->read)
		return _stream_seterror(stream, ENOSYS, 0);

	if (!(stream->flags & SMAP_STREAM_READ))
		return _stream_seterror(stream, EACCES, 1);

	if (stream->flags & _SMAP_STR_ERR)
		return stream->last_err;

	if ((stream->flags & _SMAP_STR_EOF) || size == 0) {
		if (pnread)
			*pnread = 0;
		return 0;
	}

	if (full_read) {
		size_t rdbytes;

		nread = 0;
		while (size > 0
		       && (rc = stream->read(stream, buf, size, &rdbytes))
			    == 0) {
			if (rdbytes == 0) {
				stream->flags |= _SMAP_STR_EOF;
				break;
			}
			buf += rdbytes;
			nread += rdbytes;
			size -= rdbytes;
			stream->bytes_in += rdbytes;
		}
		if (size && rc)
			rc = _stream_seterror(stream, rc, 0);
	} else {
		rc = stream->read(stream, buf, size, &nread);
		if (rc == 0) {
			if (nread == 0)
				stream->flags |= _SMAP_STR_EOF;
			stream->bytes_in += nread;
		}
		_stream_seterror(stream, rc, rc != 0);
	}
	stream->offset += nread;
	if (pnread)
		*pnread = nread;

	return rc;
}

int
smap_stream_write_unbuffered(smap_stream_t stream,
			     const void *buf, size_t size,
			     int full_write,
			     size_t *pnwritten)
{
	int rc;
	size_t nwritten;

	if (!stream->write)
		return _stream_seterror(stream, ENOSYS, 0);

	if (!(stream->flags & SMAP_STREAM_WRITE))
		return _stream_seterror(stream, EACCES, 1);

	if (stream->flags & _SMAP_STR_ERR)
		return stream->last_err;

	if (size == 0) {
		if (pnwritten)
			*pnwritten = 0;
		return 0;
	}

	if (full_write) {
		size_t wrbytes;
		const char *bufp = buf;

		nwritten = 0;
		while (size > 0
		       && (rc = stream->write(stream, bufp, size, &wrbytes))
			    == 0) {
			if (wrbytes == 0) {
				rc = EIO;
				break;
			}
			bufp += wrbytes;
			nwritten += wrbytes;
			size -= wrbytes;
			stream->bytes_out += wrbytes;
		}
	} else {
		rc = stream->write(stream, buf, size, &nwritten);
		if (rc == 0)
			stream->bytes_out += nwritten;
	}
	stream->flags |= _SMAP_STR_WRT;
	stream->offset += nwritten;
	if (pnwritten)
		*pnwritten = nwritten;
	_stream_seterror(stream, rc, rc != 0);
	return rc;
}

int
smap_stream_read(smap_stream_t stream, void *buf, size_t size, size_t *pread)
{
	if (stream->buftype == smap_buffer_none)
		return smap_stream_read_unbuffered(stream, buf, size,
						   !pread, pread);
	else {
		char *bufp = buf;
		size_t nbytes = 0;
		while (size) {
			size_t n;
			int rc;

			if (stream->level == 0) {
				if ((rc = _stream_fill_buffer(stream))) {
					if (nbytes)
						break;
					return rc;
				}
				if (stream->level == 0)
					break;
			}

			n = size;
			if (n > stream->level)
				n = stream->level;
			memcpy(bufp, stream->cur, n);
			_stream_advance_buffer(stream, n);
			nbytes += n;
			bufp += n;
			size -= n;
			if (stream->buftype == smap_buffer_line
			    && bufp[-1] == '\n')
				break;
		}

		if (pread)
			*pread = nbytes;
	}
	return 0;
}

int
_stream_scandelim(smap_stream_t stream, char *buf, size_t size, int delim,
		  size_t *pnread)
{
	int rc = 0;
	size_t nread = 0;

	size--;
	if (size == 0)
		return EOVERFLOW;
	while (size) {
		char *p;
		size_t len;

		if (stream->level == 0) {
			if ((rc = _stream_fill_buffer(stream))
			    || stream->level == 0)
				break;
		}

		p = memchr(stream->cur, delim, stream->level);
		len = p ? p - stream->cur + 1 : stream->level;
		if (len > size)
			len = size;
		memcpy(buf, stream->cur, len);
		_stream_advance_buffer(stream, len);
		buf += len;
		size -= len;
		nread += len;
		if (p) /* Delimiter found */
			break;
	}
	*buf = 0;
	*pnread = nread;
	return rc;
}

static int
_stream_readdelim(smap_stream_t stream, char *buf, size_t size,
		  int delim, size_t *pread)
{
	int rc;
	char c;
	size_t n = 0, rdn;

	size--;
	if (size == 0)
		return EOVERFLOW;
	for (n = 0;
	     n < size &&
		     (rc = smap_stream_read(stream, &c, 1, &rdn)) == 0 &&
		     rdn;) {
		*buf++ = c;
		n++;
		if (c == delim)
			break;
	}
	*buf = 0;
	if (pread)
		*pread = n;
	return rc;
}

int
smap_stream_readdelim(smap_stream_t stream, char *buf, size_t size,
		      int delim, size_t *pread)
{
	int rc;

	if (size == 0)
		return EINVAL;

	if (stream->readdelim)
		rc = stream->readdelim(stream, buf, size, delim, pread);
	else if (stream->buftype != smap_buffer_none)
		rc = _stream_scandelim(stream, buf, size, delim, pread);
	else
		rc = _stream_readdelim(stream, buf, size, delim, pread);
	return rc;
}

int
smap_stream_readline(smap_stream_t stream, char *buf, size_t size,
		     size_t *pread)
{
	return smap_stream_readdelim(stream, buf, size, '\n', pread);
}

int
smap_stream_getdelim(smap_stream_t stream, char **pbuf, size_t *psize,
		     int delim, size_t *pread)
{
	int rc;
	char *lineptr = *pbuf;
	size_t n = *psize;
	size_t cur_len = 0;

	if (lineptr == NULL || n == 0) {
		char *new_lineptr;
		n = 120;
		new_lineptr = realloc(lineptr, n);
		if (new_lineptr == NULL)
			return ENOMEM;
		lineptr = new_lineptr;
	}

	for (;;) {
		size_t rdn;

		/* Make enough space for len+1 (for final NUL) bytes.  */
		if (cur_len + 1 >= n) {
			size_t needed_max =
				SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1
						     : SIZE_MAX;
			size_t needed = 2 * n + 1;   /* Be generous. */
			char *new_lineptr;

			if (needed_max < needed)
				needed = needed_max;
			if (cur_len + 1 >= needed) {
				rc = EOVERFLOW;
				break;
			}

			new_lineptr = realloc(lineptr, needed);
			if (new_lineptr == NULL) {
				rc = ENOMEM;
				break;
			}

			lineptr = new_lineptr;
			n = needed;
		}

		if (stream->readdelim)
			rc = stream->readdelim(stream, lineptr + cur_len,
					       n - cur_len, delim, &rdn);
		else if (stream->buftype != smap_buffer_none)
			rc = _stream_scandelim(stream, lineptr + cur_len,
					       n - cur_len, delim, &rdn);
		else
			rc = smap_stream_read(stream, lineptr + cur_len, 1,
					      &rdn);

		if (rc || rdn == 0)
			break;
		cur_len += rdn;

		if (lineptr[cur_len - 1] == delim)
			break;
	}
	lineptr[cur_len] = '\0';

	*pbuf = lineptr;
	*psize = n;

	if (pread)
		*pread = cur_len;
	return rc;
}

int
smap_stream_getline(smap_stream_t stream, char **pbuf, size_t *psize,
		    size_t *pread)
{
	return smap_stream_getdelim(stream, pbuf, psize, '\n', pread);
}

int
smap_stream_write(smap_stream_t stream, const void *buf, size_t size,
		  size_t *pnwritten)
{
	int rc = 0;

	if (stream->buftype == smap_buffer_none)
		rc = smap_stream_write_unbuffered(stream, buf, size,
						  !pnwritten, pnwritten);
	else {
		size_t nbytes = 0;
		const char *bufp = buf;

		while (1) {
			size_t n;

			if (_stream_buffer_full_p(stream)
			    && (rc = _stream_flush_buffer(stream, 0)))
				break;

			if (size == 0)
				break;

			n = stream->bufsize - stream->level;
			if (n > size)
				n = size;
			memcpy(stream->cur + stream->level, bufp, n);
			stream->level += n;
			nbytes += n;
			bufp += n;
			size -= n;
			stream->flags |= _SMAP_STR_DIRTY;
		}
		if (pnwritten)
			*pnwritten = nbytes;
	}
	return rc;
}

int
smap_stream_writeline(smap_stream_t stream, const char *buf, size_t size)
{
	int rc;
	if ((rc = smap_stream_write(stream, buf, size, NULL)) == 0)
		rc = smap_stream_write(stream, "\r\n", 2, NULL);
	return rc;
}

int
smap_stream_flush(smap_stream_t stream)
{
	int rc;

	if (!stream)
		return EINVAL;
	rc = _stream_flush_buffer(stream, 1);
	if (rc)
		return rc;
	if ((stream->flags & _SMAP_STR_WRT) && stream->flush)
		return stream->flush(stream);
	stream->flags &= ~_SMAP_STR_WRT;
	return 0;
}

int
smap_stream_close(smap_stream_t stream)
{
	int rc = 0;

	if (!stream)
		return EINVAL;
	smap_stream_flush(stream);
	/* Do close the stream only if it is not used by anyone else */
	if (stream->ref_count > 1)
		return 0;
	if (stream->close)
		rc = stream->close(stream);
	return rc;
}

int
smap_stream_size(smap_stream_t stream, smap_off_t *psize)
{
	int rc;

	if (!stream->size)
		return _stream_seterror(stream, ENOSYS, 0);
	rc = stream->size(stream, psize);
	return _stream_seterror(stream, rc, rc != 0);
}

smap_off_t
smap_stream_bytes_in(smap_stream_t stream)
{
	return stream->bytes_in;
}

smap_off_t
smap_stream_bytes_out(smap_stream_t stream)
{
	return stream->bytes_out;
}

int
smap_stream_ioctl(smap_stream_t stream, int code, void *ptr)
{
	if (stream->ctl == NULL)
		return ENOSYS;
	return stream->ctl(stream, code, ptr);
}

int
smap_stream_wait(smap_stream_t stream, int *pflags, struct timeval *tvp)
{
	int flg = 0;
	if (stream == NULL)
		return EINVAL;

	/* Take to account if we have any buffering.  */
	/* FIXME: How about SMAP_STREAM_READY_WR? */
	if ((*pflags) & SMAP_STREAM_READY_RD
	    && stream->buftype != smap_buffer_none
	    && stream->level > 0) {
		flg = SMAP_STREAM_READY_RD;
		*pflags &= ~SMAP_STREAM_READY_RD;
	}

	if (stream->wait) {
		int rc = stream->wait(stream, pflags, tvp);
		if (rc == 0)
			*pflags |= flg;
		return rc;
	}

	return ENOSYS;
}

int
smap_stream_truncate(smap_stream_t stream, smap_off_t size)
{
	if (stream->truncate)
		return stream->truncate(stream, size);
	return ENOSYS;
}

int
smap_stream_shutdown(smap_stream_t stream, int how)
{
	if (stream->shutdown)
		return stream->shutdown(stream, how);
	return ENOSYS;
}

int
smap_stream_set_flags(smap_stream_t stream, int fl)
{
	if (stream == NULL)
		return EINVAL;
	stream->flags |= (fl & ~_SMAP_STR_INTERN_MASK);
	return 0;
}

int
smap_stream_clr_flags(smap_stream_t stream, int fl)
{
	if (stream == NULL)
		return EINVAL;
	stream->flags &= ~(fl & ~_SMAP_STR_INTERN_MASK);
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.