3
/* Function to handle io exceptions.
4
** Written by Kiem-Phong Vo
8
int _sfexcept(Sfio_t* f, int type, ssize_t io, Sfdisc_t* disc)
10
int _sfexcept(f,type,io,disc)
11
Sfio_t* f; /* stream where the exception happened */
12
int type; /* io type that was performed */
13
ssize_t io; /* the io return value that indicated exception */
14
Sfdisc_t* disc; /* discipline in use */
17
reg int ev, local, lock;
24
lock = f->mode&SF_LOCK;
27
f->flags |= io < 0 ? SF_ERROR : SF_EOF;
29
if(disc && disc->exceptf)
30
{ /* let the stream be generally accessible for this duration */
34
/* so that exception handler knows what we are asking for */
36
ev = (*(disc->exceptf))(f,type,&io,disc);
38
/* relock if necessary */
42
if(io > 0 && !(f->flags&SF_STRING) )
45
SFMTXRETURN(f, SF_EDONE);
47
SFMTXRETURN(f, SF_EDISC);
50
if(f->flags&SF_STRING)
53
else if(type != SF_WRITE && type != SF_SEEK)
54
SFMTXRETURN(f, SF_EDONE);
56
{ if(f->size >= 0 && !(f->flags&SF_MALLOC))
59
if((size = f->size) < 0)
63
size = ((size+io+SF_GRAIN-1)/SF_GRAIN)*SF_GRAIN;
65
data = (uchar*)realloc((char*)f->data,size);
66
else data = (uchar*)malloc(size);
69
f->endb = data + size;
70
f->next = data + (f->next - f->data);
71
f->endr = f->endw = f->data = data;
74
SFMTXRETURN(f, SF_EDISC);
78
{ if(_Sfexiting || (f->bits&SF_ENDING)) /* stop being a hero */
79
SFMTXRETURN(f, SF_EDONE);
81
/* a normal interrupt, we can continue */
83
f->flags &= ~(SF_EOF|SF_ERROR);
84
SFMTXRETURN(f, SF_ECONT);
88
if(local && f->push &&
89
((type == SF_READ && f->next >= f->endb) ||
90
(type == SF_WRITE && f->next <= f->data)))
98
pf = (*_Sfstack)(f,NIL(Sfio_t*));
99
if((ev = sfclose(pf)) < 0) /* can't close, restack */
105
ev = ev < 0 ? SF_EDONE : SF_ESTACK;