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

« back to all changes in this revision

Viewing changes to tools/sfio/sfexit.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
/*
 
4
**      Any required functions for process exiting.
 
5
**      Written by Kiem-Phong Vo
 
6
*/
 
7
 
 
8
#if PACKAGE_ast
 
9
int     _AST_already_has_them;
 
10
#else
 
11
 
 
12
#if !_lib_atexit
 
13
#if _lib_onexit
 
14
 
 
15
#if __STD_C
 
16
int atexit(void (*exitf)(void))
 
17
#else
 
18
int atexit(exitf)
 
19
void    (*exitf)();
 
20
#endif
 
21
{
 
22
#if _lib_onexit
 
23
        return onexit(exitf);
 
24
#endif
 
25
}
 
26
 
 
27
#else /* !_lib_onexit */
 
28
 
 
29
typedef struct _exit_s
 
30
{       struct _exit_s* next;
 
31
        void            (*exitf)_ARG_((void));
 
32
} Exit_t;
 
33
static Exit_t*  Exit;
 
34
 
 
35
#if __STD_C
 
36
atexit(void (*exitf)(void))
 
37
#else
 
38
atexit(exitf)
 
39
void    (*exitf)();
 
40
#endif
 
41
{       Exit_t* e;
 
42
        int     rv;
 
43
 
 
44
        vtmtxlock(_Sfmutex);
 
45
 
 
46
        if(!(e = (Exit_t*)malloc(sizeof(Exit_t))) )
 
47
                rv = -1;
 
48
        else
 
49
        {       e->exitf = exitf;
 
50
                e->next = Exit;
 
51
                Exit = e;
 
52
                rv = 0;
 
53
        }
 
54
 
 
55
        vtmtxunlock(_Sfmutex);
 
56
 
 
57
        return rv;
 
58
}
 
59
 
 
60
#if _exit_cleanup
 
61
/* since exit() calls _cleanup(), we only need to redefine _cleanup() */
 
62
#if __STD_C
 
63
_cleanup(void)
 
64
#else
 
65
_cleanup()
 
66
#endif
 
67
{
 
68
        Exit_t* e;
 
69
        for(e = Exit; e; e = e->next)
 
70
                (*e->exitf)();
 
71
        if(_Sfcleanup)
 
72
                (*_Sfcleanup)();
 
73
}
 
74
 
 
75
#else
 
76
 
 
77
/* in this case, we have to redefine exit() itself */
 
78
#if __STD_C
 
79
exit(int type)
 
80
#else
 
81
exit(type)
 
82
int     type;
 
83
#endif
 
84
{
 
85
        Exit_t* e;
 
86
        for(e = Exit; e; e = e->next)
 
87
                (*e->exitf)();
 
88
        _exit(type);
 
89
        return type;
 
90
}
 
91
#endif /* _exit_cleanup */
 
92
 
 
93
#endif  /* _lib_onexit */
 
94
 
 
95
#endif /* !_lib_atexit */
 
96
 
 
97
#if _lib_waitpid
 
98
int     _Sf_no_need_for_waitpid;
 
99
#else
 
100
 
 
101
/* we need to supply our own waitpid here so that sfpclose() can wait
 
102
** for the right process to die.
 
103
*/
 
104
typedef struct _wait_
 
105
{
 
106
        int             pid;
 
107
        int             status;
 
108
        struct _wait_*  next;
 
109
} Waitpid_t;
 
110
 
 
111
static Waitpid_t*       Wait;
 
112
 
 
113
#if __STD_C
 
114
waitpid(int pid, int* status, int options)
 
115
#else
 
116
waitpid(pid, status, options)
 
117
int     pid;
 
118
int*    status;
 
119
int     options;
 
120
#endif
 
121
{
 
122
        int             id, ps;
 
123
        Waitpid_t*      w;
 
124
        Waitpid_t*      last;
 
125
 
 
126
        /* we don't know options */
 
127
        if(options != 0)
 
128
                return -1;
 
129
 
 
130
        vtmtxlock(_Sfmutex);
 
131
 
 
132
        for(w = Wait, last = NIL(Waitpid_t*); w; last = w, w = w->next)
 
133
        {       if(pid > 0 && pid != w->pid)
 
134
                        continue;
 
135
 
 
136
                if(last)
 
137
                        last->next = w->next;
 
138
                else    Wait = w->next;
 
139
                if(status)
 
140
                        *status = w->status;
 
141
                pid = w->pid;
 
142
                free(w);
 
143
 
 
144
                vtmtxunlock(_Sfmutex);
 
145
                return pid;
 
146
        }
 
147
 
 
148
        while((id = wait(&ps)) >= 0)
 
149
        {       if(pid <= 0 || id == pid)
 
150
                {       if(status)
 
151
                                *status = ps;
 
152
 
 
153
                        vtmtxunlock(_Sfmutex);
 
154
                        return pid;
 
155
                }
 
156
 
 
157
                if(!(w = (Waitpid_t*)malloc(sizeof(Waitpid_t))) )
 
158
                        continue;
 
159
 
 
160
                w->pid = id;
 
161
                w->status = ps;
 
162
                w->next = Wait;
 
163
                Wait = w;
 
164
        }
 
165
 
 
166
        vtmtxunlock(_Sfmutex);
 
167
        return -1;
 
168
}
 
169
 
 
170
#endif /*_lib_waitpid*/
 
171
 
 
172
#endif /*!PACKAGE_ast*/