1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000-2002 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: wsetup.c,v 1.20 2002/02/07 18:02:45 ca Exp $") 17*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 18*7c478bd9Sstevel@tonic-gate #include <errno.h> 19*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 20*7c478bd9Sstevel@tonic-gate #include "local.h" 21*7c478bd9Sstevel@tonic-gate 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate ** SM_WSETUP -- check writing is safe 24*7c478bd9Sstevel@tonic-gate ** 25*7c478bd9Sstevel@tonic-gate ** Various output routines call wsetup to be sure it is safe to write, 26*7c478bd9Sstevel@tonic-gate ** because either flags does not include SMMWR, or buf is NULL. 27*7c478bd9Sstevel@tonic-gate ** Used in the macro "cantwrite" found in "local.h". 28*7c478bd9Sstevel@tonic-gate ** 29*7c478bd9Sstevel@tonic-gate ** Parameters: 30*7c478bd9Sstevel@tonic-gate ** fp -- the file pointer 31*7c478bd9Sstevel@tonic-gate ** 32*7c478bd9Sstevel@tonic-gate ** Results: 33*7c478bd9Sstevel@tonic-gate ** Failure: SM_IO_EOF and sets errno 34*7c478bd9Sstevel@tonic-gate ** Success: 0 (zero) 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate int 38*7c478bd9Sstevel@tonic-gate sm_wsetup(fp) 39*7c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 40*7c478bd9Sstevel@tonic-gate { 41*7c478bd9Sstevel@tonic-gate /* make sure stdio is set up */ 42*7c478bd9Sstevel@tonic-gate if (!Sm_IO_DidInit) 43*7c478bd9Sstevel@tonic-gate sm_init(); 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate /* If we are not writing, we had better be reading and writing. */ 46*7c478bd9Sstevel@tonic-gate if ((fp->f_flags & SMWR) == 0) 47*7c478bd9Sstevel@tonic-gate { 48*7c478bd9Sstevel@tonic-gate if ((fp->f_flags & SMRW) == 0) 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate errno = EBADF; 51*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 52*7c478bd9Sstevel@tonic-gate } 53*7c478bd9Sstevel@tonic-gate if (fp->f_flags & SMRD) 54*7c478bd9Sstevel@tonic-gate { 55*7c478bd9Sstevel@tonic-gate /* clobber any ungetc data */ 56*7c478bd9Sstevel@tonic-gate if (HASUB(fp)) 57*7c478bd9Sstevel@tonic-gate FREEUB(fp); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* discard read buffer */ 60*7c478bd9Sstevel@tonic-gate fp->f_flags &= ~(SMRD|SMFEOF); 61*7c478bd9Sstevel@tonic-gate fp->f_r = 0; 62*7c478bd9Sstevel@tonic-gate fp->f_p = fp->f_bf.smb_base; 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate fp->f_flags |= SMWR; 65*7c478bd9Sstevel@tonic-gate } 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* Make a buffer if necessary, then set w. */ 68*7c478bd9Sstevel@tonic-gate if (fp->f_bf.smb_base == NULL) 69*7c478bd9Sstevel@tonic-gate sm_makebuf(fp); 70*7c478bd9Sstevel@tonic-gate if (fp->f_flags & SMLBF) 71*7c478bd9Sstevel@tonic-gate { 72*7c478bd9Sstevel@tonic-gate /* 73*7c478bd9Sstevel@tonic-gate ** It is line buffered, so make lbfsize be -bufsize 74*7c478bd9Sstevel@tonic-gate ** for the sm_putc() macro. We will change lbfsize back 75*7c478bd9Sstevel@tonic-gate ** to 0 whenever we turn off SMWR. 76*7c478bd9Sstevel@tonic-gate */ 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate fp->f_w = 0; 79*7c478bd9Sstevel@tonic-gate fp->f_lbfsize = -fp->f_bf.smb_size; 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate else 82*7c478bd9Sstevel@tonic-gate fp->f_w = fp->f_flags & SMNBF ? 0 : fp->f_bf.smb_size; 83*7c478bd9Sstevel@tonic-gate return 0; 84*7c478bd9Sstevel@tonic-gate } 85