1 | /* mr_file.c
|
---|
2 | *
|
---|
3 | * $Id$
|
---|
4 | *
|
---|
5 | * File management for mondo
|
---|
6 | * Code (c)2006 Bruno Cornec <bruno@mondorescue.org>
|
---|
7 | *
|
---|
8 | * Provided under the GPLv2
|
---|
9 | */
|
---|
10 |
|
---|
11 |
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <sys/types.h>
|
---|
14 | #include <sys/stat.h>
|
---|
15 |
|
---|
16 | #include "mr_err.h"
|
---|
17 | #include "mr_msg.h"
|
---|
18 |
|
---|
19 | /*open and read file: each call must be coupled with mr_conf_close
|
---|
20 | function: return 0 if success*/
|
---|
21 | FILE *mr_fopen_int(const char *path, const char *mode,int line, char *file) {
|
---|
22 | FILE *fd = NULL;
|
---|
23 |
|
---|
24 | if ((fd = fopen(path, mode)) == NULL) {
|
---|
25 | mr_msg_int(1,line,file,"Unable to open %s",path);
|
---|
26 | mr_exit(-1,"Exiting");
|
---|
27 | }
|
---|
28 | return(fd);
|
---|
29 | }
|
---|
30 |
|
---|
31 | void mr_fprintf_int(FILE *fd, int line, char *file, const char *fmt, ...) {
|
---|
32 |
|
---|
33 | va_list args;
|
---|
34 |
|
---|
35 | if (fd == NULL) {
|
---|
36 | mr_msg_int(1,line,file,"fd is NULL.\nShould NOT happen.");
|
---|
37 | mr_exit(-1,"Exiting");
|
---|
38 | }
|
---|
39 | va_start(args,fmt);
|
---|
40 | if (vfprintf(fd, fmt, args) < 0) {
|
---|
41 | mr_msg_int(1,line,file,"Unable to print to fd");
|
---|
42 | mr_exit(-1,"Exiting");
|
---|
43 | }
|
---|
44 | va_end(args);
|
---|
45 | }
|
---|
46 |
|
---|
47 | void mr_fclose_int(FILE **fd, int line, char *file) {
|
---|
48 |
|
---|
49 | if (fd == NULL) {
|
---|
50 | mr_msg_int(1,line,file,"fd is NULL.\nShould NOT happen.");
|
---|
51 | mr_exit(-1,"Exiting");
|
---|
52 | }
|
---|
53 | if (*fd == NULL) {
|
---|
54 | mr_msg_int(1,line,file,"File descriptor is NULL.\nShould NOT happen.");
|
---|
55 | mr_exit(-1,"Exiting");
|
---|
56 | }
|
---|
57 | if (fclose(*fd) < 0) {
|
---|
58 | mr_msg_int(1,line,file,"Unable to close File Descriptor");
|
---|
59 | }
|
---|
60 | *fd = NULL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void mr_mkdir_int(const char *pathname, mode_t mode, int line, char *file) {
|
---|
64 |
|
---|
65 | if (mkdir(pathname,mode) != 0) {
|
---|
66 | mr_msg_int(1,line,file,"Unable to create directory %s",pathname);
|
---|
67 | mr_exit(-1,"Exiting");
|
---|
68 | }
|
---|
69 | }
|
---|