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

« back to all changes in this revision

Viewing changes to tools/sfio/sfsetfd.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
/*      Change the file descriptor
 
4
**
 
5
**      Written by Kiem-Phong Vo.
 
6
*/
 
7
 
 
8
#if __STD_C
 
9
static int _sfdup(reg int fd, reg int newfd)
 
10
#else
 
11
static int _sfdup(fd,newfd)
 
12
reg int fd;
 
13
reg int newfd;
 
14
#endif
 
15
{
 
16
        reg int dupfd;
 
17
 
 
18
#ifdef F_DUPFD  /* the simple case */
 
19
        while((dupfd = fcntl(fd,F_DUPFD,newfd)) < 0 && errno == EINTR)
 
20
                errno = 0;
 
21
        return dupfd;
 
22
 
 
23
#else   /* do it the hard way */
 
24
        if((dupfd = dup(fd)) < 0 || dupfd >= newfd)
 
25
                return dupfd;
 
26
 
 
27
        /* dup() succeeded but didn't get the right number, recurse */
 
28
        newfd = _sfdup(fd,newfd);
 
29
 
 
30
        /* close the one that didn't match */
 
31
        CLOSE(dupfd);
 
32
 
 
33
        return newfd;
 
34
#endif
 
35
}
 
36
 
 
37
#if __STD_C
 
38
int sfsetfd(reg Sfio_t* f, reg int newfd)
 
39
#else
 
40
int sfsetfd(f,newfd)
 
41
reg Sfio_t      *f;
 
42
reg int         newfd;
 
43
#endif
 
44
{
 
45
        reg int         oldfd;
 
46
 
 
47
        SFMTXSTART(f, -1);
 
48
 
 
49
        if(f->flags&SF_STRING)
 
50
                SFMTXRETURN(f, -1);
 
51
 
 
52
        if((f->mode&SF_INIT) && f->file < 0)
 
53
        {       /* restoring file descriptor after a previous freeze */
 
54
                if(newfd < 0)
 
55
                        SFMTXRETURN(f, -1);
 
56
        }
 
57
        else
 
58
        {       /* change file descriptor */
 
59
                if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
 
60
                        SFMTXRETURN(f, -1);
 
61
                SFLOCK(f,0);
 
62
 
 
63
                oldfd = f->file;
 
64
                if(oldfd >= 0)
 
65
                {       if(newfd >= 0)
 
66
                        {       if((newfd = _sfdup(oldfd,newfd)) < 0)
 
67
                                {       SFOPEN(f,0);
 
68
                                        SFMTXRETURN(f, -1);
 
69
                                }
 
70
                                CLOSE(oldfd);
 
71
                        }
 
72
                        else
 
73
                        {       /* sync stream if necessary */
 
74
                                if(((f->mode&SF_WRITE) && f->next > f->data) ||
 
75
                                   (f->mode&SF_READ) || f->disc == _Sfudisc)
 
76
                                {       if(SFSYNC(f) < 0)
 
77
                                        {       SFOPEN(f,0);
 
78
                                                SFMTXRETURN(f, -1);
 
79
                                        }
 
80
                                }
 
81
 
 
82
                                if(((f->mode&SF_WRITE) && f->next > f->data) ||
 
83
                                   ((f->mode&SF_READ) && f->extent < 0 &&
 
84
                                    f->next < f->endb) )
 
85
                                {       SFOPEN(f,0);
 
86
                                        SFMTXRETURN(f, -1);
 
87
                                }
 
88
 
 
89
#ifdef MAP_TYPE
 
90
                                if((f->bits&SF_MMAP) && f->data)
 
91
                                {       SFMUNMAP(f,f->data,f->endb-f->data);
 
92
                                        f->data = NIL(uchar*);
 
93
                                }
 
94
#endif
 
95
 
 
96
                                /* make stream appears uninitialized */
 
97
                                f->endb = f->endr = f->endw = f->data;
 
98
                                f->extent = f->here = 0;
 
99
                                f->mode = (f->mode&SF_RDWR)|SF_INIT;
 
100
                                f->bits &= ~SF_NULL;    /* off /dev/null handling */
 
101
                        }
 
102
                }
 
103
 
 
104
                SFOPEN(f,0);
 
105
        }
 
106
 
 
107
        /* notify changes */
 
108
        if(_Sfnotify)
 
109
                (*_Sfnotify)(f,SF_SETFD,newfd);
 
110
 
 
111
        f->file = newfd;
 
112
 
 
113
        SFMTXRETURN(f,newfd);
 
114
}