• Home
  • Raw
  • Download

Lines Matching refs:shf

40 static int shf_fillbuf(struct shf *);
41 static int shf_emptybuf(struct shf *, int);
48 struct shf *
51 struct shf *shf; in shf_open() local
58 shf = alloc(sizeof(struct shf) + bsize, ATEMP); in shf_open()
59 shf->areap = ATEMP; in shf_open()
60 shf->buf = (unsigned char *)&shf[1]; in shf_open()
61 shf->bsize = bsize; in shf_open()
62 shf->flags = SHF_ALLOCS; in shf_open()
68 afree(shf, shf->areap); in shf_open()
79 afree(shf, shf->areap); in shf_open()
89 return (shf_reopen(fd, sflags, shf)); in shf_open()
126 struct shf *
127 shf_fdopen(int fd, int sflags, struct shf *shf) in shf_fdopen() argument
134 if (shf) { in shf_fdopen()
136 shf->buf = alloc(bsize, ATEMP); in shf_fdopen()
139 shf->buf = NULL; in shf_fdopen()
141 shf = alloc(sizeof(struct shf) + bsize, ATEMP); in shf_fdopen()
142 shf->buf = (unsigned char *)&shf[1]; in shf_fdopen()
145 shf->areap = ATEMP; in shf_fdopen()
146 shf->fd = fd; in shf_fdopen()
147 shf->rp = shf->wp = shf->buf; in shf_fdopen()
148 shf->rnleft = 0; in shf_fdopen()
149 shf->rbsize = bsize; in shf_fdopen()
150 shf->wnleft = 0; /* force call to shf_emptybuf() */ in shf_fdopen()
151 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize; in shf_fdopen()
152 shf->flags = sflags; in shf_fdopen()
153 shf->errnosv = 0; in shf_fdopen()
154 shf->bsize = bsize; in shf_fdopen()
157 return (shf); in shf_fdopen()
161 struct shf *
162 shf_reopen(int fd, int sflags, struct shf *shf) in shf_reopen() argument
169 if (!shf || !shf->buf || shf->bsize < bsize) in shf_reopen()
173 shf->fd = fd; in shf_reopen()
174 shf->rp = shf->wp = shf->buf; in shf_reopen()
175 shf->rnleft = 0; in shf_reopen()
176 shf->rbsize = bsize; in shf_reopen()
177 shf->wnleft = 0; /* force call to shf_emptybuf() */ in shf_reopen()
178 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize; in shf_reopen()
179 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags; in shf_reopen()
180 shf->errnosv = 0; in shf_reopen()
183 return (shf); in shf_reopen()
195 struct shf *
196 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf) in shf_sopen() argument
203 if (!shf) { in shf_sopen()
204 shf = alloc(sizeof(struct shf), ATEMP); in shf_sopen()
207 shf->areap = ATEMP; in shf_sopen()
212 buf = alloc(bsize, shf->areap); in shf_sopen()
214 shf->fd = -1; in shf_sopen()
215 shf->buf = shf->rp = shf->wp = (unsigned char *)buf; in shf_sopen()
216 shf->rnleft = bsize; in shf_sopen()
217 shf->rbsize = bsize; in shf_sopen()
218 shf->wnleft = bsize - 1; /* space for a '\0' */ in shf_sopen()
219 shf->wbsize = bsize; in shf_sopen()
220 shf->flags = sflags | SHF_STRING; in shf_sopen()
221 shf->errnosv = 0; in shf_sopen()
222 shf->bsize = bsize; in shf_sopen()
224 return (shf); in shf_sopen()
229 shf_close(struct shf *shf) in shf_close() argument
233 if (shf->fd >= 0) { in shf_close()
234 ret = shf_flush(shf); in shf_close()
235 if (close(shf->fd) < 0) in shf_close()
238 if (shf->flags & SHF_ALLOCS) in shf_close()
239 afree(shf, shf->areap); in shf_close()
240 else if (shf->flags & SHF_ALLOCB) in shf_close()
241 afree(shf->buf, shf->areap); in shf_close()
248 shf_fdclose(struct shf *shf) in shf_fdclose() argument
252 if (shf->fd >= 0) { in shf_fdclose()
253 ret = shf_flush(shf); in shf_fdclose()
254 if (close(shf->fd) < 0) in shf_fdclose()
256 shf->rnleft = 0; in shf_fdclose()
257 shf->rp = shf->buf; in shf_fdclose()
258 shf->wnleft = 0; in shf_fdclose()
259 shf->fd = -1; in shf_fdclose()
271 shf_sclose(struct shf *shf) in shf_sclose() argument
273 unsigned char *s = shf->buf; in shf_sclose()
276 if (shf->flags & SHF_WR) { in shf_sclose()
277 shf->wnleft++; in shf_sclose()
278 shf_putc('\0', shf); in shf_sclose()
280 if (shf->flags & SHF_ALLOCS) in shf_sclose()
281 afree(shf, shf->areap); in shf_sclose()
290 shf_flush(struct shf *shf) in shf_flush() argument
294 if (shf->flags & SHF_STRING) in shf_flush()
295 rv = (shf->flags & SHF_WR) ? -1 : 0; in shf_flush()
296 else if (shf->fd < 0) in shf_flush()
298 else if (shf->flags & SHF_ERROR) { in shf_flush()
299 errno = shf->errnosv; in shf_flush()
301 } else if (shf->flags & SHF_READING) { in shf_flush()
302 shf->flags &= ~(SHF_EOF | SHF_READING); in shf_flush()
303 if (shf->rnleft > 0) { in shf_flush()
304 if (lseek(shf->fd, (off_t)-shf->rnleft, in shf_flush()
306 shf->flags |= SHF_ERROR; in shf_flush()
307 shf->errnosv = errno; in shf_flush()
310 shf->rnleft = 0; in shf_flush()
311 shf->rp = shf->buf; in shf_flush()
313 } else if (shf->flags & SHF_WRITING) in shf_flush()
314 rv = shf_emptybuf(shf, 0); in shf_flush()
324 shf_emptybuf(struct shf *shf, int flags) in shf_emptybuf() argument
328 if (!(shf->flags & SHF_STRING) && shf->fd < 0) in shf_emptybuf()
331 if (shf->flags & SHF_ERROR) { in shf_emptybuf()
332 errno = shf->errnosv; in shf_emptybuf()
336 if (shf->flags & SHF_READING) { in shf_emptybuf()
340 ret = shf_flush(shf); in shf_emptybuf()
341 shf->flags &= ~SHF_READING; in shf_emptybuf()
343 if (shf->flags & SHF_STRING) { in shf_emptybuf()
351 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) || in shf_emptybuf()
352 !(shf->flags & SHF_ALLOCB)) in shf_emptybuf()
355 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap); in shf_emptybuf()
356 shf->rp = nbuf + (shf->rp - shf->buf); in shf_emptybuf()
357 shf->wp = nbuf + (shf->wp - shf->buf); in shf_emptybuf()
358 shf->rbsize += shf->wbsize; in shf_emptybuf()
359 shf->wnleft += shf->wbsize; in shf_emptybuf()
360 shf->wbsize <<= 1; in shf_emptybuf()
361 shf->buf = nbuf; in shf_emptybuf()
363 if (shf->flags & SHF_WRITING) { in shf_emptybuf()
364 ssize_t n, ntowrite = shf->wp - shf->buf; in shf_emptybuf()
365 unsigned char *buf = shf->buf; in shf_emptybuf()
368 n = write(shf->fd, buf, ntowrite); in shf_emptybuf()
371 !(shf->flags & SHF_INTERRUPT)) in shf_emptybuf()
373 shf->flags |= SHF_ERROR; in shf_emptybuf()
374 shf->errnosv = errno; in shf_emptybuf()
375 shf->wnleft = 0; in shf_emptybuf()
376 if (buf != shf->buf) { in shf_emptybuf()
381 memmove(shf->buf, buf, in shf_emptybuf()
383 shf->wp = shf->buf + ntowrite; in shf_emptybuf()
391 shf->wp = shf->buf; in shf_emptybuf()
392 shf->wnleft = 0; in shf_emptybuf()
393 shf->flags &= ~SHF_WRITING; in shf_emptybuf()
397 shf->wp = shf->buf; in shf_emptybuf()
398 shf->wnleft = shf->wbsize; in shf_emptybuf()
400 shf->flags |= SHF_WRITING; in shf_emptybuf()
407 shf_fillbuf(struct shf *shf) in shf_fillbuf() argument
411 if (shf->flags & SHF_STRING) in shf_fillbuf()
414 if (shf->fd < 0) in shf_fillbuf()
417 if (shf->flags & (SHF_EOF | SHF_ERROR)) { in shf_fillbuf()
418 if (shf->flags & SHF_ERROR) in shf_fillbuf()
419 errno = shf->errnosv; in shf_fillbuf()
423 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1) in shf_fillbuf()
426 shf->flags |= SHF_READING; in shf_fillbuf()
428 shf->rp = shf->buf; in shf_fillbuf()
430 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize); in shf_fillbuf()
431 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT)) in shf_fillbuf()
436 shf->flags |= SHF_ERROR; in shf_fillbuf()
437 shf->errnosv = errno; in shf_fillbuf()
438 shf->rnleft = 0; in shf_fillbuf()
439 shf->rp = shf->buf; in shf_fillbuf()
442 if ((shf->rnleft = n) == 0) in shf_fillbuf()
443 shf->flags |= SHF_EOF; in shf_fillbuf()
453 shf_read(char *buf, ssize_t bsize, struct shf *shf) in shf_read() argument
457 if (!(shf->flags & SHF_RD)) in shf_read()
459 (unsigned int)shf->flags); in shf_read()
465 if (shf->rnleft == 0 && in shf_read()
466 (shf_fillbuf(shf) == -1 || shf->rnleft == 0)) in shf_read()
468 ncopy = shf->rnleft; in shf_read()
471 memcpy(buf, shf->rp, ncopy); in shf_read()
474 shf->rp += ncopy; in shf_read()
475 shf->rnleft -= ncopy; in shf_read()
478 return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) : in shf_read()
489 shf_getse(char *buf, ssize_t bsize, struct shf *shf) in shf_getse() argument
495 if (!(shf->flags & SHF_RD)) in shf_getse()
497 (unsigned int)shf->flags); in shf_getse()
505 if (shf->rnleft == 0) { in shf_getse()
506 if (shf_fillbuf(shf) == -1) in shf_getse()
508 if (shf->rnleft == 0) { in shf_getse()
513 end = (unsigned char *)memchr((char *)shf->rp, '\n', in shf_getse()
514 shf->rnleft); in shf_getse()
515 ncopy = end ? end - shf->rp + 1 : shf->rnleft; in shf_getse()
518 memcpy(buf, (char *) shf->rp, ncopy); in shf_getse()
519 shf->rp += ncopy; in shf_getse()
520 shf->rnleft -= ncopy; in shf_getse()
533 int c = shf_getc(shf); in shf_getse()
537 shf_ungetc(c, shf); in shf_getse()
546 shf_getchar(struct shf *shf) in shf_getchar() argument
548 if (!(shf->flags & SHF_RD)) in shf_getchar()
550 (unsigned int)shf->flags); in shf_getchar()
552 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0)) in shf_getchar()
554 --shf->rnleft; in shf_getchar()
555 return (*shf->rp++); in shf_getchar()
563 shf_ungetc(int c, struct shf *shf) in shf_ungetc() argument
565 if (!(shf->flags & SHF_RD)) in shf_ungetc()
567 (unsigned int)shf->flags); in shf_ungetc()
569 if ((shf->flags & SHF_ERROR) || c == -1 || in shf_ungetc()
570 (shf->rp == shf->buf && shf->rnleft)) in shf_ungetc()
573 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1) in shf_ungetc()
576 if (shf->rp == shf->buf) in shf_ungetc()
577 shf->rp = shf->buf + shf->rbsize; in shf_ungetc()
578 if (shf->flags & SHF_STRING) { in shf_ungetc()
583 if ((int)(shf->rp[-1]) != c) in shf_ungetc()
585 shf->flags &= ~SHF_EOF; in shf_ungetc()
586 shf->rp--; in shf_ungetc()
587 shf->rnleft++; in shf_ungetc()
590 shf->flags &= ~SHF_EOF; in shf_ungetc()
591 *--(shf->rp) = c; in shf_ungetc()
592 shf->rnleft++; in shf_ungetc()
601 shf_putchar(int c, struct shf *shf) in shf_putchar() argument
603 if (!(shf->flags & SHF_WR)) in shf_putchar()
605 (unsigned int)shf->flags); in shf_putchar()
610 if (shf->flags & SHF_UNBUF) { in shf_putchar()
614 if (shf->fd < 0) in shf_putchar()
616 if (shf->flags & SHF_ERROR) { in shf_putchar()
617 errno = shf->errnosv; in shf_putchar()
620 while ((n = write(shf->fd, &cc, 1)) != 1) in shf_putchar()
623 !(shf->flags & SHF_INTERRUPT)) in shf_putchar()
625 shf->flags |= SHF_ERROR; in shf_putchar()
626 shf->errnosv = errno; in shf_putchar()
631 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1) in shf_putchar()
633 shf->wnleft--; in shf_putchar()
634 *shf->wp++ = c; in shf_putchar()
645 shf_puts(const char *s, struct shf *shf) in shf_puts() argument
650 return (shf_write(s, strlen(s), shf)); in shf_puts()
655 shf_write(const char *buf, ssize_t nbytes, struct shf *shf) in shf_write() argument
659 if (!(shf->flags & SHF_WR)) in shf_write()
661 (unsigned int)shf->flags); in shf_write()
667 if ((ncopy = shf->wnleft) && in shf_write()
668 (shf->wp != shf->buf || nbytes < shf->wnleft)) { in shf_write()
671 memcpy(shf->wp, buf, ncopy); in shf_write()
674 shf->wp += ncopy; in shf_write()
675 shf->wnleft -= ncopy; in shf_write()
678 if (shf->flags & SHF_STRING) { in shf_write()
680 while (nbytes > shf->wnleft) in shf_write()
681 if (shf_emptybuf(shf, EB_GROW) == -1) in shf_write()
686 if (shf_emptybuf(shf, EB_GROW) == -1) in shf_write()
689 if (nbytes > shf->wbsize) { in shf_write()
691 if (shf->wbsize) in shf_write()
692 ncopy -= nbytes % shf->wbsize; in shf_write()
695 n = write(shf->fd, buf, ncopy); in shf_write()
698 !(shf->flags & SHF_INTERRUPT)) in shf_write()
700 shf->flags |= SHF_ERROR; in shf_write()
701 shf->errnosv = errno; in shf_write()
702 shf->wnleft = 0; in shf_write()
717 memcpy(shf->wp, buf, nbytes); in shf_write()
718 shf->wp += nbytes; in shf_write()
719 shf->wnleft -= nbytes; in shf_write()
727 shf_fprintf(struct shf *shf, const char *fmt, ...) in shf_fprintf() argument
733 n = shf_vfprintf(shf, fmt, args); in shf_fprintf()
742 struct shf shf; in shf_snprintf() local
750 shf_sopen(buf, bsize, SHF_WR, &shf); in shf_snprintf()
752 n = shf_vfprintf(&shf, fmt, args); in shf_snprintf()
755 shf_sclose(&shf); in shf_snprintf()
762 struct shf shf; in shf_smprintf() local
765 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf); in shf_smprintf()
767 shf_vfprintf(&shf, fmt, args); in shf_smprintf()
770 return (shf_sclose(&shf)); in shf_smprintf()
787 shf_vfprintf(struct shf *shf, const char *fmt, va_list args) in shf_vfprintf() argument
806 shf_putc(c, shf); in shf_vfprintf()
1001 print_value_quoted(shf, s); in shf_vfprintf()
1034 shf_putc(*s, shf); in shf_vfprintf()
1039 shf_putc(*s, shf); in shf_vfprintf()
1044 shf_putc(*s, shf); in shf_vfprintf()
1055 shf_putc(c, shf); in shf_vfprintf()
1065 shf_putc(*s++, shf); in shf_vfprintf()
1069 shf_putc(c, shf); in shf_vfprintf()
1072 return (shf_error(shf) ? -1 : nwritten); in shf_vfprintf()
1077 shf_getc(struct shf *shf) in shf_getc() argument
1079 return (shf_getc_i(shf)); in shf_getc()
1083 shf_putc(int c, struct shf *shf) in shf_putc() argument
1085 return (shf_putc_i(c, shf)); in shf_putc()