Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / libsm / fseek.c
blobe85d41ae92789e95fb94a0cce4faf8e25730eef5
1 /*
2 * Copyright (c) 2000-2001, 2004 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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
8 * Chris Torek.
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.
15 #include <sm/gen.h>
16 SM_RCSID("@(#)$Id: fseek.c,v 1.46 2004/08/03 20:17:38 ca Exp $")
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <setjmp.h>
23 #include <sys/time.h>
24 #include <sm/signal.h>
25 #include <sm/io.h>
26 #include <sm/assert.h>
27 #include <sm/clock.h>
28 #include "local.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.
40 ** Parameters:
41 ** sig -- unused
43 ** Returns:
44 ** does not return
46 ** Side Effects:
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
51 ** DOING.
54 /* ARGSUSED0 */
55 static void
56 seekalrm(sig)
57 int sig;
59 longjmp(SeekTimeOut, 1);
63 ** SM_IO_SEEK -- position the file pointer
65 ** Parameters:
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}.
71 ** Returns:
72 ** Failure: returns -1 (minus 1) and sets errno
73 ** Success: returns 0 (zero)
76 int
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;
83 bool havepos;
84 off_t target, curoff;
85 size_t n;
86 struct stat st;
87 int ret;
88 SM_EVENT *evt = NULL;
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 */
94 if (!Sm_IO_DidInit)
95 sm_init();
97 /* Have to be able to seek. */
98 if ((seekfn = fp->f_seek) == NULL)
100 errno = ESPIPE; /* historic practice */
101 return -1;
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...
113 errno = EAGAIN;
114 return -1;
117 #define SM_SET_ALARM() \
118 if (timeout != SM_TIME_FOREVER) \
120 if (setjmp(SeekTimeOut) != 0) \
122 errno = EAGAIN; \
123 return -1; \
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
131 ** SM_IO_SEEK_END.
134 switch (whence)
136 case SM_IO_SEEK_CUR:
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);
146 SM_SET_ALARM();
147 if (fp->f_flags & SMOFF)
148 curoff = fp->f_lseekoff;
149 else
151 curoff = (*seekfn)(fp, (off_t) 0, SM_IO_SEEK_CUR);
152 if (curoff == -1L)
154 ret = -1;
155 goto clean;
158 if (fp->f_flags & SMRD)
160 curoff -= fp->f_r;
161 if (HASUB(fp))
162 curoff -= fp->f_ur;
164 else if (fp->f_flags & SMWR && fp->f_p != NULL)
165 curoff += fp->f_p - fp->f_bf.smb_base;
167 offset += curoff;
168 whence = SM_IO_SEEK_SET;
169 havepos = true;
170 break;
172 case SM_IO_SEEK_SET:
173 case SM_IO_SEEK_END:
174 SM_SET_ALARM();
175 curoff = 0; /* XXX just to keep gcc quiet */
176 havepos = false;
177 break;
179 default:
180 errno = EINVAL;
181 return -1;
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)
194 sm_makebuf(fp);
195 if (fp->f_flags & (SMWR | SMRW | SMNBF | SMNPT))
196 goto dumb;
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;
204 goto dumb;
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)
216 target = offset;
217 else
219 if (fstat(fp->f_file, &st))
220 goto dumb;
221 target = st.st_size + offset;
224 if (!havepos)
226 if (fp->f_flags & SMOFF)
227 curoff = fp->f_lseekoff;
228 else
230 curoff = (*seekfn)(fp, (off_t) 0, SM_IO_SEEK_CUR);
231 if (curoff == POS_ERR)
232 goto dumb;
234 curoff -= fp->f_r;
235 if (HASUB(fp))
236 curoff -= fp->f_ur;
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.
246 if (HASUB(fp))
248 curoff += fp->f_r; /* kill off ungetc */
249 n = fp->f_up - fp->f_bf.smb_base;
250 curoff -= n;
251 n += fp->f_ur;
253 else
255 n = fp->f_p - fp->f_bf.smb_base;
256 curoff -= n;
257 n += fp->f_r;
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;
272 fp->f_r = n - o;
273 if (HASUB(fp))
274 FREEUB(fp);
275 fp->f_flags &= ~SMFEOF;
276 ret = 0;
277 goto clean;
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)
291 goto dumb;
292 fp->f_r = 0;
293 fp->f_p = fp->f_bf.smb_base;
294 if (HASUB(fp))
295 FREEUB(fp);
296 fp->f_flags &= ~SMFEOF;
297 n = target - curoff;
298 if (n)
300 /* Note: SM_TIME_FOREVER since fn timeout already set */
301 if (sm_refill(fp, SM_TIME_FOREVER) || fp->f_r < (int) n)
302 goto dumb;
303 fp->f_p += n;
304 fp->f_r -= n;
307 ret = 0;
308 clean:
309 /* We're back. So undo our timeout and handler */
310 if (evt != NULL)
311 sm_clrevent(evt);
312 return ret;
313 dumb:
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)
324 ret = -1;
325 goto clean;
328 /* success: clear SMFEOF indicator and discard ungetc() data */
329 if (HASUB(fp))
330 FREEUB(fp);
331 fp->f_p = fp->f_bf.smb_base;
332 fp->f_r = 0;
333 fp->f_flags &= ~SMFEOF;
334 ret = 0;
335 goto clean;