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

« back to all changes in this revision

Viewing changes to tools/sfio/sfnew.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
/*      Fundamental function to create a new stream.
 
4
**      The argument flags defines the type of stream and the scheme
 
5
**      of buffering.
 
6
**
 
7
**      Written by Kiem-Phong Vo.
 
8
*/
 
9
 
 
10
#if __STD_C
 
11
Sfio_t* sfnew(Sfio_t* oldf, Void_t* buf, size_t size, int file, int flags)
 
12
#else
 
13
Sfio_t* sfnew(oldf,buf,size,file,flags)
 
14
Sfio_t* oldf;   /* old stream to be reused */
 
15
Void_t* buf;    /* a buffer to read/write, if NULL, will be allocated */
 
16
size_t  size;   /* buffer size if buf is given or desired buffer size */
 
17
int     file;   /* file descriptor to read/write from */
 
18
int     flags;  /* type of file stream */
 
19
#endif
 
20
{
 
21
        reg Sfio_t*     f;
 
22
        reg int         sflags;
 
23
 
 
24
        SFONCE();       /* initialize mutexes */
 
25
 
 
26
        if(!(flags&SF_RDWR))
 
27
                return NIL(Sfio_t*);
 
28
 
 
29
        sflags = 0;
 
30
        if((f = oldf) )
 
31
        {       if(flags&SF_EOF)
 
32
                {       if(f != sfstdin && f != sfstdout && f != sfstderr)
 
33
                                f->mutex = NIL(Vtmutex_t*);
 
34
                        SFCLEAR(f, f->mutex);
 
35
                        oldf = NIL(Sfio_t*);
 
36
                }
 
37
                else if(f->mode&SF_AVAIL)
 
38
                {       /* only allow SF_STATIC to be already closed */
 
39
                        if(!(f->flags&SF_STATIC) )
 
40
                                return NIL(Sfio_t*);
 
41
                        sflags = f->flags;
 
42
                        oldf = NIL(Sfio_t*);
 
43
                }
 
44
                else
 
45
                {       /* reopening an open stream, close it first */
 
46
                        sflags = f->flags;
 
47
 
 
48
                        if(((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0) ||
 
49
                           SFCLOSE(f) < 0 )
 
50
                                return NIL(Sfio_t*);
 
51
 
 
52
                        if(f->data && ((flags&SF_STRING) || size != (size_t)SF_UNBOUND) )
 
53
                        {       if(sflags&SF_MALLOC)
 
54
                                        free((Void_t*)f->data);
 
55
                                f->data = NIL(uchar*);
 
56
                        }
 
57
                        if(!f->data)
 
58
                                sflags &= ~SF_MALLOC;
 
59
                }
 
60
        }
 
61
 
 
62
        if(!f)
 
63
        {       /* reuse a standard stream structure if possible */
 
64
                if(!(flags&SF_STRING) && file >= 0 && file <= 2)
 
65
                {       f = file == 0 ? sfstdin : file == 1 ? sfstdout : sfstderr;
 
66
                        if(f)
 
67
                        {       if(f->mode&SF_AVAIL)
 
68
                                {       sflags = f->flags;
 
69
                                        SFCLEAR(f, f->mutex);
 
70
                                }
 
71
                                else    f = NIL(Sfio_t*);
 
72
                        }
 
73
                }
 
74
 
 
75
                if(!f)
 
76
                {       if(!(f = (Sfio_t*)malloc(sizeof(Sfio_t))) )
 
77
                                return NIL(Sfio_t*);
 
78
                        SFCLEAR(f, NIL(Vtmutex_t*));
 
79
                }
 
80
        }
 
81
 
 
82
        /* create a mutex */
 
83
        if(!f->mutex)
 
84
                f->mutex = vtmtxopen(NIL(Vtmutex_t*), VT_INIT);
 
85
 
 
86
        /* stream type */
 
87
        f->mode = (flags&SF_READ) ? SF_READ : SF_WRITE;
 
88
        f->flags = (flags&SF_FLAGS) | (sflags&(SF_MALLOC|SF_STATIC));
 
89
        f->bits = (flags&SF_RDWR) == SF_RDWR ? SF_BOTH : 0;
 
90
        f->file = file;
 
91
        f->here = f->extent = 0;
 
92
        f->getr = f->tiny[0] = 0;
 
93
 
 
94
        f->mode |= SF_INIT;
 
95
        if(size != (size_t)SF_UNBOUND)
 
96
        {       f->size = size;
 
97
                f->data = size <= 0 ? NIL(uchar*) : (uchar*)buf;
 
98
        }
 
99
        f->endb = f->endr = f->endw = f->next = f->data;
 
100
 
 
101
        if(_Sfnotify)
 
102
                (*_Sfnotify)(f,SF_NEW,f->file);
 
103
 
 
104
        if(f->flags&SF_STRING)
 
105
                (void)_sfmode(f,f->mode&SF_RDWR,0);
 
106
 
 
107
        return f;
 
108
}