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: fseek.c,v 1.45 2001/09/11 04:04:48 gshapiro 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 jmp_buf SeekTimeOut
;
35 ** SEEKALRM -- handler when timeout activated for sm_io_seek()
37 ** Returns flow of control to where setjmp(SeekTimeOut) was set.
46 ** returns flow of control to setjmp(SeekTimeOut).
48 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
49 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
58 longjmp(SeekTimeOut
, 1);
62 ** SM_IO_SEEK -- position the file pointer
65 ** fp -- the file pointer to be seek'd
66 ** timeout -- time to complete seek (milliseconds)
67 ** offset -- seek offset based on 'whence'
68 ** whence -- indicates where seek is relative from.
69 ** One of SM_IO_SEEK_{CUR,SET,END}.
71 ** Failure: returns -1 (minus 1) and sets errno
72 ** Success: returns 0 (zero)
76 sm_io_seek(fp
, timeout
, offset
, whence
)
77 register SM_FILE_T
*fp
;
78 int SM_NONVOLATILE timeout
;
79 long SM_NONVOLATILE offset
;
80 int SM_NONVOLATILE whence
;
88 register off_t (*seekfn
) __P((SM_FILE_T
*, off_t
, int));
90 SM_REQUIRE_ISA(fp
, SmFileMagic
);
92 /* make sure stdio is set up */
96 /* Have to be able to seek. */
97 if ((seekfn
= fp
->f_seek
) == NULL
)
99 errno
= ESPIPE
; /* historic practice */
103 if (timeout
== SM_TIME_DEFAULT
)
104 timeout
= fp
->f_timeout
;
105 if (timeout
== SM_TIME_IMMEDIATE
)
108 ** Filling the buffer will take time and we are wanted to
109 ** return immediately. So...
116 #define SM_SET_ALARM() \
117 if (timeout != SM_TIME_FOREVER) \
119 if (setjmp(SeekTimeOut) != 0) \
124 evt = sm_seteventm(timeout, seekalrm, 0); \
128 ** Change any SM_IO_SEEK_CUR to SM_IO_SEEK_SET, and check `whence'
129 ** argument. After this, whence is either SM_IO_SEEK_SET or
138 ** In order to seek relative to the current stream offset,
139 ** we have to first find the current stream offset a la
140 ** ftell (see ftell for details).
143 /* may adjust seek offset on append stream */
144 sm_flush(fp
, (int *) &timeout
);
146 if (fp
->f_flags
& SMOFF
)
147 curoff
= fp
->f_lseekoff
;
150 curoff
= (*seekfn
)(fp
, (off_t
) 0, SM_IO_SEEK_CUR
);
157 if (fp
->f_flags
& SMRD
)
163 else if (fp
->f_flags
& SMWR
&& fp
->f_p
!= NULL
)
164 curoff
+= fp
->f_p
- fp
->f_bf
.smb_base
;
167 whence
= SM_IO_SEEK_SET
;
174 curoff
= 0; /* XXX just to keep gcc quiet */
184 ** Can only optimise if:
185 ** reading (and not reading-and-writing);
186 ** not unbuffered; and
187 ** this is a `regular' Unix file (and hence seekfn==sm_stdseek).
188 ** We must check SMNBF first, because it is possible to have SMNBF
189 ** and SMSOPT both set.
192 if (fp
->f_bf
.smb_base
== NULL
)
194 if (fp
->f_flags
& (SMWR
| SMRW
| SMNBF
| SMNPT
))
196 if ((fp
->f_flags
& SMOPT
) == 0)
198 if (seekfn
!= sm_stdseek
||
199 fp
->f_file
< 0 || fstat(fp
->f_file
, &st
) ||
200 (st
.st_mode
& S_IFMT
) != S_IFREG
)
202 fp
->f_flags
|= SMNPT
;
205 fp
->f_blksize
= st
.st_blksize
;
206 fp
->f_flags
|= SMOPT
;
210 ** We are reading; we can try to optimise.
211 ** Figure out where we are going and where we are now.
214 if (whence
== SM_IO_SEEK_SET
)
218 if (fstat(fp
->f_file
, &st
))
220 target
= st
.st_size
+ offset
;
225 if (fp
->f_flags
& SMOFF
)
226 curoff
= fp
->f_lseekoff
;
229 curoff
= (*seekfn
)(fp
, (off_t
) 0, SM_IO_SEEK_CUR
);
230 if (curoff
== POS_ERR
)
239 ** Compute the number of bytes in the input buffer (pretending
240 ** that any ungetc() input has been discarded). Adjust current
241 ** offset backwards by this count so that it represents the
242 ** file offset for the first byte in the current input buffer.
247 curoff
+= fp
->f_r
; /* kill off ungetc */
248 n
= fp
->f_up
- fp
->f_bf
.smb_base
;
254 n
= fp
->f_p
- fp
->f_bf
.smb_base
;
260 ** If the target offset is within the current buffer,
261 ** simply adjust the pointers, clear SMFEOF, undo ungetc(),
262 ** and return. (If the buffer was modified, we have to
263 ** skip this; see getln in fget.c.)
266 if (target
>= curoff
&& target
< curoff
+ (off_t
) n
)
268 register int o
= target
- curoff
;
270 fp
->f_p
= fp
->f_bf
.smb_base
+ o
;
274 fp
->f_flags
&= ~SMFEOF
;
280 ** The place we want to get to is not within the current buffer,
281 ** but we can still be kind to the kernel copyout mechanism.
282 ** By aligning the file offset to a block boundary, we can let
283 ** the kernel use the VM hardware to map pages instead of
284 ** copying bytes laboriously. Using a block boundary also
285 ** ensures that we only read one block, rather than two.
288 curoff
= target
& ~(fp
->f_blksize
- 1);
289 if ((*seekfn
)(fp
, curoff
, SM_IO_SEEK_SET
) == POS_ERR
)
292 fp
->f_p
= fp
->f_bf
.smb_base
;
295 fp
->f_flags
&= ~SMFEOF
;
299 /* Note: SM_TIME_FOREVER since fn timeout already set */
300 if (sm_refill(fp
, SM_TIME_FOREVER
) || fp
->f_r
< (int) n
)
308 /* We're back. So undo our timeout and handler */
314 ** We get here if we cannot optimise the seek ... just
315 ** do it. Allow the seek function to change fp->f_bf.smb_base.
318 /* Note: SM_TIME_FOREVER since fn timeout already set */
319 ret
= SM_TIME_FOREVER
;
320 if (sm_flush(fp
, &ret
) != 0 ||
321 (*seekfn
)(fp
, (off_t
) offset
, whence
) == POS_ERR
)
327 /* success: clear SMFEOF indicator and discard ungetc() data */
330 fp
->f_p
= fp
->f_bf
.smb_base
;
332 fp
->f_flags
&= ~SMFEOF
;