2 * Copyright (c) 2000-2001, 2004 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.30 2005/06/14 23:07:20 ca Exp $")
25 #include <sm/assert.h>
29 static void sm_submore_x
__P((SM_FILE_T
*));
32 ** SM_SUBMORE_X -- expand ungetc buffer
34 ** Expand the ungetc buffer `in place'. That is, adjust fp->f_p when
35 ** the buffer moves, so that it points the same distance from the end,
36 ** and move the bytes in the buffer around as necessary so that they
37 ** are all at the end (stack-style).
40 ** fp -- the file pointer
46 ** F:sm_heap -- out of memory
54 register unsigned char *p
;
56 if (fp
->f_ub
.smb_base
== fp
->f_ubuf
)
58 /* Get a buffer; f_ubuf is fixed size. */
59 p
= sm_malloc_x((size_t) SM_IO_BUFSIZ
);
60 fp
->f_ub
.smb_base
= p
;
61 fp
->f_ub
.smb_size
= SM_IO_BUFSIZ
;
62 p
+= SM_IO_BUFSIZ
- sizeof(fp
->f_ubuf
);
63 for (i
= sizeof(fp
->f_ubuf
); --i
>= 0;)
68 i
= fp
->f_ub
.smb_size
;
69 p
= sm_realloc_x(fp
->f_ub
.smb_base
, i
<< 1);
71 /* no overlap (hence can use memcpy) because we doubled the size */
72 (void) memcpy((void *) (p
+ i
), (void *) p
, (size_t) i
);
74 fp
->f_ub
.smb_base
= p
;
75 fp
->f_ub
.smb_size
= i
<< 1;
79 ** SM_IO_UNGETC -- place a character back into the buffer just read
82 ** fp -- the file pointer affected
83 ** timeout -- time to complete ungetc
84 ** c -- the character to place back
87 ** On success, returns value of character placed back, 0-255.
88 ** Returns SM_IO_EOF if c == SM_IO_EOF or if last operation
89 ** was a write and flush failed.
92 ** F:sm_heap -- out of memory
96 sm_io_ungetc(fp
, timeout
, c
)
97 register SM_FILE_T
*fp
;
101 SM_REQUIRE_ISA(fp
, SmFileMagic
);
104 if (timeout
== SM_TIME_IMMEDIATE
)
107 ** Ungetting the buffer will take time and we are wanted to
108 ** return immediately. So...
117 if ((fp
->f_flags
& SMRD
) == 0)
120 ** Not already reading: no good unless reading-and-writing.
121 ** Otherwise, flush any current write stuff.
124 if ((fp
->f_flags
& SMRW
) == 0)
126 if (fp
->f_flags
& SMWR
)
128 if (sm_flush(fp
, &timeout
))
130 fp
->f_flags
&= ~SMWR
;
136 c
= (unsigned char) c
;
139 ** If we are in the middle of ungetc'ing, just continue.
140 ** This may require expanding the current ungetc buffer.
145 if (fp
->f_r
>= fp
->f_ub
.smb_size
)
151 fp
->f_flags
&= ~SMFEOF
;
154 ** If we can handle this by simply backing up, do so,
155 ** but never replace the original character.
156 ** (This makes sscanf() work when scanning `const' data.)
159 if (fp
->f_bf
.smb_base
!= NULL
&& fp
->f_p
> fp
->f_bf
.smb_base
&&
168 ** Create an ungetc buffer.
169 ** Initially, we will use the `reserve' buffer.
174 fp
->f_ub
.smb_base
= fp
->f_ubuf
;
175 fp
->f_ub
.smb_size
= sizeof(fp
->f_ubuf
);
176 fp
->f_ubuf
[sizeof(fp
->f_ubuf
) - 1] = c
;
177 fp
->f_p
= &fp
->f_ubuf
[sizeof(fp
->f_ubuf
) - 1];