xref: /illumos-gate/usr/src/cmd/sendmail/libsm/ungetc.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001, 2004 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
57c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
87c478bd9Sstevel@tonic-gate  * Chris Torek.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
117c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
127c478bd9Sstevel@tonic-gate  * the sendmail distribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <sm/gen.h>
16*49218d4fSjbeck SM_IDSTR(id, "@(#)$Id: ungetc.c,v 1.30 2005/06/14 23:07:20 ca Exp $")
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include <stdlib.h>
197c478bd9Sstevel@tonic-gate #include <string.h>
207c478bd9Sstevel@tonic-gate #include <signal.h>
21*49218d4fSjbeck #include <sm/time.h>
227c478bd9Sstevel@tonic-gate #include <errno.h>
237c478bd9Sstevel@tonic-gate #include <sm/io.h>
247c478bd9Sstevel@tonic-gate #include <sm/heap.h>
257c478bd9Sstevel@tonic-gate #include <sm/assert.h>
267c478bd9Sstevel@tonic-gate #include <sm/conf.h>
277c478bd9Sstevel@tonic-gate #include "local.h"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate static void	sm_submore_x __P((SM_FILE_T *));
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate **  SM_SUBMORE_X -- expand ungetc buffer
337c478bd9Sstevel@tonic-gate **
347c478bd9Sstevel@tonic-gate **  Expand the ungetc buffer `in place'.  That is, adjust fp->f_p when
357c478bd9Sstevel@tonic-gate **  the buffer moves, so that it points the same distance from the end,
367c478bd9Sstevel@tonic-gate **  and move the bytes in the buffer around as necessary so that they
377c478bd9Sstevel@tonic-gate **  are all at the end (stack-style).
387c478bd9Sstevel@tonic-gate **
397c478bd9Sstevel@tonic-gate **	Parameters:
407c478bd9Sstevel@tonic-gate **		fp -- the file pointer
417c478bd9Sstevel@tonic-gate **
427c478bd9Sstevel@tonic-gate **	Results:
437c478bd9Sstevel@tonic-gate **		none.
447c478bd9Sstevel@tonic-gate **
457c478bd9Sstevel@tonic-gate **	Exceptions:
467c478bd9Sstevel@tonic-gate **		F:sm_heap -- out of memory
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static void
sm_submore_x(fp)507c478bd9Sstevel@tonic-gate sm_submore_x(fp)
517c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	register int i;
547c478bd9Sstevel@tonic-gate 	register unsigned char *p;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (fp->f_ub.smb_base == fp->f_ubuf)
577c478bd9Sstevel@tonic-gate 	{
587c478bd9Sstevel@tonic-gate 		/* Get a buffer; f_ubuf is fixed size. */
597c478bd9Sstevel@tonic-gate 		p = sm_malloc_x((size_t) SM_IO_BUFSIZ);
607c478bd9Sstevel@tonic-gate 		fp->f_ub.smb_base = p;
617c478bd9Sstevel@tonic-gate 		fp->f_ub.smb_size = SM_IO_BUFSIZ;
627c478bd9Sstevel@tonic-gate 		p += SM_IO_BUFSIZ - sizeof(fp->f_ubuf);
637c478bd9Sstevel@tonic-gate 		for (i = sizeof(fp->f_ubuf); --i >= 0;)
647c478bd9Sstevel@tonic-gate 			p[i] = fp->f_ubuf[i];
657c478bd9Sstevel@tonic-gate 		fp->f_p = p;
667c478bd9Sstevel@tonic-gate 		return;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	i = fp->f_ub.smb_size;
697c478bd9Sstevel@tonic-gate 	p = sm_realloc_x(fp->f_ub.smb_base, i << 1);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* no overlap (hence can use memcpy) because we doubled the size */
727c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) (p + i), (void *) p, (size_t) i);
737c478bd9Sstevel@tonic-gate 	fp->f_p = p + i;
747c478bd9Sstevel@tonic-gate 	fp->f_ub.smb_base = p;
757c478bd9Sstevel@tonic-gate 	fp->f_ub.smb_size = i << 1;
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate **  SM_IO_UNGETC -- place a character back into the buffer just read
807c478bd9Sstevel@tonic-gate **
817c478bd9Sstevel@tonic-gate **	Parameters:
827c478bd9Sstevel@tonic-gate **		fp -- the file pointer affected
837c478bd9Sstevel@tonic-gate **		timeout -- time to complete ungetc
847c478bd9Sstevel@tonic-gate **		c -- the character to place back
857c478bd9Sstevel@tonic-gate **
867c478bd9Sstevel@tonic-gate **	Results:
877c478bd9Sstevel@tonic-gate **		On success, returns value of character placed back, 0-255.
887c478bd9Sstevel@tonic-gate **		Returns SM_IO_EOF if c == SM_IO_EOF or if last operation
897c478bd9Sstevel@tonic-gate **		was a write and flush failed.
907c478bd9Sstevel@tonic-gate **
917c478bd9Sstevel@tonic-gate **	Exceptions:
927c478bd9Sstevel@tonic-gate **		F:sm_heap -- out of memory
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int
sm_io_ungetc(fp,timeout,c)967c478bd9Sstevel@tonic-gate sm_io_ungetc(fp, timeout, c)
977c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
987c478bd9Sstevel@tonic-gate 	int timeout;
997c478bd9Sstevel@tonic-gate 	int c;
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	SM_REQUIRE_ISA(fp, SmFileMagic);
1027c478bd9Sstevel@tonic-gate 	if (c == SM_IO_EOF)
1037c478bd9Sstevel@tonic-gate 		return SM_IO_EOF;
1047c478bd9Sstevel@tonic-gate 	if (timeout == SM_TIME_IMMEDIATE)
1057c478bd9Sstevel@tonic-gate 	{
1067c478bd9Sstevel@tonic-gate 		/*
1077c478bd9Sstevel@tonic-gate 		**  Ungetting the buffer will take time and we are wanted to
1087c478bd9Sstevel@tonic-gate 		**  return immediately. So...
1097c478bd9Sstevel@tonic-gate 		*/
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		errno = EAGAIN;
1127c478bd9Sstevel@tonic-gate 		return SM_IO_EOF;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (!Sm_IO_DidInit)
1167c478bd9Sstevel@tonic-gate 		sm_init();
1177c478bd9Sstevel@tonic-gate 	if ((fp->f_flags & SMRD) == 0)
1187c478bd9Sstevel@tonic-gate 	{
1197c478bd9Sstevel@tonic-gate 		/*
1207c478bd9Sstevel@tonic-gate 		**  Not already reading: no good unless reading-and-writing.
1217c478bd9Sstevel@tonic-gate 		**  Otherwise, flush any current write stuff.
1227c478bd9Sstevel@tonic-gate 		*/
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		if ((fp->f_flags & SMRW) == 0)
1257c478bd9Sstevel@tonic-gate 			return SM_IO_EOF;
1267c478bd9Sstevel@tonic-gate 		if (fp->f_flags & SMWR)
1277c478bd9Sstevel@tonic-gate 		{
1287c478bd9Sstevel@tonic-gate 			if (sm_flush(fp, &timeout))
1297c478bd9Sstevel@tonic-gate 				return SM_IO_EOF;
1307c478bd9Sstevel@tonic-gate 			fp->f_flags &= ~SMWR;
1317c478bd9Sstevel@tonic-gate 			fp->f_w = 0;
1327c478bd9Sstevel@tonic-gate 			fp->f_lbfsize = 0;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		fp->f_flags |= SMRD;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	c = (unsigned char) c;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	**  If we are in the middle of ungetc'ing, just continue.
1407c478bd9Sstevel@tonic-gate 	**  This may require expanding the current ungetc buffer.
1417c478bd9Sstevel@tonic-gate 	*/
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (HASUB(fp))
1447c478bd9Sstevel@tonic-gate 	{
1457c478bd9Sstevel@tonic-gate 		if (fp->f_r >= fp->f_ub.smb_size)
1467c478bd9Sstevel@tonic-gate 			sm_submore_x(fp);
1477c478bd9Sstevel@tonic-gate 		*--fp->f_p = c;
1487c478bd9Sstevel@tonic-gate 		fp->f_r++;
1497c478bd9Sstevel@tonic-gate 		return c;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 	fp->f_flags &= ~SMFEOF;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	**  If we can handle this by simply backing up, do so,
1557c478bd9Sstevel@tonic-gate 	**  but never replace the original character.
1567c478bd9Sstevel@tonic-gate 	**  (This makes sscanf() work when scanning `const' data.)
1577c478bd9Sstevel@tonic-gate 	*/
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (fp->f_bf.smb_base != NULL && fp->f_p > fp->f_bf.smb_base &&
1607c478bd9Sstevel@tonic-gate 	    fp->f_p[-1] == c)
1617c478bd9Sstevel@tonic-gate 	{
1627c478bd9Sstevel@tonic-gate 		fp->f_p--;
1637c478bd9Sstevel@tonic-gate 		fp->f_r++;
1647c478bd9Sstevel@tonic-gate 		return c;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	**  Create an ungetc buffer.
1697c478bd9Sstevel@tonic-gate 	**  Initially, we will use the `reserve' buffer.
1707c478bd9Sstevel@tonic-gate 	*/
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	fp->f_ur = fp->f_r;
1737c478bd9Sstevel@tonic-gate 	fp->f_up = fp->f_p;
1747c478bd9Sstevel@tonic-gate 	fp->f_ub.smb_base = fp->f_ubuf;
1757c478bd9Sstevel@tonic-gate 	fp->f_ub.smb_size = sizeof(fp->f_ubuf);
1767c478bd9Sstevel@tonic-gate 	fp->f_ubuf[sizeof(fp->f_ubuf) - 1] = c;
1777c478bd9Sstevel@tonic-gate 	fp->f_p = &fp->f_ubuf[sizeof(fp->f_ubuf) - 1];
1787c478bd9Sstevel@tonic-gate 	fp->f_r = 1;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	return c;
1817c478bd9Sstevel@tonic-gate }
182