Initialize Tizen 2.3
[external/shadow-utils.git] / libmisc / cleanup.c
1 /*
2  * Copyright (c) 2008       , Nicolas François
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the copyright holders or contributors may not be used to
14  *    endorse or promote products derived from this software without
15  *    specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
21  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include "prototypes.h"
36
37 /*
38  * The cleanup_functions stack.
39  */
40 #define CLEANUP_FUNCTIONS 10
41 static cleanup_function cleanup_functions[CLEANUP_FUNCTIONS];
42 static void * cleanup_function_args[CLEANUP_FUNCTIONS];
43
44 /*
45  * - Cleanup functions shall not fail.
46  * - You should register do_cleanups with atexit.
47  * - You should add cleanup functions to the stack with add_cleanup when
48  *   an operation is expected to be executed later, and remove it from the
49  *   stack with del_cleanup when it has been executed.
50  *
51  **/
52
53 /*
54  * do_cleanups - perform the actions stored in the cleanup_functions stack.
55  *
56  * It is intended to be used as:
57  *     atexit (do_cleanups);
58  */
59 void do_cleanups (void)
60 {
61         unsigned int i;
62
63         /* Make sure there were no overflow */
64         assert (NULL == cleanup_functions[CLEANUP_FUNCTIONS-1]);
65
66         i = CLEANUP_FUNCTIONS;
67         do {
68                 i--;
69                 if (cleanup_functions[i] != NULL) {
70                         cleanup_functions[i] (cleanup_function_args[i]);
71                 }
72         } while (i>0);
73 }
74
75 /*
76  * add_cleanup - Add a cleanup_function to the cleanup_functions stack.
77  */
78 void add_cleanup (cleanup_function pcf, /*@null@*/void *arg)
79 {
80         unsigned int i;
81         assert (NULL != pcf);
82
83         assert (NULL == cleanup_functions[CLEANUP_FUNCTIONS-2]);
84
85         /* Add the cleanup_function at the end of the stack */
86         for (i=0; NULL != cleanup_functions[i]; i++);
87         cleanup_functions[i] = pcf;
88         cleanup_function_args[i] = arg;
89 }
90
91 /*
92  * del_cleanup - Remove a cleanup_function from the cleanup_functions stack.
93  */
94 void del_cleanup (cleanup_function pcf)
95 {
96         unsigned int i;
97         assert (NULL != pcf);
98
99         /* Find the pcf cleanup function */
100         for (i=0; i<CLEANUP_FUNCTIONS; i++) {
101                 if (cleanup_functions[i] == pcf) {
102                         break;
103                 }
104         }
105
106         /* Make sure the cleanup function was found */
107         assert (i<CLEANUP_FUNCTIONS);
108
109         /* Move the rest of the cleanup functions */
110         for (; i<CLEANUP_FUNCTIONS; i++) {
111                 /* Make sure the cleanup function was specified only once */
112                 assert (cleanup_functions[i+1] != pcf);
113
114                 if (i == (CLEANUP_FUNCTIONS -1)) {
115                         cleanup_functions[i] = NULL;
116                         cleanup_function_args[i] = NULL;
117                 } else {
118                         cleanup_functions[i] = cleanup_functions[i+1];
119                         cleanup_function_args[i] = cleanup_function_args[i+1];
120                 }
121
122                 /* A NULL indicates the end of the stack */
123                 if (NULL == cleanup_functions[i]) {
124                         break;
125                 }
126         }
127 }
128