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_RCSID("@(#)$Id: fpos.c,v 1.37 2001/09/11 04:04:48 gshapiro Exp $")
21 #include <sm/signal.h>
24 #include <sm/assert.h>
27 static jmp_buf TellTimeOut;
30 ** TELLALRM -- handler when timeout activated for sm_io_tell()
32 ** Returns flow of control to where setjmp(TellTimeOut) was set.
41 ** returns flow of control to setjmp(TellTimeOut).
43 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
44 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
53 longjmp(TellTimeOut, 1);
57 ** SM_IO_TELL -- position the file pointer
60 ** fp -- the file pointer to get repositioned
61 ** timeout -- time to complete the tell (milliseconds)
64 ** Success -- the repositioned location.
65 ** Failure -- -1 (minus 1) and sets errno
69 sm_io_tell(fp, timeout)
70 register SM_FILE_T *fp;
71 int SM_NONVOLATILE timeout;
76 SM_REQUIRE_ISA(fp, SmFileMagic);
77 if (fp->f_seek == NULL)
79 errno = ESPIPE; /* historic practice */
83 if (timeout == SM_TIME_DEFAULT)
84 timeout = fp->f_timeout;
85 if (timeout == SM_TIME_IMMEDIATE)
88 ** Filling the buffer will take time and we are wanted to
89 ** return immediately. So...
97 ** Find offset of underlying I/O object, then adjust byte position
98 ** may adjust seek offset on append stream
101 (void) sm_flush(fp, (int *) &timeout);
103 /* This is where we start the timeout */
104 if (timeout != SM_TIME_FOREVER)
106 if (setjmp(TellTimeOut) != 0)
112 evt = sm_seteventm(timeout, tellalrm, 0);
115 if (fp->f_flags & SMOFF)
116 pos = fp->f_lseekoff;
119 /* XXX only set the timeout here? */
120 pos = (*fp->f_seek)(fp, (off_t) 0, SM_IO_SEEK_CUR);
124 if (fp->f_flags & SMRD)
127 ** Reading. Any unread characters (including
128 ** those from ungetc) cause the position to be
129 ** smaller than that in the underlying object.
136 else if (fp->f_flags & SMWR && fp->f_p != NULL)
139 ** Writing. Any buffered characters cause the
140 ** position to be greater than that in the
141 ** underlying object.
144 pos += fp->f_p - fp->f_bf.smb_base;
148 /* We're back. So undo our timeout and handler */