[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>
|
---|
[3612] | 15 | #include <stdlib.h>
|
---|
| 16 | #include <errno.h>
|
---|
| 17 | #include <unistd.h>
|
---|
[1100] | 18 |
|
---|
[1088] | 19 | #include "mr_err.h"
|
---|
| 20 | #include "mr_msg.h"
|
---|
| 21 |
|
---|
| 22 | /*open and read file: each call must be coupled with mr_conf_close
|
---|
| 23 | function: return 0 if success*/
|
---|
[1133] | 24 | FILE *mr_fopen_int(const char *path, const char *mode,int line, char *file) {
|
---|
[1088] | 25 | FILE *fd = NULL;
|
---|
| 26 |
|
---|
| 27 | if ((fd = fopen(path, mode)) == NULL) {
|
---|
[3509] | 28 | mr_msg_int(1,line,file,"mr_fopen_int","Unable to open %s",path);
|
---|
[1088] | 29 | mr_exit(-1,"Exiting");
|
---|
| 30 | }
|
---|
| 31 | return(fd);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[1133] | 34 | void mr_fprintf_int(FILE *fd, int line, char *file, const char *fmt, ...) {
|
---|
[1088] | 35 |
|
---|
| 36 | va_list args;
|
---|
| 37 |
|
---|
| 38 | if (fd == NULL) {
|
---|
[3509] | 39 | mr_msg_int(1,line,file,"mr_fprintf_int","fd is NULL.\nShould NOT happen.");
|
---|
[1133] | 40 | mr_exit(-1,"Exiting");
|
---|
[1088] | 41 | }
|
---|
| 42 | va_start(args,fmt);
|
---|
| 43 | if (vfprintf(fd, fmt, args) < 0) {
|
---|
[3509] | 44 | mr_msg_int(1,line,file,"mr_fprintf_int","Unable to print to fd");
|
---|
[1133] | 45 | mr_exit(-1,"Exiting");
|
---|
[1088] | 46 | }
|
---|
| 47 | va_end(args);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[1133] | 50 | void mr_fclose_int(FILE **fd, int line, char *file) {
|
---|
[1088] | 51 |
|
---|
[1136] | 52 | if (fd == NULL) {
|
---|
[3509] | 53 | mr_msg_int(1,line,file,"mr_fclose_int","fd is NULL.\nShould NOT happen.");
|
---|
[1133] | 54 | mr_exit(-1,"Exiting");
|
---|
[1088] | 55 | }
|
---|
[1133] | 56 | if (*fd == NULL) {
|
---|
[3509] | 57 | mr_msg_int(1,line,file,"mr_fclose_int","File descriptor is NULL.\nShould NOT happen.");
|
---|
[1133] | 58 | mr_exit(-1,"Exiting");
|
---|
[1091] | 59 | }
|
---|
[1133] | 60 | if (fclose(*fd) < 0) {
|
---|
[3509] | 61 | mr_msg_int(1,line,file,"mr_fclose_int","Unable to close File Descriptor");
|
---|
[1133] | 62 | }
|
---|
| 63 | *fd = NULL;
|
---|
[1088] | 64 | }
|
---|
[1100] | 65 |
|
---|
[1133] | 66 | void mr_mkdir_int(const char *pathname, mode_t mode, int line, char *file) {
|
---|
[1100] | 67 |
|
---|
| 68 | if (mkdir(pathname,mode) != 0) {
|
---|
[3509] | 69 | mr_msg_int(1,line,file,"mr_mkdir_int","Unable to create directory %s",pathname);
|
---|
[1100] | 70 | mr_exit(-1,"Exiting");
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
[3612] | 73 |
|
---|
| 74 | /* Version of getcwd using dynamically allocated memory. Cf: man 3p getcwd */
|
---|
| 75 | char *mr_getcwd_int(int line, char *file) {
|
---|
| 76 |
|
---|
| 77 | long path_max;
|
---|
| 78 | size_t size;
|
---|
| 79 | char *buf;
|
---|
| 80 | char *ptr;
|
---|
| 81 |
|
---|
| 82 | path_max = pathconf(".", _PC_PATH_MAX);
|
---|
| 83 | if (path_max == -1) {
|
---|
| 84 | size = (size_t)512;
|
---|
| 85 | } else if (path_max > 10240) {
|
---|
| 86 | size = (size_t)10240;
|
---|
| 87 | } else {
|
---|
| 88 | size = (size_t)path_max;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | for (buf = ptr = NULL; ptr == NULL; size *= 2) {
|
---|
| 92 | if ((buf = realloc(buf, size)) == NULL) {
|
---|
| 93 | mr_msg_int(1,line,file,"mr_getcwd","Unable to realloc memory");
|
---|
| 94 | mr_exit(-1,"Exiting");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | ptr = getcwd(buf, size);
|
---|
| 98 | if (ptr == NULL && errno != ERANGE) {
|
---|
| 99 | mr_msg_int(1,line,file,"mr_getcwd","Unable to get current working directory");
|
---|
| 100 | mr_exit(-1,"Exiting");
|
---|
| 101 | }
|
---|
| 102 | return(buf);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|