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