1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000-2001, 2004 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_IDSTR(id, "@(#)$Id: ungetc.c,v 1.29 2004/08/03 20:54:49 ca Exp $") 19*7c478bd9Sstevel@tonic-gate 20*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 21*7c478bd9Sstevel@tonic-gate #include <string.h> 22*7c478bd9Sstevel@tonic-gate #include <signal.h> 23*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 24*7c478bd9Sstevel@tonic-gate #include <errno.h> 25*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 26*7c478bd9Sstevel@tonic-gate #include <sm/heap.h> 27*7c478bd9Sstevel@tonic-gate #include <sm/assert.h> 28*7c478bd9Sstevel@tonic-gate #include <sm/conf.h> 29*7c478bd9Sstevel@tonic-gate #include "local.h" 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate static void sm_submore_x __P((SM_FILE_T *)); 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate ** SM_SUBMORE_X -- expand ungetc buffer 35*7c478bd9Sstevel@tonic-gate ** 36*7c478bd9Sstevel@tonic-gate ** Expand the ungetc buffer `in place'. That is, adjust fp->f_p when 37*7c478bd9Sstevel@tonic-gate ** the buffer moves, so that it points the same distance from the end, 38*7c478bd9Sstevel@tonic-gate ** and move the bytes in the buffer around as necessary so that they 39*7c478bd9Sstevel@tonic-gate ** are all at the end (stack-style). 40*7c478bd9Sstevel@tonic-gate ** 41*7c478bd9Sstevel@tonic-gate ** Parameters: 42*7c478bd9Sstevel@tonic-gate ** fp -- the file pointer 43*7c478bd9Sstevel@tonic-gate ** 44*7c478bd9Sstevel@tonic-gate ** Results: 45*7c478bd9Sstevel@tonic-gate ** none. 46*7c478bd9Sstevel@tonic-gate ** 47*7c478bd9Sstevel@tonic-gate ** Exceptions: 48*7c478bd9Sstevel@tonic-gate ** F:sm_heap -- out of memory 49*7c478bd9Sstevel@tonic-gate */ 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate static void 52*7c478bd9Sstevel@tonic-gate sm_submore_x(fp) 53*7c478bd9Sstevel@tonic-gate SM_FILE_T *fp; 54*7c478bd9Sstevel@tonic-gate { 55*7c478bd9Sstevel@tonic-gate register int i; 56*7c478bd9Sstevel@tonic-gate register unsigned char *p; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate if (fp->f_ub.smb_base == fp->f_ubuf) 59*7c478bd9Sstevel@tonic-gate { 60*7c478bd9Sstevel@tonic-gate /* Get a buffer; f_ubuf is fixed size. */ 61*7c478bd9Sstevel@tonic-gate p = sm_malloc_x((size_t) SM_IO_BUFSIZ); 62*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_base = p; 63*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_size = SM_IO_BUFSIZ; 64*7c478bd9Sstevel@tonic-gate p += SM_IO_BUFSIZ - sizeof(fp->f_ubuf); 65*7c478bd9Sstevel@tonic-gate for (i = sizeof(fp->f_ubuf); --i >= 0;) 66*7c478bd9Sstevel@tonic-gate p[i] = fp->f_ubuf[i]; 67*7c478bd9Sstevel@tonic-gate fp->f_p = p; 68*7c478bd9Sstevel@tonic-gate return; 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate i = fp->f_ub.smb_size; 71*7c478bd9Sstevel@tonic-gate p = sm_realloc_x(fp->f_ub.smb_base, i << 1); 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* no overlap (hence can use memcpy) because we doubled the size */ 74*7c478bd9Sstevel@tonic-gate (void) memcpy((void *) (p + i), (void *) p, (size_t) i); 75*7c478bd9Sstevel@tonic-gate fp->f_p = p + i; 76*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_base = p; 77*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_size = i << 1; 78*7c478bd9Sstevel@tonic-gate } 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate /* 81*7c478bd9Sstevel@tonic-gate ** SM_IO_UNGETC -- place a character back into the buffer just read 82*7c478bd9Sstevel@tonic-gate ** 83*7c478bd9Sstevel@tonic-gate ** Parameters: 84*7c478bd9Sstevel@tonic-gate ** fp -- the file pointer affected 85*7c478bd9Sstevel@tonic-gate ** timeout -- time to complete ungetc 86*7c478bd9Sstevel@tonic-gate ** c -- the character to place back 87*7c478bd9Sstevel@tonic-gate ** 88*7c478bd9Sstevel@tonic-gate ** Results: 89*7c478bd9Sstevel@tonic-gate ** On success, returns value of character placed back, 0-255. 90*7c478bd9Sstevel@tonic-gate ** Returns SM_IO_EOF if c == SM_IO_EOF or if last operation 91*7c478bd9Sstevel@tonic-gate ** was a write and flush failed. 92*7c478bd9Sstevel@tonic-gate ** 93*7c478bd9Sstevel@tonic-gate ** Exceptions: 94*7c478bd9Sstevel@tonic-gate ** F:sm_heap -- out of memory 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate int 98*7c478bd9Sstevel@tonic-gate sm_io_ungetc(fp, timeout, c) 99*7c478bd9Sstevel@tonic-gate register SM_FILE_T *fp; 100*7c478bd9Sstevel@tonic-gate int timeout; 101*7c478bd9Sstevel@tonic-gate int c; 102*7c478bd9Sstevel@tonic-gate { 103*7c478bd9Sstevel@tonic-gate SM_REQUIRE_ISA(fp, SmFileMagic); 104*7c478bd9Sstevel@tonic-gate if (c == SM_IO_EOF) 105*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 106*7c478bd9Sstevel@tonic-gate if (timeout == SM_TIME_IMMEDIATE) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate /* 109*7c478bd9Sstevel@tonic-gate ** Ungetting the buffer will take time and we are wanted to 110*7c478bd9Sstevel@tonic-gate ** return immediately. So... 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate errno = EAGAIN; 114*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate if (!Sm_IO_DidInit) 118*7c478bd9Sstevel@tonic-gate sm_init(); 119*7c478bd9Sstevel@tonic-gate if ((fp->f_flags & SMRD) == 0) 120*7c478bd9Sstevel@tonic-gate { 121*7c478bd9Sstevel@tonic-gate /* 122*7c478bd9Sstevel@tonic-gate ** Not already reading: no good unless reading-and-writing. 123*7c478bd9Sstevel@tonic-gate ** Otherwise, flush any current write stuff. 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate if ((fp->f_flags & SMRW) == 0) 127*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 128*7c478bd9Sstevel@tonic-gate if (fp->f_flags & SMWR) 129*7c478bd9Sstevel@tonic-gate { 130*7c478bd9Sstevel@tonic-gate if (sm_flush(fp, &timeout)) 131*7c478bd9Sstevel@tonic-gate return SM_IO_EOF; 132*7c478bd9Sstevel@tonic-gate fp->f_flags &= ~SMWR; 133*7c478bd9Sstevel@tonic-gate fp->f_w = 0; 134*7c478bd9Sstevel@tonic-gate fp->f_lbfsize = 0; 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate fp->f_flags |= SMRD; 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate c = (unsigned char) c; 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate /* 141*7c478bd9Sstevel@tonic-gate ** If we are in the middle of ungetc'ing, just continue. 142*7c478bd9Sstevel@tonic-gate ** This may require expanding the current ungetc buffer. 143*7c478bd9Sstevel@tonic-gate */ 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate if (HASUB(fp)) 146*7c478bd9Sstevel@tonic-gate { 147*7c478bd9Sstevel@tonic-gate if (fp->f_r >= fp->f_ub.smb_size) 148*7c478bd9Sstevel@tonic-gate sm_submore_x(fp); 149*7c478bd9Sstevel@tonic-gate *--fp->f_p = c; 150*7c478bd9Sstevel@tonic-gate fp->f_r++; 151*7c478bd9Sstevel@tonic-gate return c; 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate fp->f_flags &= ~SMFEOF; 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* 156*7c478bd9Sstevel@tonic-gate ** If we can handle this by simply backing up, do so, 157*7c478bd9Sstevel@tonic-gate ** but never replace the original character. 158*7c478bd9Sstevel@tonic-gate ** (This makes sscanf() work when scanning `const' data.) 159*7c478bd9Sstevel@tonic-gate */ 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate if (fp->f_bf.smb_base != NULL && fp->f_p > fp->f_bf.smb_base && 162*7c478bd9Sstevel@tonic-gate fp->f_p[-1] == c) 163*7c478bd9Sstevel@tonic-gate { 164*7c478bd9Sstevel@tonic-gate fp->f_p--; 165*7c478bd9Sstevel@tonic-gate fp->f_r++; 166*7c478bd9Sstevel@tonic-gate return c; 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate /* 170*7c478bd9Sstevel@tonic-gate ** Create an ungetc buffer. 171*7c478bd9Sstevel@tonic-gate ** Initially, we will use the `reserve' buffer. 172*7c478bd9Sstevel@tonic-gate */ 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate fp->f_ur = fp->f_r; 175*7c478bd9Sstevel@tonic-gate fp->f_up = fp->f_p; 176*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_base = fp->f_ubuf; 177*7c478bd9Sstevel@tonic-gate fp->f_ub.smb_size = sizeof(fp->f_ubuf); 178*7c478bd9Sstevel@tonic-gate fp->f_ubuf[sizeof(fp->f_ubuf) - 1] = c; 179*7c478bd9Sstevel@tonic-gate fp->f_p = &fp->f_ubuf[sizeof(fp->f_ubuf) - 1]; 180*7c478bd9Sstevel@tonic-gate fp->f_r = 1; 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate return c; 183*7c478bd9Sstevel@tonic-gate } 184