1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers. 3*7c478bd9Sstevel@tonic-gate * All rights reserved. 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1993 5*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 8*7c478bd9Sstevel@tonic-gate * Chris Torek. 9*7c478bd9Sstevel@tonic-gate * 10*7c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 11*7c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 12*7c478bd9Sstevel@tonic-gate * the sendmail distribution. 13*7c478bd9Sstevel@tonic-gate */ 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #include <sm/gen.h> 16*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: wbuf.c,v 1.19 2001/03/05 03:22:41 ca Exp $") 17*7c478bd9Sstevel@tonic-gate #include <errno.h> 18*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 19*7c478bd9Sstevel@tonic-gate #include "local.h" 20*7c478bd9Sstevel@tonic-gate 21*7c478bd9Sstevel@tonic-gate /* Note: This function is called from a macro located in <sm/io.h> */ 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate /* 24*7c478bd9Sstevel@tonic-gate ** SM_WBUF -- write character to and flush (likely now full) buffer 25*7c478bd9Sstevel@tonic-gate ** 26*7c478bd9Sstevel@tonic-gate ** Write the given character into the (probably full) buffer for 27*7c478bd9Sstevel@tonic-gate ** the given file. Flush the buffer out if it is or becomes full, 28*7c478bd9Sstevel@tonic-gate ** or if c=='\n' and the file is line buffered. 29*7c478bd9Sstevel@tonic-gate ** 30*7c478bd9Sstevel@tonic-gate ** Parameters: 31*7c478bd9Sstevel@tonic-gate ** fp -- the file pointer 32*7c478bd9Sstevel@tonic-gate ** timeout -- time to complete operation (milliseconds) 33*7c478bd9Sstevel@tonic-gate ** c -- int representation of the character to add 34*7c478bd9Sstevel@tonic-gate ** 35*7c478bd9Sstevel@tonic-gate ** Results: 36*7c478bd9Sstevel@tonic-gate ** Failure: -1 and sets errno 37*7c478bd9Sstevel@tonic-gate ** Success: int value of 'c' 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate int 41*7c478bd9Sstevel@tonic-gate sm_wbuf(fp, timeout, c) 42*7c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 43*7c478bd9Sstevel@tonic-gate int timeout; 44*7c478bd9Sstevel@tonic-gate register int c; 45*7c478bd9Sstevel@tonic-gate { 46*7c478bd9Sstevel@tonic-gate register int n; 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate ** In case we cannot write, or longjmp takes us out early, 50*7c478bd9Sstevel@tonic-gate ** make sure w is 0 (if fully- or un-buffered) or -bf.smb_size 51*7c478bd9Sstevel@tonic-gate ** (if line buffered) so that we will get called again. 52*7c478bd9Sstevel@tonic-gate ** If we did not do this, a sufficient number of sm_io_putc() 53*7c478bd9Sstevel@tonic-gate ** calls might wrap w from negative to positive. 54*7c478bd9Sstevel@tonic-gate */ 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate fp->f_w = fp->f_lbfsize; 57*7c478bd9Sstevel@tonic-gate if (cantwrite(fp)) 58*7c478bd9Sstevel@tonic-gate { 59*7c478bd9Sstevel@tonic-gate errno = EBADF; 60*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate c = (unsigned char)c; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* 65*7c478bd9Sstevel@tonic-gate ** If it is completely full, flush it out. Then, in any case, 66*7c478bd9Sstevel@tonic-gate ** stuff c into the buffer. If this causes the buffer to fill 67*7c478bd9Sstevel@tonic-gate ** completely, or if c is '\n' and the file is line buffered, 68*7c478bd9Sstevel@tonic-gate ** flush it (perhaps a second time). The second flush will always 69*7c478bd9Sstevel@tonic-gate ** happen on unbuffered streams, where bf.smb_size==1; sm_io_flush() 70*7c478bd9Sstevel@tonic-gate ** guarantees that sm_io_putc() will always call sm_wbuf() by setting 71*7c478bd9Sstevel@tonic-gate ** w to 0, so we need not do anything else. 72*7c478bd9Sstevel@tonic-gate ** Note for the timeout, only one of the sm_io_flush's will get called. 73*7c478bd9Sstevel@tonic-gate */ 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate n = fp->f_p - fp->f_bf.smb_base; 76*7c478bd9Sstevel@tonic-gate if (n >= fp->f_bf.smb_size) 77*7c478bd9Sstevel@tonic-gate { 78*7c478bd9Sstevel@tonic-gate if (sm_io_flush(fp, timeout)) 79*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; /* sm_io_flush() sets errno */ 80*7c478bd9Sstevel@tonic-gate n = 0; 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate fp->f_w--; 83*7c478bd9Sstevel@tonic-gate *fp->f_p++ = c; 84*7c478bd9Sstevel@tonic-gate if (++n == fp->f_bf.smb_size || (fp->f_flags & SMLBF && c == '\n')) 85*7c478bd9Sstevel@tonic-gate if (sm_io_flush(fp, timeout)) 86*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; /* sm_io_flush() sets errno */ 87*7c478bd9Sstevel@tonic-gate return c; 88*7c478bd9Sstevel@tonic-gate } 89