source: MondoRescue/branches/3.3/mondo/src/lib/mr_mem.c@ 3837

Last change on this file since 3837 was 3837, checked in by Bruno Cornec, 2 months ago

integrates mr_allocstr_int functions not used yet into mr_asprintf_int

  • Property svn:eol-style set to native
File size: 4.2 KB
RevLine 
[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]29void 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 {
[3645]38 mr_msg_int(98,line,file,"mr_free_int","Attempt to free NULL pointer");
[1104]39 }
[1054]40}
41
42/* encapsulation function for malloc */
[1104]43void *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) {
[3645]49 mr_msg_int(1,line,file,"mr_malloc_int","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
[3837]55/*
56 * Function that properly allocates a string from a format and params by encapsulating asprintf
57 * freeing it before in any case
58 */
[1104]59void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
[1054]60
61 int res = 0;
[1064]62 va_list args;
[1054]63
[3837]64 if ((strp != NULL) && (*strp != NULL)) {
65 mr_free_int((void **)strp, line, file);
66 }
67
[1064]68 va_start(args,fmt);
[1054]69 res = vasprintf(strp, fmt, args);
70 if (res == -1) {
[3645]71 mr_msg_int(1,line,file,"mr_asprintf_int","Unable to alloc memory in mr_asprintf\nExiting...");
[1104]72 mr_exit(-1,"Unable to alloc memory in mr_asprintf");
[1054]73 }
[1064]74 va_end(args);
[1054]75}
76
[3127]77/* encapsulation function for getline */
78void mr_getline_int(char **lineptr, FILE *fd, int line, const char *file) {
79
80 ssize_t ret;
81 size_t n = 0;
82
[3292]83 if (*lineptr != NULL) {
84 /* free a variable which should be already free */
[3643]85 mr_msg_int(1,line,file,"mr_getline_int","mr_getline found a non freed variable here");
[3294]86 mr_free_int((void **)lineptr,line,file);
[3292]87 }
[3127]88 ret = getline(lineptr,&n,fd);
[3208]89 if ((ret == -1) && (! feof(fd))) {
[3509]90 mr_msg_int(1,line,file,"mr_getline_int","Unable to alloc memory in mr_getline\nExiting...");
[3127]91 mr_exit(-1,"Unable to alloc memory in mr_getline");
92 }
[3208]93 /* We reached end of file, allocating empty string */
94 if (ret == -1) {
[3294]95 mr_free_int((void **)lineptr,line,file);
[3292]96 mr_asprintf_int(lineptr,line,file,"");
[3208]97 }
[3127]98}
99
[1152]100/*
101 * Function that properly put a variable in the environment
102 */
103void mr_setenv_int(const char *name, const char *value, int line, char *file) {
104
105 if (name == NULL) {
[3509]106 mr_msg_int(1,line,file,"mr_setenv_int","Unable to setenv a NULL variable\nExiting...");
[1152]107 mr_exit(-1, "Unable to setenv a NULL variable");
108 }
109 if (value == NULL) {
[3509]110 mr_msg_int(1,line,file,"mr_setenv_int","Unable to affect NULL to %s\nExiting...", name);
[1152]111 mr_exit(-1, "Unable to affect a NULL variable");
112 }
[1156]113 if (setenv(name, value, 1) != 0) {
[3509]114 mr_msg_int(1,line,file,"mr_setenv_int","Unable to put %s in environment", name);
[1152]115 mr_exit(-1,"Unable to put in environment");
116 }
117}
[1178]118
119/*
120 * Equivalent function of strcat but safe
121 * from memory allocation point of view
[1196]122 * and richer from an interface point of view
[1178]123 */
[1196]124void mr_strcat_int(char **in, int line, const char *file, const char *fmt, ...) {
125 char *fmt2 = NULL;
126 va_list args;
127 int res = 0;
[1178]128
[1196]129 if (fmt == NULL) {
[1178]130 return;
131 }
132 if (in == NULL) {
[3509]133 mr_msg_int(1,line,file,"mr_strcat_int","Unable to add to NULL pointer\nExiting...");
[1178]134 mr_exit(-1, "Unable to add to a NULL pointer");
135 }
[1196]136 va_start(args,fmt);
[1178]137 if (*in == NULL) {
[1196]138 res = vasprintf(in, fmt, args);
139 if (res == -1) {
[3509]140 mr_msg_int(1,line,file,"mr_strcat_int","Unable to alloc memory in mr_strcat\nExiting...");
[1196]141 mr_exit(-1,"Unable to alloc memory in mr_strcat");
142 }
[1178]143 } else {
[1196]144 mr_asprintf_int(&fmt2, line, file, "%s%s", *in, fmt);
[2308]145 mr_free_int((void **)in,line,file);
146 res = vasprintf(in, fmt2, args);
147 if (res == -1) {
[3509]148 mr_msg_int(1,line,file,"mr_strcat_int","Unable to alloc memory in mr_strcat\nExiting...");
[2308]149 mr_exit(-1,"Unable to alloc memory in mr_strcat");
150 }
[1196]151 mr_free_int((void **)&fmt2,line,file);
[1178]152 }
[1196]153 va_end(args);
[1178]154}
[1196]155
156
Note: See TracBrowser for help on using the repository browser.