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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
18 SM_IDSTR(id
, "@(#)$Id: ungetc.c,v 1.30 2005/06/14 23:07:20 ca Exp $")
27 #include <sm/assert.h>
31 static void sm_submore_x
__P((SM_FILE_T
*));
34 ** SM_SUBMORE_X -- expand ungetc buffer
36 ** Expand the ungetc buffer `in place'. That is, adjust fp->f_p when
37 ** the buffer moves, so that it points the same distance from the end,
38 ** and move the bytes in the buffer around as necessary so that they
39 ** are all at the end (stack-style).
42 ** fp -- the file pointer
48 ** F:sm_heap -- out of memory
56 register unsigned char *p
;
58 if (fp
->f_ub
.smb_base
== fp
->f_ubuf
)
60 /* Get a buffer; f_ubuf is fixed size. */
61 p
= sm_malloc_x((size_t) SM_IO_BUFSIZ
);
62 fp
->f_ub
.smb_base
= p
;
63 fp
->f_ub
.smb_size
= SM_IO_BUFSIZ
;
64 p
+= SM_IO_BUFSIZ
- sizeof(fp
->f_ubuf
);
65 for (i
= sizeof(fp
->f_ubuf
); --i
>= 0;)
70 i
= fp
->f_ub
.smb_size
;
71 p
= sm_realloc_x(fp
->f_ub
.smb_base
, i
<< 1);
73 /* no overlap (hence can use memcpy) because we doubled the size */
74 (void) memcpy((void *) (p
+ i
), (void *) p
, (size_t) i
);
76 fp
->f_ub
.smb_base
= p
;
77 fp
->f_ub
.smb_size
= i
<< 1;
81 ** SM_IO_UNGETC -- place a character back into the buffer just read
84 ** fp -- the file pointer affected
85 ** timeout -- time to complete ungetc
86 ** c -- the character to place back
89 ** On success, returns value of character placed back, 0-255.
90 ** Returns SM_IO_EOF if c == SM_IO_EOF or if last operation
91 ** was a write and flush failed.
94 ** F:sm_heap -- out of memory
98 sm_io_ungetc(fp
, timeout
, c
)
99 register SM_FILE_T
*fp
;
103 SM_REQUIRE_ISA(fp
, SmFileMagic
);
106 if (timeout
== SM_TIME_IMMEDIATE
)
109 ** Ungetting the buffer will take time and we are wanted to
110 ** return immediately. So...
119 if ((fp
->f_flags
& SMRD
) == 0)
122 ** Not already reading: no good unless reading-and-writing.
123 ** Otherwise, flush any current write stuff.
126 if ((fp
->f_flags
& SMRW
) == 0)
128 if (fp
->f_flags
& SMWR
)
130 if (sm_flush(fp
, &timeout
))
132 fp
->f_flags
&= ~SMWR
;
138 c
= (unsigned char) c
;
141 ** If we are in the middle of ungetc'ing, just continue.
142 ** This may require expanding the current ungetc buffer.
147 if (fp
->f_r
>= fp
->f_ub
.smb_size
)
153 fp
->f_flags
&= ~SMFEOF
;
156 ** If we can handle this by simply backing up, do so,
157 ** but never replace the original character.
158 ** (This makes sscanf() work when scanning `const' data.)
161 if (fp
->f_bf
.smb_base
!= NULL
&& fp
->f_p
> fp
->f_bf
.smb_base
&&
170 ** Create an ungetc buffer.
171 ** Initially, we will use the `reserve' buffer.
176 fp
->f_ub
.smb_base
= fp
->f_ubuf
;
177 fp
->f_ub
.smb_size
= sizeof(fp
->f_ubuf
);
178 fp
->f_ubuf
[sizeof(fp
->f_ubuf
) - 1] = c
;
179 fp
->f_p
= &fp
->f_ubuf
[sizeof(fp
->f_ubuf
) - 1];