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