]> granicus.if.org Git - fcron/blob - filesubs.c
Moved to version 3.1.2
[fcron] / filesubs.c
1 /*
2  * FCRON - periodic command scheduler 
3  *
4  *  Copyright 2000-2012 Thibault Godouet <fcron@free.fr>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  * 
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  *  The GNU General Public License can also be found in the file
21  *  `LICENSE' that comes with the fcron source distribution.
22  */
23
24
25 #include "global.h"
26 #include "filesubs.h"
27
28 /* close() a file, and set the FD to -1.
29  * Returns close()'s return value and leaves errno as is. */
30 int
31 xclose(int *fd)
32 {
33     int retval = -1;
34
35     if (*fd != -1) {
36         retval = close(*fd);
37         *fd = -1;
38     }
39
40     return retval;
41 }
42
43 /* close() a file, and set the FD to -1. Check for errors and log them.
44  * Returns close()'s return value.
45  * WARNING: do NOT call from log.c to avoid potential infinite loops! */
46 int
47 xclose_check(int *fd, const char *filedesc)
48 {
49     int retval = -1;
50
51     if (*fd != -1) {
52         retval = close(*fd);
53         if (retval != 0) {
54             error_e("Error while closing %s", filedesc);
55         }
56         *fd = -1;
57     }
58
59     return retval;
60 }
61
62 /* fclose() a file, and set the FILE* to NULL.
63  * Returns fclose()'s return value and leaves errno as is. */
64 int
65 xfclose(FILE **f)
66 {
67     int retval = EOF;
68
69     if (f == NULL) {
70         return retval;
71     }
72
73     if (*f != NULL) {
74         retval = fclose (*f);
75         *f = NULL;
76     }
77
78     return retval;
79 }
80
81 /* fclose() a file, and set the FILE* to NULL. Check for errors and log them.
82  * Returns fclose()'s return value.
83  * WARNING: do NOT call from log.c to avoid potential infinite loops! */
84 int
85 xfclose_check(FILE **f, const char *filedesc)
86 {
87     int retval = EOF;
88
89     if (f == NULL) {
90         return retval;
91     }
92
93     if (*f != NULL) {
94         retval = fclose (*f);
95         if (retval != 0) {
96             error_e("Error while fclosing %s", filedesc);
97         }
98         *f = NULL;
99     }
100
101     return retval;
102 }
103