oblok.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2003 Gunnar Ritter
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. *
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. *
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. /* Sccsid @(#)oblok.h 1.3 (gritter) 4/17/03 */
  23. #include <sys/types.h>
  24. #ifndef OBLOK
  25. enum {
  26. OBLOK = 4096
  27. };
  28. #endif /* !OBLOK */
  29. enum ob_mode {
  30. OB_EBF = 0, /* error or mode unset */
  31. OB_NBF = 1, /* not buffered */
  32. OB_LBF = 2, /* line buffered */
  33. OB_FBF = 3 /* fully buffered */
  34. };
  35. struct oblok {
  36. char ob_blk[OBLOK]; /* buffered data */
  37. long long ob_wrt; /* amount of data written */
  38. int ob_pos; /* position of first empty date byte */
  39. int ob_fd; /* file descriptor to write to */
  40. enum ob_mode ob_bf; /* buffering mode */
  41. };
  42. /*
  43. * Allocate an output buffer with file descriptor fd and buffer mode bf.
  44. * If bf is OB_EBF, the choice is made dependant upon the file type.
  45. * NULL is returned if no memory is available.
  46. */
  47. extern struct oblok *ob_alloc(int fd, enum ob_mode bf);
  48. /*
  49. * Deallocate the passed output buffer, flushing all data. The file
  50. * descriptor is not closed. Returns -1 if flushing fails.
  51. */
  52. extern ssize_t ob_free(struct oblok *op);
  53. /*
  54. * Write data of length sz to the passed output buffer. Returns -1 on
  55. * error or the amount of data written.
  56. */
  57. extern ssize_t ob_write(struct oblok *op, const char *data, size_t sz);
  58. /*
  59. * Flush all data in the passed output buffer. Returns -1 on error or
  60. * the amount of data written; 0 is success and means 'nothing to flush'.
  61. * The underlying device is not flushed (i. e. no fsync() is performed).
  62. */
  63. extern ssize_t ob_flush(struct oblok *op);
  64. /*
  65. * Flush all output buffers. Called automatically using atexit(). Returns
  66. * -1 on error or the number of buffers flushed; 0 is success.
  67. */
  68. extern int ob_clear(void);
  69. /*
  70. * putc() workalike.
  71. */
  72. #define ob_put(c, op) ((op)->ob_bf != OB_FBF || (op)->ob_pos >= (OBLOK) - 1 ?\
  73. ob_chr((c), (op)) : \
  74. (int)((op)->ob_blk[(op)->ob_pos++] = (char)(c)))
  75. /*
  76. * fputc() workalike.
  77. */
  78. extern int ob_chr(int c, struct oblok *op);
  79. /*
  80. * This function must be supplied by the calling code; it is called on
  81. * write error.
  82. */
  83. extern void writerr(struct oblok *op, int count, int written);