ungetc.c revision 90792
150276Speter/* 276726Speter * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers. 350276Speter * All rights reserved. 450276Speter * Copyright (c) 1990, 1993 550276Speter * The Regents of the University of California. All rights reserved. 650276Speter * 750276Speter * This code is derived from software contributed to Berkeley by 850276Speter * Chris Torek. 950276Speter * 1050276Speter * By using this file, you agree to the terms and conditions set 1150276Speter * forth in the LICENSE file which can be found at the top level of 1250276Speter * the sendmail distribution. 1350276Speter */ 1450276Speter 1550276Speter#include <sm/gen.h> 1650276SpeterSM_IDSTR(id, "@(#)$Id: ungetc.c,v 1.28 2001/09/11 04:04:49 gshapiro Exp $") 1750276Speter 1850276Speter#include <stdlib.h> 1950276Speter#include <string.h> 2050276Speter#include <signal.h> 2150276Speter#include <sys/time.h> 2250276Speter#include <errno.h> 2350276Speter#include <sm/io.h> 2450276Speter#include <sm/heap.h> 2550276Speter#include <sm/assert.h> 2650276Speter#include <sm/conf.h> 2750276Speter#include "local.h" 2850276Speter 2950276Speter/* 3050276Speter** SM_SUBMORE_X -- expand ungetc buffer 3150276Speter** 3250276Speter** Expand the ungetc buffer `in place'. That is, adjust fp->f_p when 3350276Speter** the buffer moves, so that it points the same distance from the end, 3450276Speter** and move the bytes in the buffer around as necessary so that they 3550276Speter** are all at the end (stack-style). 3650276Speter** 3750276Speter** Parameters: 3850276Speter** fp -- the file pointer 3950276Speter** 4050276Speter** Results: 4150276Speter** none. 4276726Speter** 4350276Speter** Exceptions: 4476726Speter** F:sm_heap -- out of memory 4576726Speter*/ 4650276Speter 4750276Speterstatic void 4850276Spetersm_submore_x(fp) 4950276Speter register SM_FILE_T *fp; 5050276Speter{ 5150276Speter register int i; 5250276Speter register unsigned char *p; 5350276Speter 5450276Speter if (fp->f_ub.smb_base == fp->f_ubuf) 5550276Speter { 5650276Speter /* Get a buffer; f_ubuf is fixed size. */ 5776726Speter p = sm_malloc_x((size_t) SM_IO_BUFSIZ); 5850276Speter fp->f_ub.smb_base = p; 5950276Speter fp->f_ub.smb_size = SM_IO_BUFSIZ; 6050276Speter p += SM_IO_BUFSIZ - sizeof(fp->f_ubuf); 6150276Speter for (i = sizeof(fp->f_ubuf); --i >= 0;) 6250276Speter p[i] = fp->f_ubuf[i]; 6376726Speter fp->f_p = p; 64 return; 65 } 66 i = fp->f_ub.smb_size; 67 p = sm_realloc_x(fp->f_ub.smb_base, i << 1); 68 69 /* no overlap (hence can use memcpy) because we doubled the size */ 70 (void) memcpy((void *) (p + i), (void *) p, (size_t) i); 71 fp->f_p = p + i; 72 fp->f_ub.smb_base = p; 73 fp->f_ub.smb_size = i << 1; 74} 75 76/* 77** SM_IO_UNGETC -- place a character back into the buffer just read 78** 79** Parameters: 80** fp -- the file pointer affected 81** timeout -- time to complete ungetc 82** c -- the character to place back 83** 84** Results: 85** On success, returns value of character placed back, 0-255. 86** Returns SM_IO_EOF if c == SM_IO_EOF or if last operation 87** was a write and flush failed. 88** 89** Exceptions: 90** F:sm_heap -- out of memory 91*/ 92 93int 94sm_io_ungetc(fp, timeout, c) 95 register SM_FILE_T *fp; 96 int timeout; 97 int c; 98{ 99 SM_REQUIRE_ISA(fp, SmFileMagic); 100 if (c == SM_IO_EOF) 101 return SM_IO_EOF; 102 if (timeout == SM_TIME_IMMEDIATE) 103 { 104 /* 105 ** Ungetting the buffer will take time and we are wanted to 106 ** return immediately. So... 107 */ 108 109 errno = EAGAIN; 110 return SM_IO_EOF; 111 } 112 113 if (!Sm_IO_DidInit) 114 sm_init(); 115 if ((fp->f_flags & SMRD) == 0) 116 { 117 /* 118 ** Not already reading: no good unless reading-and-writing. 119 ** Otherwise, flush any current write stuff. 120 */ 121 122 if ((fp->f_flags & SMRW) == 0) 123 return SM_IO_EOF; 124 if (fp->f_flags & SMWR) 125 { 126 if (sm_flush(fp, &timeout)) 127 return SM_IO_EOF; 128 fp->f_flags &= ~SMWR; 129 fp->f_w = 0; 130 fp->f_lbfsize = 0; 131 } 132 fp->f_flags |= SMRD; 133 } 134 c = (unsigned char) c; 135 136 /* 137 ** If we are in the middle of ungetc'ing, just continue. 138 ** This may require expanding the current ungetc buffer. 139 */ 140 141 if (HASUB(fp)) 142 { 143 if (fp->f_r >= fp->f_ub.smb_size) 144 sm_submore_x(fp); 145 *--fp->f_p = c; 146 fp->f_r++; 147 return c; 148 } 149 fp->f_flags &= ~SMFEOF; 150 151 /* 152 ** If we can handle this by simply backing up, do so, 153 ** but never replace the original character. 154 ** (This makes sscanf() work when scanning `const' data.) 155 */ 156 157 if (fp->f_bf.smb_base != NULL && fp->f_p > fp->f_bf.smb_base && 158 fp->f_p[-1] == c) 159 { 160 fp->f_p--; 161 fp->f_r++; 162 return c; 163 } 164 165 /* 166 ** Create an ungetc buffer. 167 ** Initially, we will use the `reserve' buffer. 168 */ 169 170 fp->f_ur = fp->f_r; 171 fp->f_up = fp->f_p; 172 fp->f_ub.smb_base = fp->f_ubuf; 173 fp->f_ub.smb_size = sizeof(fp->f_ubuf); 174 fp->f_ubuf[sizeof(fp->f_ubuf) - 1] = c; 175 fp->f_p = &fp->f_ubuf[sizeof(fp->f_ubuf) - 1]; 176 fp->f_r = 1; 177 178 return c; 179} 180