source: MondoRescue/branches/3.2/mondo/src/lib/mr_file.c@ 3509

Last change on this file since 3509 was 3509, checked in by Bruno Cornec, 8 years ago
  • Change mr_msg_int interface by adding the FUNCTION macro
  • Use mr_msg in mr_system
  • Ability to use that new log system everywhere now
  • Property svn:eol-style set to native
File size: 1.6 KB
Line 
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*/
21FILE *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,"mr_fopen_int","Unable to open %s",path);
26 mr_exit(-1,"Exiting");
27 }
28 return(fd);
29}
30
31void 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,"mr_fprintf_int","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,"mr_fprintf_int","Unable to print to fd");
42 mr_exit(-1,"Exiting");
43 }
44 va_end(args);
45}
46
47void mr_fclose_int(FILE **fd, int line, char *file) {
48
49 if (fd == NULL) {
50 mr_msg_int(1,line,file,"mr_fclose_int","fd is NULL.\nShould NOT happen.");
51 mr_exit(-1,"Exiting");
52 }
53 if (*fd == NULL) {
54 mr_msg_int(1,line,file,"mr_fclose_int","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,"mr_fclose_int","Unable to close File Descriptor");
59 }
60 *fd = NULL;
61}
62
63void 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,"mr_mkdir_int","Unable to create directory %s",pathname);
67 mr_exit(-1,"Exiting");
68 }
69}
Note: See TracBrowser for help on using the repository browser.