2
$Id: buffering_write.c,v 1.2 2004/12/19 01:43:38 rocky Exp $
4
Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
5
Copyright (C) 1998, 1999 Monty <xiphmont@mit.edu>
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License as
9
published by the Free Software Foundation; either version 2, or (at
10
your option) any later version.
12
This program is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
/* Eliminate teeny little writes. patch submitted by
23
Rob Ross <rbross@parl.ces.clemson.edu> --Monty 19991008 */
30
#define OUTBUFSZ 32*1024
33
#include "buffering_write.h"
36
/* GLOBALS FOR BUFFERING CALLS */
37
static int bw_fd = -1;
38
static long bw_pos = 0;
39
static char bw_outbuf[OUTBUFSZ];
44
blocking_write(int outf, char *buffer, long num){
45
long int words=0,temp;
48
temp=write(outf,buffer+words,num-words);
50
if(errno!=EINTR && errno!=EAGAIN)
59
/** buffering_write() - buffers data to a specified size before writing.
62
* - MUST CALL BUFFERING_CLOSE() WHEN FINISHED!!!
66
buffering_write(int fd, char *buffer, long num)
69
/* clean up after buffering for some other file */
70
if (bw_fd >= 0 && bw_pos > 0) {
71
if (blocking_write(bw_fd, bw_outbuf, bw_pos)) {
72
perror("write (in buffering_write, flushing)");
79
if (bw_pos + num > OUTBUFSZ) {
80
/* fill our buffer first, then write, then modify buffer and num */
81
memcpy(&bw_outbuf[bw_pos], buffer, OUTBUFSZ - bw_pos);
82
if (blocking_write(fd, bw_outbuf, OUTBUFSZ)) {
83
perror("write (in buffering_write, full buffer)");
86
num -= (OUTBUFSZ - bw_pos);
87
buffer += (OUTBUFSZ - bw_pos);
91
memcpy(&bw_outbuf[bw_pos], buffer, num);
97
/** buffering_close() - writes out remaining buffered data before
102
buffering_close(int fd)
104
if (fd == bw_fd && bw_pos > 0) {
105
/* write out remaining data and clean up */
106
if (blocking_write(fd, bw_outbuf, bw_pos)) {
107
perror("write (in buffering_close)");