2 * Copyright (c) 2000-2001, 2004 Proofpoint, 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: fseek.c,v 1.48 2013-11-22 20:51:42 ca Exp $")
17 #include <sys/types.h>
24 #include <sm/signal.h>
26 #include <sm/assert.h>
30 #define POS_ERR (-(off_t)1)
32 static void seekalrm __P((int));
33 static jmp_buf SeekTimeOut;
36 ** SEEKALRM -- handler when timeout activated for sm_io_seek()
38 ** Returns flow of control to where setjmp(SeekTimeOut) was set.
47 ** returns flow of control to setjmp(SeekTimeOut).
49 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
50 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
59 longjmp(SeekTimeOut, 1);
63 ** SM_IO_SEEK -- position the file pointer
66 ** fp -- the file pointer to be seek'd
67 ** timeout -- time to complete seek (milliseconds)
68 ** offset -- seek offset based on 'whence'
69 ** whence -- indicates where seek is relative from.
70 ** One of SM_IO_SEEK_{CUR,SET,END}.
72 ** Failure: returns -1 (minus 1) and sets errno
73 ** Success: returns 0 (zero)
77 sm_io_seek(fp, timeout, offset, whence)
78 register SM_FILE_T *fp;
79 int SM_NONVOLATILE timeout;
80 long SM_NONVOLATILE offset;
81 int SM_NONVOLATILE whence;
89 register off_t (*seekfn) __P((SM_FILE_T *, off_t, int));
91 SM_REQUIRE_ISA(fp, SmFileMagic);
93 /* make sure stdio is set up */
97 /* Have to be able to seek. */
98 if ((seekfn = fp->f_seek) == NULL)
100 errno = ESPIPE; /* historic practice */
104 if (timeout == SM_TIME_DEFAULT)
105 timeout = fp->f_timeout;
106 if (timeout == SM_TIME_IMMEDIATE)
109 ** Filling the buffer will take time and we are wanted to
110 ** return immediately. So...
117 #define SM_SET_ALARM() \
118 if (timeout != SM_TIME_FOREVER) \
120 if (setjmp(SeekTimeOut) != 0) \
125 evt = sm_seteventm(timeout, seekalrm, 0); \
129 ** Change any SM_IO_SEEK_CUR to SM_IO_SEEK_SET, and check `whence'
130 ** argument. After this, whence is either SM_IO_SEEK_SET or
139 ** In order to seek relative to the current stream offset,
140 ** we have to first find the current stream offset a la
141 ** ftell (see ftell for details).
144 /* may adjust seek offset on append stream */
145 sm_flush(fp, (int *) &timeout);
147 if (fp->f_flags & SMOFF)
148 curoff = fp->f_lseekoff;
151 curoff = (*seekfn)(fp, (off_t) 0, SM_IO_SEEK_CUR);
158 if (fp->f_flags & SMRD)
164 else if (fp->f_flags & SMWR && fp->f_p != NULL)
165 curoff += fp->f_p - fp->f_bf.smb_base;
168 whence = SM_IO_SEEK_SET;
175 curoff = 0; /* XXX just to keep gcc quiet */
185 ** Can only optimise if:
186 ** reading (and not reading-and-writing);
187 ** not unbuffered; and
188 ** this is a `regular' Unix file (and hence seekfn==sm_stdseek).
189 ** We must check SMNBF first, because it is possible to have SMNBF
190 ** and SMSOPT both set.
193 if (fp->f_bf.smb_base == NULL)
195 if (fp->f_flags & (SMWR | SMRW | SMNBF | SMNPT))
197 if ((fp->f_flags & SMOPT) == 0)
199 if (seekfn != sm_stdseek ||
200 fp->f_file < 0 || fstat(fp->f_file, &st) ||
201 (st.st_mode & S_IFMT) != S_IFREG)
203 fp->f_flags |= SMNPT;
206 fp->f_blksize = st.st_blksize;
207 fp->f_flags |= SMOPT;
211 ** We are reading; we can try to optimise.
212 ** Figure out where we are going and where we are now.
215 if (whence == SM_IO_SEEK_SET)
219 if (fstat(fp->f_file, &st))
221 target = st.st_size + offset;
226 if (fp->f_flags & SMOFF)
227 curoff = fp->f_lseekoff;
230 curoff = (*seekfn)(fp, (off_t) 0, SM_IO_SEEK_CUR);
231 if (curoff == POS_ERR)
240 ** Compute the number of bytes in the input buffer (pretending
241 ** that any ungetc() input has been discarded). Adjust current
242 ** offset backwards by this count so that it represents the
243 ** file offset for the first byte in the current input buffer.
248 curoff += fp->f_r; /* kill off ungetc */
249 n = fp->f_up - fp->f_bf.smb_base;
255 n = fp->f_p - fp->f_bf.smb_base;
261 ** If the target offset is within the current buffer,
262 ** simply adjust the pointers, clear SMFEOF, undo ungetc(),
263 ** and return. (If the buffer was modified, we have to
264 ** skip this; see getln in fget.c.)
267 if (target >= curoff && target < curoff + (off_t) n)
269 register int o = target - curoff;
271 fp->f_p = fp->f_bf.smb_base + o;
275 fp->f_flags &= ~SMFEOF;
281 ** The place we want to get to is not within the current buffer,
282 ** but we can still be kind to the kernel copyout mechanism.
283 ** By aligning the file offset to a block boundary, we can let
284 ** the kernel use the VM hardware to map pages instead of
285 ** copying bytes laboriously. Using a block boundary also
286 ** ensures that we only read one block, rather than two.
289 curoff = target & ~(fp->f_blksize - 1);
290 if ((*seekfn)(fp, curoff, SM_IO_SEEK_SET) == POS_ERR)
293 fp->f_p = fp->f_bf.smb_base;
296 fp->f_flags &= ~SMFEOF;
300 /* Note: SM_TIME_FOREVER since fn timeout already set */
301 if (sm_refill(fp, SM_TIME_FOREVER) || fp->f_r < (int) n)
309 /* We're back. So undo our timeout and handler */
315 ** We get here if we cannot optimise the seek ... just
316 ** do it. Allow the seek function to change fp->f_bf.smb_base.
319 /* Note: SM_TIME_FOREVER since fn timeout already set */
320 ret = SM_TIME_FOREVER;
321 if (sm_flush(fp, &ret) != 0 ||
322 (*seekfn)(fp, (off_t) offset, whence) == POS_ERR)
328 /* success: clear SMFEOF indicator and discard ungetc() data */
331 fp->f_p = fp->f_bf.smb_base;
333 fp->f_flags &= ~SMFEOF;