summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/fpos.c
blob: a8083fd86fc0f02ff5687fd6f44da55d043639f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
 *      All rights reserved.
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: fpos.c,v 1.37 2001/09/11 04:04:48 gshapiro Exp $")
#include <errno.h>
#include <setjmp.h>
#include <sys/time.h>
#include <sm/heap.h>
#include <sm/signal.h>
#include <sm/clock.h>
#include <sm/io.h>
#include <sm/assert.h>
#include "local.h"

static jmp_buf TellTimeOut;

/*
**  TELLALRM -- handler when timeout activated for sm_io_tell()
**
**  Returns flow of control to where setjmp(TellTimeOut) was set.
**
**	Parameters:
**		sig -- unused
**
**	Returns:
**		does not return
**
**	Side Effects:
**		returns flow of control to setjmp(TellTimeOut).
**
**	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
**		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
**		DOING.
*/

/* ARGSUSED0 */
static void
tellalrm(sig)
	int sig;
{
	longjmp(TellTimeOut, 1);
}

/*
**  SM_IO_TELL -- position the file pointer
**
**	Paramters:
**		fp -- the file pointer to get repositioned
**		timeout -- time to complete the tell (milliseconds)
**
**	Returns:
**		Success -- the repositioned location.
**		Failure -- -1 (minus 1) and sets errno
*/

long
sm_io_tell(fp, timeout)
	register SM_FILE_T *fp;
	int SM_NONVOLATILE timeout;
{
	register off_t pos;
	SM_EVENT *evt = NULL;

	SM_REQUIRE_ISA(fp, SmFileMagic);
	if (fp->f_seek == NULL)
	{
		errno = ESPIPE;			/* historic practice */
		return -1L;
	}

	if (timeout == SM_TIME_DEFAULT)
		timeout = fp->f_timeout;
	if (timeout == SM_TIME_IMMEDIATE)
	{
		/*
		**  Filling the buffer will take time and we are wanted to
		**  return immediately. So...
		*/

		errno = EAGAIN;
		return -1L;
	}

	/*
	**  Find offset of underlying I/O object, then adjust byte position
	**  may adjust seek offset on append stream
	*/

	(void) sm_flush(fp, (int *) &timeout);

	/* This is where we start the timeout */
	if (timeout != SM_TIME_FOREVER)
	{
		if (setjmp(TellTimeOut) != 0)
		{
			errno = EAGAIN;
			return -1L;
		}

		evt = sm_seteventm(timeout, tellalrm, 0);
	}

	if (fp->f_flags & SMOFF)
		pos = fp->f_lseekoff;
	else
	{
		/* XXX only set the timeout here? */
		pos = (*fp->f_seek)(fp, (off_t) 0, SM_IO_SEEK_CUR);
		if (pos == -1L)
			goto clean;
	}
	if (fp->f_flags & SMRD)
	{
		/*
		**  Reading.  Any unread characters (including
		**  those from ungetc) cause the position to be
		**  smaller than that in the underlying object.
		*/

		pos -= fp->f_r;
		if (HASUB(fp))
			pos -= fp->f_ur;
	}
	else if (fp->f_flags & SMWR && fp->f_p != NULL)
	{
		/*
		**  Writing.  Any buffered characters cause the
		**  position to be greater than that in the
		**  underlying object.
		*/

		pos += fp->f_p - fp->f_bf.smb_base;
	}

clean:
	/*  We're back. So undo our timeout and handler */
	if (evt != NULL)
		sm_clrevent(evt);
	return pos;
}
OpenPOWER on IntegriCloud