[1088] | 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>
|
---|
[1100] | 13 | #include <sys/types.h>
|
---|
| 14 | #include <sys/stat.h>
|
---|
| 15 |
|
---|
[1088] | 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*/
|
---|
[1133] | 21 | FILE *mr_fopen_int(const char *path, const char *mode,int line, char *file) {
|
---|
[1088] | 22 | FILE *fd = NULL;
|
---|
| 23 |
|
---|
| 24 | if ((fd = fopen(path, mode)) == NULL) {
|
---|
[1135] | 25 | mr_msg_int(1,line,file,"Unable to open %s",path);
|
---|
[1088] | 26 | mr_exit(-1,"Exiting");
|
---|
| 27 | }
|
---|
| 28 | return(fd);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[1133] | 31 | void mr_fprintf_int(FILE *fd, int line, char *file, const char *fmt, ...) {
|
---|
[1088] | 32 |
|
---|
| 33 | va_list args;
|
---|
| 34 |
|
---|
| 35 | if (fd == NULL) {
|
---|
[1135] | 36 | mr_msg_int(1,line,file,"fd is NULL.\nShould NOT happen.");
|
---|
[1133] | 37 | mr_exit(-1,"Exiting");
|
---|
[1088] | 38 | }
|
---|
| 39 | va_start(args,fmt);
|
---|
| 40 | if (vfprintf(fd, fmt, args) < 0) {
|
---|
[1135] | 41 | mr_msg_int(1,line,file,"Unable to print to fd");
|
---|
[1133] | 42 | mr_exit(-1,"Exiting");
|
---|
[1088] | 43 | }
|
---|
| 44 | va_end(args);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[1133] | 47 | void mr_fclose_int(FILE **fd, int line, char *file) {
|
---|
[1088] | 48 |
|
---|
[1136] | 49 | if (fd == NULL) {
|
---|
[1135] | 50 | mr_msg_int(1,line,file,"fd is NULL.\nShould NOT happen.");
|
---|
[1133] | 51 | mr_exit(-1,"Exiting");
|
---|
[1088] | 52 | }
|
---|
[1133] | 53 | if (*fd == NULL) {
|
---|
[1135] | 54 | mr_msg_int(1,line,file,"File descriptor is NULL.\nShould NOT happen.");
|
---|
[1133] | 55 | mr_exit(-1,"Exiting");
|
---|
[1091] | 56 | }
|
---|
[1133] | 57 | if (fclose(*fd) < 0) {
|
---|
[1135] | 58 | mr_msg_int(1,line,file,"Unable to close File Descriptor");
|
---|
[1133] | 59 | }
|
---|
| 60 | *fd = NULL;
|
---|
[1088] | 61 | }
|
---|
[1100] | 62 |
|
---|
[1133] | 63 | void mr_mkdir_int(const char *pathname, mode_t mode, int line, char *file) {
|
---|
[1100] | 64 |
|
---|
| 65 | if (mkdir(pathname,mode) != 0) {
|
---|
[1135] | 66 | mr_msg_int(1,line,file,"Unable to create directory %s",pathname);
|
---|
[1100] | 67 | mr_exit(-1,"Exiting");
|
---|
| 68 | }
|
---|
| 69 | }
|
---|