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*/
|
---|
21 | FILE *mr_fopen(const char *path, const char *mode) {
|
---|
22 | FILE *fd = NULL;
|
---|
23 |
|
---|
24 | if ((fd = fopen(path, mode)) == NULL) {
|
---|
25 | mr_msg(0,"Unable to open %s",path);
|
---|
26 | mr_exit(-1,"Exiting");
|
---|
27 | }
|
---|
28 | return(fd);
|
---|
29 | }
|
---|
30 |
|
---|
31 | void mr_fprintf(FILE *fd, const char *fmt, ...) {
|
---|
32 |
|
---|
33 | va_list args;
|
---|
34 |
|
---|
35 | if (fd == NULL) {
|
---|
36 | mr_log_exit(-1,"fd is NULL.\nShould NOT happen.\nExiting");
|
---|
37 | }
|
---|
38 | va_start(args,fmt);
|
---|
39 | if (vfprintf(fd, fmt, args) < 0) {
|
---|
40 | mr_msg(0,"Unable to print '%s'",args);
|
---|
41 | }
|
---|
42 | va_end(args);
|
---|
43 | }
|
---|
44 |
|
---|
45 | void mr_fclose(FILE *fd) {
|
---|
46 |
|
---|
47 | if (fd == NULL) {
|
---|
48 | mr_log_exit(-1,"fd is NULL.\nShould NOT happen.\nExiting");
|
---|
49 | }
|
---|
50 | if (fclose(fd) < 0) {
|
---|
51 | mr_msg(0,"Unable to close fd");
|
---|
52 | }
|
---|
53 | fd = NULL;
|
---|
54 | }
|
---|
55 |
|
---|
56 | void mr_mkdir(const char *pathname, mode_t mode) {
|
---|
57 |
|
---|
58 | if (mkdir(pathname,mode) != 0) {
|
---|
59 | mr_msg("Unable to create directory %s",pathname);
|
---|
60 | mr_exit(-1,"Exiting");
|
---|
61 | }
|
---|
62 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.