2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
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.
16 SM_IDSTR(id, "@(#)$Id: ungetc.c,v 1.28 2001/09/11 04:04:49 gshapiro Exp $")
25 #include <sm/assert.h>
30 ** SM_SUBMORE_X -- expand ungetc buffer
32 ** Expand the ungetc buffer `in place'. That is, adjust fp->f_p when
33 ** the buffer moves, so that it points the same distance from the end,
34 ** and move the bytes in the buffer around as necessary so that they
35 ** are all at the end (stack-style).
38 ** fp -- the file pointer
44 ** F:sm_heap -- out of memory
49 register SM_FILE_T *fp;
52 register unsigned char *p;
54 if (fp->f_ub.smb_base == fp->f_ubuf)
56 /* Get a buffer; f_ubuf is fixed size. */
57 p = sm_malloc_x((size_t) SM_IO_BUFSIZ);
58 fp->f_ub.smb_base = p;
59 fp->f_ub.smb_size = SM_IO_BUFSIZ;
60 p += SM_IO_BUFSIZ - sizeof(fp->f_ubuf);
61 for (i = sizeof(fp->f_ubuf); --i >= 0;)
66 i = fp->f_ub.smb_size;
67 p = sm_realloc_x(fp->f_ub.smb_base, i << 1);
69 /* no overlap (hence can use memcpy) because we doubled the size */
70 (void) memcpy((void *) (p + i), (void *) p, (size_t) i);
72 fp->f_ub.smb_base = p;
73 fp->f_ub.smb_size = i << 1;
77 ** SM_IO_UNGETC -- place a character back into the buffer just read
80 ** fp -- the file pointer affected
81 ** timeout -- time to complete ungetc
82 ** c -- the character to place back
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.
90 ** F:sm_heap -- out of memory
94 sm_io_ungetc(fp, timeout, c)
95 register SM_FILE_T *fp;
99 SM_REQUIRE_ISA(fp, SmFileMagic);
102 if (timeout == SM_TIME_IMMEDIATE)
105 ** Ungetting the buffer will take time and we are wanted to
106 ** return immediately. So...
115 if ((fp->f_flags & SMRD) == 0)
118 ** Not already reading: no good unless reading-and-writing.
119 ** Otherwise, flush any current write stuff.
122 if ((fp->f_flags & SMRW) == 0)
124 if (fp->f_flags & SMWR)
126 if (sm_flush(fp, &timeout))
128 fp->f_flags &= ~SMWR;
134 c = (unsigned char) c;
137 ** If we are in the middle of ungetc'ing, just continue.
138 ** This may require expanding the current ungetc buffer.
143 if (fp->f_r >= fp->f_ub.smb_size)
149 fp->f_flags &= ~SMFEOF;
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.)
157 if (fp->f_bf.smb_base != NULL && fp->f_p > fp->f_bf.smb_base &&
166 ** Create an ungetc buffer.
167 ** Initially, we will use the `reserve' buffer.
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];