[1054] | 1 | /*
|
---|
| 2 | * $Id$
|
---|
| 3 | *
|
---|
| 4 | * Code (c)2006 Bruno Cornec <bruno@mondorescue.org>
|
---|
| 5 | *
|
---|
| 6 | * Main file of mr_mem : a very small and simple
|
---|
| 7 | * library for memory management
|
---|
| 8 | *
|
---|
| 9 | * Provided under the GPLv2
|
---|
| 10 | */
|
---|
| 11 |
|
---|
[1064] | 12 | #ifndef _GNU_SOURCE
|
---|
| 13 | #define _GNU_SOURCE
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
[1054] | 16 | #include <stdio.h>
|
---|
| 17 | #include <stdlib.h>
|
---|
| 18 |
|
---|
| 19 | #include "mr_err.h"
|
---|
| 20 | #include "mr_msg.h"
|
---|
| 21 |
|
---|
| 22 | /*
|
---|
| 23 | * Function that frees memory if necessary
|
---|
[1104] | 24 | * A pointer to the memory pointed is passed to it.
|
---|
| 25 | * *allocated variable points then to the original content
|
---|
| 26 | * pointed to by the caller
|
---|
[2241] | 27 | * In case of NULL pointer it just logs it
|
---|
[1054] | 28 | */
|
---|
[1104] | 29 | void mr_free_int(void **allocated, int line, const char *file) {
|
---|
[1054] | 30 |
|
---|
| 31 | /* free man pages says that if allocated is NULL
|
---|
| 32 | * nothing happens
|
---|
| 33 | */
|
---|
[1104] | 34 | if (*allocated != NULL) {
|
---|
| 35 | free(*allocated);
|
---|
| 36 | *allocated = NULL;
|
---|
| 37 | } else {
|
---|
[2241] | 38 | mr_msg_int(8,line,file,"Attempt to free NULL pointer");
|
---|
[1104] | 39 | }
|
---|
[1054] | 40 | }
|
---|
| 41 |
|
---|
| 42 | /* encapsulation function for malloc */
|
---|
[1104] | 43 | void *mr_malloc_int(size_t size, int line, const char *file) {
|
---|
[1054] | 44 |
|
---|
| 45 | void *ret;
|
---|
| 46 |
|
---|
| 47 | ret = malloc(size);
|
---|
| 48 | if (ret == NULL) {
|
---|
[1133] | 49 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_malloc\nExiting...");
|
---|
[1104] | 50 | mr_exit(-1,"Unable to alloc memory in mr_malloc");
|
---|
[1054] | 51 | }
|
---|
| 52 | return(ret);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /* encapsulation function for asprintf */
|
---|
[1104] | 56 | void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
|
---|
[1054] | 57 |
|
---|
| 58 | int res = 0;
|
---|
[1064] | 59 | va_list args;
|
---|
[1054] | 60 |
|
---|
[1064] | 61 | va_start(args,fmt);
|
---|
[1054] | 62 | res = vasprintf(strp, fmt, args);
|
---|
| 63 | if (res == -1) {
|
---|
[1133] | 64 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
|
---|
[1104] | 65 | mr_exit(-1,"Unable to alloc memory in mr_asprintf");
|
---|
[1054] | 66 | }
|
---|
[1064] | 67 | va_end(args);
|
---|
[1054] | 68 | }
|
---|
| 69 |
|
---|
[3127] | 70 | /* encapsulation function for getline */
|
---|
| 71 | void mr_getline_int(char **lineptr, FILE *fd, int line, const char *file) {
|
---|
| 72 |
|
---|
| 73 | ssize_t ret;
|
---|
| 74 | size_t n = 0;
|
---|
| 75 |
|
---|
| 76 | ret = getline(lineptr,&n,fd);
|
---|
| 77 | if ((ret == -1) && (! feof(fd))) {
|
---|
| 78 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_getline\nExiting...");
|
---|
| 79 | mr_exit(-1,"Unable to alloc memory in mr_getline");
|
---|
| 80 | }
|
---|
| 81 | /* We reached end of file, allocating empty string */
|
---|
| 82 | if (ret == -1) {
|
---|
| 83 | mr_asprintf_int(lineptr, line, file, "");
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[1054] | 87 | /*
|
---|
| 88 | * Function that properly allocates a string from another one
|
---|
| 89 | * freeing it before in any case
|
---|
| 90 | */
|
---|
[1104] | 91 | void mr_allocstr_int(char *alloc, const char *orig, int line, const char *file) {
|
---|
[1054] | 92 |
|
---|
[1174] | 93 | if (alloc != NULL) {
|
---|
| 94 | mr_free_int((void **)&alloc, line, file);
|
---|
| 95 | }
|
---|
[1104] | 96 | mr_asprintf_int(&alloc, line, file, orig);
|
---|
[1054] | 97 | }
|
---|
[1152] | 98 |
|
---|
| 99 | /*
|
---|
| 100 | * Function that properly put a variable in the environment
|
---|
| 101 | */
|
---|
| 102 | void mr_setenv_int(const char *name, const char *value, int line, char *file) {
|
---|
| 103 |
|
---|
| 104 | if (name == NULL) {
|
---|
| 105 | mr_msg_int(1,line,file,"Unable to setenv a NULL variable\nExiting...");
|
---|
| 106 | mr_exit(-1, "Unable to setenv a NULL variable");
|
---|
| 107 | }
|
---|
| 108 | if (value == NULL) {
|
---|
| 109 | mr_msg_int(1,line,file,"Unable to affect NULL to %s\nExiting...", name);
|
---|
| 110 | mr_exit(-1, "Unable to affect a NULL variable");
|
---|
| 111 | }
|
---|
[1156] | 112 | if (setenv(name, value, 1) != 0) {
|
---|
[1152] | 113 | mr_msg_int(1,line,file,"Unable to put %s in environment", name);
|
---|
| 114 | mr_exit(-1,"Unable to put in environment");
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
[1178] | 117 |
|
---|
| 118 | /*
|
---|
| 119 | * Equivalent function of strcat but safe
|
---|
| 120 | * from memory allocation point of view
|
---|
[1196] | 121 | * and richer from an interface point of view
|
---|
[1178] | 122 | */
|
---|
[1196] | 123 | void mr_strcat_int(char **in, int line, const char *file, const char *fmt, ...) {
|
---|
| 124 | char *fmt2 = NULL;
|
---|
| 125 | va_list args;
|
---|
| 126 | int res = 0;
|
---|
[1178] | 127 |
|
---|
[1196] | 128 | if (fmt == NULL) {
|
---|
[1178] | 129 | return;
|
---|
| 130 | }
|
---|
| 131 | if (in == NULL) {
|
---|
[1196] | 132 | mr_msg_int(1,line,file,"Unable to add to NULL pointer\nExiting...");
|
---|
[1178] | 133 | mr_exit(-1, "Unable to add to a NULL pointer");
|
---|
| 134 | }
|
---|
[1196] | 135 | va_start(args,fmt);
|
---|
[1178] | 136 | if (*in == NULL) {
|
---|
[1196] | 137 | res = vasprintf(in, fmt, args);
|
---|
| 138 | if (res == -1) {
|
---|
| 139 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
|
---|
| 140 | mr_exit(-1,"Unable to alloc memory in mr_strcat");
|
---|
| 141 | }
|
---|
[1178] | 142 | } else {
|
---|
[1196] | 143 | mr_asprintf_int(&fmt2, line, file, "%s%s", *in, fmt);
|
---|
[2308] | 144 | mr_free_int((void **)in,line,file);
|
---|
| 145 | res = vasprintf(in, fmt2, args);
|
---|
| 146 | if (res == -1) {
|
---|
| 147 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
|
---|
| 148 | mr_exit(-1,"Unable to alloc memory in mr_strcat");
|
---|
| 149 | }
|
---|
[1196] | 150 | mr_free_int((void **)&fmt2,line,file);
|
---|
[1178] | 151 | }
|
---|
[1196] | 152 | va_end(args);
|
---|
[1178] | 153 | }
|
---|
[1196] | 154 |
|
---|
| 155 |
|
---|