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 #pragma ident "%Z%%M% %I% %E% SMI" 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate #include <sm/gen.h> 18*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: wbuf.c,v 1.19 2001/03/05 03:22:41 ca Exp $") 19*7c478bd9Sstevel@tonic-gate #include <errno.h> 20*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 21*7c478bd9Sstevel@tonic-gate #include "local.h" 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate /* Note: This function is called from a macro located in <sm/io.h> */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate /* 26*7c478bd9Sstevel@tonic-gate ** SM_WBUF -- write character to and flush (likely now full) buffer 27*7c478bd9Sstevel@tonic-gate ** 28*7c478bd9Sstevel@tonic-gate ** Write the given character into the (probably full) buffer for 29*7c478bd9Sstevel@tonic-gate ** the given file. Flush the buffer out if it is or becomes full, 30*7c478bd9Sstevel@tonic-gate ** or if c=='\n' and the file is line buffered. 31*7c478bd9Sstevel@tonic-gate ** 32*7c478bd9Sstevel@tonic-gate ** Parameters: 33*7c478bd9Sstevel@tonic-gate ** fp -- the file pointer 34*7c478bd9Sstevel@tonic-gate ** timeout -- time to complete operation (milliseconds) 35*7c478bd9Sstevel@tonic-gate ** c -- int representation of the character to add 36*7c478bd9Sstevel@tonic-gate ** 37*7c478bd9Sstevel@tonic-gate ** Results: 38*7c478bd9Sstevel@tonic-gate ** Failure: -1 and sets errno 39*7c478bd9Sstevel@tonic-gate ** Success: int value of 'c' 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate int 43*7c478bd9Sstevel@tonic-gate sm_wbuf(fp, timeout, c) 44*7c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 45*7c478bd9Sstevel@tonic-gate int timeout; 46*7c478bd9Sstevel@tonic-gate register int c; 47*7c478bd9Sstevel@tonic-gate { 48*7c478bd9Sstevel@tonic-gate register int n; 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate /* 51*7c478bd9Sstevel@tonic-gate ** In case we cannot write, or longjmp takes us out early, 52*7c478bd9Sstevel@tonic-gate ** make sure w is 0 (if fully- or un-buffered) or -bf.smb_size 53*7c478bd9Sstevel@tonic-gate ** (if line buffered) so that we will get called again. 54*7c478bd9Sstevel@tonic-gate ** If we did not do this, a sufficient number of sm_io_putc() 55*7c478bd9Sstevel@tonic-gate ** calls might wrap w from negative to positive. 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate fp->f_w = fp->f_lbfsize; 59*7c478bd9Sstevel@tonic-gate if (cantwrite(fp)) 60*7c478bd9Sstevel@tonic-gate { 61*7c478bd9Sstevel@tonic-gate errno = EBADF; 62*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate c = (unsigned char)c; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate /* 67*7c478bd9Sstevel@tonic-gate ** If it is completely full, flush it out. Then, in any case, 68*7c478bd9Sstevel@tonic-gate ** stuff c into the buffer. If this causes the buffer to fill 69*7c478bd9Sstevel@tonic-gate ** completely, or if c is '\n' and the file is line buffered, 70*7c478bd9Sstevel@tonic-gate ** flush it (perhaps a second time). The second flush will always 71*7c478bd9Sstevel@tonic-gate ** happen on unbuffered streams, where bf.smb_size==1; sm_io_flush() 72*7c478bd9Sstevel@tonic-gate ** guarantees that sm_io_putc() will always call sm_wbuf() by setting 73*7c478bd9Sstevel@tonic-gate ** w to 0, so we need not do anything else. 74*7c478bd9Sstevel@tonic-gate ** Note for the timeout, only one of the sm_io_flush's will get called. 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate n = fp->f_p - fp->f_bf.smb_base; 78*7c478bd9Sstevel@tonic-gate if (n >= fp->f_bf.smb_size) 79*7c478bd9Sstevel@tonic-gate { 80*7c478bd9Sstevel@tonic-gate if (sm_io_flush(fp, timeout)) 81*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; /* sm_io_flush() sets errno */ 82*7c478bd9Sstevel@tonic-gate n = 0; 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate fp->f_w--; 85*7c478bd9Sstevel@tonic-gate *fp->f_p++ = c; 86*7c478bd9Sstevel@tonic-gate if (++n == fp->f_bf.smb_size || (fp->f_flags & SMLBF && c == '\n')) 87*7c478bd9Sstevel@tonic-gate if (sm_io_flush(fp, timeout)) 88*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; /* sm_io_flush() sets errno */ 89*7c478bd9Sstevel@tonic-gate return c; 90*7c478bd9Sstevel@tonic-gate } 91