oblok.h (2940B)
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 24 #include <sys/types.h> 25 26 #ifndef OBLOK 27 enum { 28 OBLOK = 4096 29 }; 30 #endif /* !OBLOK */ 31 32 enum ob_mode { 33 OB_EBF = 0, /* error or mode unset */ 34 OB_NBF = 1, /* not buffered */ 35 OB_LBF = 2, /* line buffered */ 36 OB_FBF = 3 /* fully buffered */ 37 }; 38 39 struct oblok { 40 char ob_blk[OBLOK]; /* buffered data */ 41 long long ob_wrt; /* amount of data written */ 42 int ob_pos; /* position of first empty date byte */ 43 int ob_fd; /* file descriptor to write to */ 44 enum ob_mode ob_bf; /* buffering mode */ 45 }; 46 47 /* 48 * Allocate an output buffer with file descriptor fd and buffer mode bf. 49 * If bf is OB_EBF, the choice is made dependant upon the file type. 50 * NULL is returned if no memory is available. 51 */ 52 extern struct oblok *ob_alloc(int fd, enum ob_mode bf); 53 54 /* 55 * Deallocate the passed output buffer, flushing all data. The file 56 * descriptor is not closed. Returns -1 if flushing fails. 57 */ 58 extern ssize_t ob_free(struct oblok *op); 59 60 /* 61 * Write data of length sz to the passed output buffer. Returns -1 on 62 * error or the amount of data written. 63 */ 64 extern ssize_t ob_write(struct oblok *op, const char *data, size_t sz); 65 66 /* 67 * Flush all data in the passed output buffer. Returns -1 on error or 68 * the amount of data written; 0 is success and means 'nothing to flush'. 69 * The underlying device is not flushed (i. e. no fsync() is performed). 70 */ 71 extern ssize_t ob_flush(struct oblok *op); 72 73 /* 74 * Flush all output buffers. Called automatically using atexit(). Returns 75 * -1 on error or the number of buffers flushed; 0 is success. 76 */ 77 extern int ob_clear(void); 78 79 /* 80 * putc() workalike. 81 */ 82 #define ob_put(c, op) ((op)->ob_bf != OB_FBF || (op)->ob_pos >= (OBLOK) - 1 ?\ 83 ob_chr((c), (op)) : \ 84 (int)((op)->ob_blk[(op)->ob_pos++] = (char)(c))) 85 86 87 /* 88 * fputc() workalike. 89 */ 90 extern int ob_chr(int c, struct oblok *op); 91 92 /* 93 * This function must be supplied by the calling code; it is called on 94 * write error. 95 */ 96 extern void writerr(struct oblok *op, int count, int written);