140266059SGregory Neil Shapiro /* 25dd76dd0SGregory Neil Shapiro * Copyright (c) 2000-2002 Proofpoint, Inc. and its suppliers. 340266059SGregory Neil Shapiro * All rights reserved. 440266059SGregory Neil Shapiro * Copyright (c) 1990, 1993 540266059SGregory Neil Shapiro * The Regents of the University of California. All rights reserved. 640266059SGregory Neil Shapiro * 740266059SGregory Neil Shapiro * This code is derived from software contributed to Berkeley by 840266059SGregory Neil Shapiro * Chris Torek. 940266059SGregory Neil Shapiro * 1040266059SGregory Neil Shapiro * By using this file, you agree to the terms and conditions set 1140266059SGregory Neil Shapiro * forth in the LICENSE file which can be found at the top level of 1240266059SGregory Neil Shapiro * the sendmail distribution. 1340266059SGregory Neil Shapiro */ 1440266059SGregory Neil Shapiro 1540266059SGregory Neil Shapiro #include <sm/gen.h> 16*4313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: wsetup.c,v 1.21 2013-11-22 20:51:44 ca Exp $") 1740266059SGregory Neil Shapiro #include <stdlib.h> 1840266059SGregory Neil Shapiro #include <errno.h> 1940266059SGregory Neil Shapiro #include <sm/io.h> 2040266059SGregory Neil Shapiro #include "local.h" 2140266059SGregory Neil Shapiro 2240266059SGregory Neil Shapiro /* 23605302a5SGregory Neil Shapiro ** SM_WSETUP -- check writing is safe 2440266059SGregory Neil Shapiro ** 2540266059SGregory Neil Shapiro ** Various output routines call wsetup to be sure it is safe to write, 2640266059SGregory Neil Shapiro ** because either flags does not include SMMWR, or buf is NULL. 2740266059SGregory Neil Shapiro ** Used in the macro "cantwrite" found in "local.h". 2840266059SGregory Neil Shapiro ** 2940266059SGregory Neil Shapiro ** Parameters: 3040266059SGregory Neil Shapiro ** fp -- the file pointer 3140266059SGregory Neil Shapiro ** 3240266059SGregory Neil Shapiro ** Results: 3340266059SGregory Neil Shapiro ** Failure: SM_IO_EOF and sets errno 3440266059SGregory Neil Shapiro ** Success: 0 (zero) 3540266059SGregory Neil Shapiro */ 3640266059SGregory Neil Shapiro 3740266059SGregory Neil Shapiro int 3840266059SGregory Neil Shapiro sm_wsetup(fp) 3940266059SGregory Neil Shapiro register SM_FILE_T *fp; 4040266059SGregory Neil Shapiro { 4140266059SGregory Neil Shapiro /* make sure stdio is set up */ 4240266059SGregory Neil Shapiro if (!Sm_IO_DidInit) 4340266059SGregory Neil Shapiro sm_init(); 4440266059SGregory Neil Shapiro 4540266059SGregory Neil Shapiro /* If we are not writing, we had better be reading and writing. */ 4640266059SGregory Neil Shapiro if ((fp->f_flags & SMWR) == 0) 4740266059SGregory Neil Shapiro { 4840266059SGregory Neil Shapiro if ((fp->f_flags & SMRW) == 0) 4940266059SGregory Neil Shapiro { 5040266059SGregory Neil Shapiro errno = EBADF; 5140266059SGregory Neil Shapiro return SM_IO_EOF; 5240266059SGregory Neil Shapiro } 5340266059SGregory Neil Shapiro if (fp->f_flags & SMRD) 5440266059SGregory Neil Shapiro { 5540266059SGregory Neil Shapiro /* clobber any ungetc data */ 5640266059SGregory Neil Shapiro if (HASUB(fp)) 5740266059SGregory Neil Shapiro FREEUB(fp); 58605302a5SGregory Neil Shapiro 59605302a5SGregory Neil Shapiro /* discard read buffer */ 6040266059SGregory Neil Shapiro fp->f_flags &= ~(SMRD|SMFEOF); 6140266059SGregory Neil Shapiro fp->f_r = 0; 6240266059SGregory Neil Shapiro fp->f_p = fp->f_bf.smb_base; 6340266059SGregory Neil Shapiro } 6440266059SGregory Neil Shapiro fp->f_flags |= SMWR; 6540266059SGregory Neil Shapiro } 6640266059SGregory Neil Shapiro 6740266059SGregory Neil Shapiro /* Make a buffer if necessary, then set w. */ 6840266059SGregory Neil Shapiro if (fp->f_bf.smb_base == NULL) 6940266059SGregory Neil Shapiro sm_makebuf(fp); 7040266059SGregory Neil Shapiro if (fp->f_flags & SMLBF) 7140266059SGregory Neil Shapiro { 7240266059SGregory Neil Shapiro /* 7340266059SGregory Neil Shapiro ** It is line buffered, so make lbfsize be -bufsize 7440266059SGregory Neil Shapiro ** for the sm_putc() macro. We will change lbfsize back 7540266059SGregory Neil Shapiro ** to 0 whenever we turn off SMWR. 7640266059SGregory Neil Shapiro */ 7740266059SGregory Neil Shapiro 7840266059SGregory Neil Shapiro fp->f_w = 0; 7940266059SGregory Neil Shapiro fp->f_lbfsize = -fp->f_bf.smb_size; 8040266059SGregory Neil Shapiro } 8140266059SGregory Neil Shapiro else 8240266059SGregory Neil Shapiro fp->f_w = fp->f_flags & SMNBF ? 0 : fp->f_bf.smb_size; 8340266059SGregory Neil Shapiro return 0; 8440266059SGregory Neil Shapiro } 85