~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to tools/sfio/sfungetc.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include        "sfhdr.h"
 
2
 
 
3
/*      Push back one byte to a given SF_READ stream
 
4
**
 
5
**      Written by Kiem-Phong Vo.
 
6
*/
 
7
#if __STD_C
 
8
static int _uexcept(reg Sfio_t* f, reg int type, Void_t* val, reg Sfdisc_t* disc)
 
9
#else
 
10
static int _uexcept(f,type,val,disc)
 
11
reg Sfio_t      *f;
 
12
reg int         type;
 
13
Void_t*         val;
 
14
reg Sfdisc_t    *disc;
 
15
#endif
 
16
{       
 
17
        NOTUSED(val);
 
18
 
 
19
        /* hmm! This should never happen */
 
20
        if(disc != _Sfudisc)
 
21
                return -1;
 
22
 
 
23
        /* close the unget stream */
 
24
        if(type != SF_CLOSING)
 
25
                (void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
 
26
 
 
27
        return 1;
 
28
}
 
29
 
 
30
#if __STD_C
 
31
int sfungetc(reg Sfio_t* f, reg int c)
 
32
#else
 
33
int sfungetc(f,c)
 
34
reg Sfio_t*     f;      /* push back one byte to this stream */
 
35
reg int         c;      /* the value to be pushed back */
 
36
#endif
 
37
{
 
38
        reg Sfio_t*     uf;
 
39
 
 
40
        SFMTXSTART(f, -1)
 
41
 
 
42
        if(c < 0 || (f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0))
 
43
                SFMTXRETURN(f, -1);
 
44
        SFLOCK(f,0);
 
45
 
 
46
        /* fast handling of the typical unget */
 
47
        if(f->next > f->data && f->next[-1] == (uchar)c)
 
48
        {       f->next -= 1;
 
49
                goto done;
 
50
        }
 
51
 
 
52
        /* make a string stream for unget characters */
 
53
        if(f->disc != _Sfudisc)
 
54
        {       if(!(uf = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND,
 
55
                                -1,SF_STRING|SF_READ)))
 
56
                {       c = -1;
 
57
                        goto done;
 
58
                }
 
59
                _Sfudisc->exceptf = _uexcept;
 
60
                sfdisc(uf,_Sfudisc);
 
61
                SFOPEN(f,0); (void)sfstack(f,uf); SFLOCK(f,0);
 
62
        }
 
63
 
 
64
        /* space for data */
 
65
        if(f->next == f->data)
 
66
        {       reg uchar*      data;
 
67
                if(f->size < 0)
 
68
                        f->size = 0;
 
69
                if(!(data = (uchar*)malloc(f->size+16)))
 
70
                {       c = -1;
 
71
                        goto done;
 
72
                }
 
73
                f->flags |= SF_MALLOC;
 
74
                if(f->data)
 
75
                        memcpy((char*)(data+16),(char*)f->data,f->size);
 
76
                f->size += 16;
 
77
                f->data  = data;
 
78
                f->next  = data+16;
 
79
                f->endb  = data+f->size;
 
80
        }
 
81
 
 
82
        *--f->next = (uchar)c;
 
83
done:
 
84
        SFOPEN(f,0);
 
85
        SFMTXRETURN(f, c);
 
86
}