1 /* 2 * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * By using this file, you agree to the terms and conditions set 11 * forth in the LICENSE file which can be found at the top level of 12 * the sendmail distribution. 13 */ 14 15 #include <sm/gen.h> 16 SM_RCSID("@(#)$Id: setvbuf.c,v 1.33 2013-11-22 20:51:43 ca Exp $") 17 #include <stdlib.h> 18 #include <errno.h> 19 #include <fcntl.h> 20 #include <sm/limits.h> 21 #include <sm/io.h> 22 #include <sm/heap.h> 23 #include <sm/assert.h> 24 #include <sm/conf.h> 25 #include "local.h" 26 27 /* 28 ** SM_IO_SETVBUF -- set the buffering type for a file 29 ** 30 ** Set one of the different kinds of buffering, optionally including 31 ** a buffer. 32 ** If 'size' is == 0 then an "optimal" size will be selected. 33 ** If 'buf' is == NULL then space will be allocated at 'size'. 34 ** 35 ** Parameters: 36 ** fp -- the file that buffering is to be changed for 37 ** timeout -- time allowed for completing the function 38 ** buf -- buffer to use 39 ** mode -- buffering method to use 40 ** size -- size of 'buf' 41 ** 42 ** Returns: 43 ** Failure: SM_IO_EOF 44 ** Success: 0 (zero) 45 */ 46 47 int 48 sm_io_setvbuf(fp, timeout, buf, mode, size) 49 SM_FILE_T *fp; 50 int timeout; 51 char *buf; 52 int mode; 53 size_t size; 54 { 55 int ret, flags; 56 size_t iosize; 57 int ttyflag; 58 int fd; 59 struct timeval to; 60 61 SM_REQUIRE_ISA(fp, SmFileMagic); 62 63 /* 64 ** Verify arguments. The `int' limit on `size' is due to this 65 ** particular implementation. Note, buf and size are ignored 66 ** when setting SM_IO_NBF. 67 */ 68 69 if (mode != SM_IO_NBF) 70 if ((mode != SM_IO_FBF && mode != SM_IO_LBF && 71 mode != SM_IO_NOW) || size > INT_MAX) 72 return SM_IO_EOF; 73 74 /* 75 ** Write current buffer, if any. Discard unread input (including 76 ** ungetc data), cancel line buffering, and free old buffer if 77 ** malloc()ed. We also clear any eof condition, as if this were 78 ** a seek. 79 */ 80 81 ret = 0; 82 SM_CONVERT_TIME(fp, fd, timeout, &to); 83 (void) sm_flush(fp, &timeout); 84 if (HASUB(fp)) 85 FREEUB(fp); 86 fp->f_r = fp->f_lbfsize = 0; 87 flags = fp->f_flags; 88 if (flags & SMMBF) 89 { 90 sm_free((void *) fp->f_bf.smb_base); 91 fp->f_bf.smb_base = NULL; 92 } 93 flags &= ~(SMLBF | SMNBF | SMMBF | SMOPT | SMNPT | SMFEOF | SMNOW | 94 SMFBF); 95 96 /* If setting unbuffered mode, skip all the hard work. */ 97 if (mode == SM_IO_NBF) 98 goto nbf; 99 100 /* 101 ** Find optimal I/O size for seek optimization. This also returns 102 ** a `tty flag' to suggest that we check isatty(fd), but we do not 103 ** care since our caller told us how to buffer. 104 */ 105 106 flags |= sm_whatbuf(fp, &iosize, &ttyflag); 107 if (size == 0) 108 { 109 buf = NULL; /* force local allocation */ 110 size = iosize; 111 } 112 113 /* Allocate buffer if needed. */ 114 if (buf == NULL) 115 { 116 if ((buf = sm_malloc(size)) == NULL) 117 { 118 /* 119 ** Unable to honor user's request. We will return 120 ** failure, but try again with file system size. 121 */ 122 123 ret = SM_IO_EOF; 124 if (size != iosize) 125 { 126 size = iosize; 127 buf = sm_malloc(size); 128 } 129 } 130 if (buf == NULL) 131 { 132 /* No luck; switch to unbuffered I/O. */ 133 nbf: 134 fp->f_flags = flags | SMNBF; 135 fp->f_w = 0; 136 fp->f_bf.smb_base = fp->f_p = fp->f_nbuf; 137 fp->f_bf.smb_size = 1; 138 return ret; 139 } 140 flags |= SMMBF; 141 } 142 143 /* 144 ** Kill any seek optimization if the buffer is not the 145 ** right size. 146 ** 147 ** SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)? 148 */ 149 150 if (size != iosize) 151 flags |= SMNPT; 152 153 /* 154 ** Fix up the SM_FILE_T fields, and set sm_cleanup for output flush on 155 ** exit (since we are buffered in some way). 156 */ 157 158 if (mode == SM_IO_LBF) 159 flags |= SMLBF; 160 else if (mode == SM_IO_NOW) 161 flags |= SMNOW; 162 else if (mode == SM_IO_FBF) 163 flags |= SMFBF; 164 fp->f_flags = flags; 165 fp->f_bf.smb_base = fp->f_p = (unsigned char *)buf; 166 fp->f_bf.smb_size = size; 167 /* fp->f_lbfsize is still 0 */ 168 if (flags & SMWR) 169 { 170 /* 171 ** Begin or continue writing: see sm_wsetup(). Note 172 ** that SMNBF is impossible (it was handled earlier). 173 */ 174 175 if (flags & SMLBF) 176 { 177 fp->f_w = 0; 178 fp->f_lbfsize = -fp->f_bf.smb_size; 179 } 180 else 181 fp->f_w = size; 182 } 183 else 184 { 185 /* begin/continue reading, or stay in intermediate state */ 186 fp->f_w = 0; 187 } 188 189 atexit(sm_cleanup); 190 return ret; 191 } 192