Changeset 1074 in MondoRescue for trunk/mondo/src/lib


Ignore:
Timestamp:
Jan 25, 2007, 4:18:12 PM (17 years ago)
Author:
Bruno Cornec
Message:

This version of trunk desn't seg fault on mr_msg anymore.
Still not ready for 3.0.0 but improvements ongoing

Location:
trunk/mondo/src/lib
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/src/lib/mr_conf.c

    r900 r1074  
    3434#define MRCONF_OPEN_OPENED          0xA
    3535#define MRCONF_CALL_BUT_NOT_OPEN    0xB
     36#define MRCONF_STRING_QUOTE         0xC
    3637
    3738/*setting flags*/
     
    5657#define MRCONF_STR_CLOSE_BUT_NOT_OPEN _("attempt to close mr_conf but it has not been opened yet")
    5758#define MRCONF_STR_CALL_BUT_NOT_OPEN _("attempt to use mr_conf when it has not been opened yet")
     59#define MRCONF_STR_STRING_QUOTE     _("string should be surrounded by quotes")
    5860
    5961/*Flags of internal state*/
     
    6668static size_t mr_conf_filesize(const char *name);
    6769static void mr_conf_error_msg(int error_code, const char *add_line);
    68 static void mr_conf_remove_comments();
     70static void mr_conf_remove_comments(void);
    6971static int mr_conf_check_int_flag(const int flag);
    7072static void mr_conf_set_int_flag(const int flag);
     
    112114
    113115    /*allocate memory for the buffers */
    114     buffer = (char *) malloc(sizeof(char) * (length + 1));
     116    buffer = (char *) mr_malloc(sizeof(char) * (length + 1));
    115117
    116118    if (buffer == NULL) {
     
    139141
    140142/*release all memory and prepare to the next possiable config file*/
    141 void mr_conf_close() {
     143void mr_conf_close(void) {
    142144    /* if not opened => error */
    143145    if (!mr_conf_check_int_flag(MRCONF_INTFLAG_OPEN)) {
     
    233235    mr_asprintf(&q, p);
    234236
     237    if (*p != '"') {
     238        mr_conf_error_msg(MRCONF_STRING_QUOTE, "");
     239        return (NULL);
     240    }
     241    p++;
     242
    235243    /* trunk at first \n */
    236244    r = index(q,'\n');
     245    r--;
     246    if (*r != '"') {
     247        mr_conf_error_msg(MRCONF_STRING_QUOTE, "");
     248        return (NULL);
     249    }
     250    r--;
    237251
    238252    size = r-q+1;
    239253    /*copy filtered data to the buffer */
    240     ret = (char *) malloc(sizeof(char) * (size));
     254    ret = (char *) mr_malloc(sizeof(char) * (size));
    241255    if (ret == NULL) {
    242256        mr_conf_error_msg(MRCONF_ALLOC_FAILED, "");
     
    249263    }
    250264
    251     ret[i] = (char) 0;      /*and set its length */
     265    ret[i] = (char) 0;      /* and set its length */
    252266    mr_free(q);
    253267
     
    278292    }
    279293    /* k is new buffer length now */
    280     tmp_buf = (char *) malloc(sizeof(char) * (k + 1));
     294    tmp_buf = (char *) mr_malloc(sizeof(char) * (k + 1));
    281295    if (tmp_buf == NULL) {
    282296        mr_conf_error_msg(MRCONF_ALLOC_FAILED, "");
     
    371385            break;
    372386
     387        case MRCONF_STRING_QUOTE:
     388            mr_msg(0,"%s %s\n", MRCONF_STR_ERROR, MRCONF_STR_STRING_QUOTE);
     389            break;
     390
    373391        default:
    374392            mr_msg(1,"%s %s\n", MRCONF_STR_ERROR, MRCONF_STR_DEFAULT_ERROR);
  • trunk/mondo/src/lib/mr_err.c

    r900 r1074  
    99 * Provided under the GPLv2
    1010 */
     11
     12#ifndef _GNU_SOURCE
     13#define _GNU_SOURCE
     14#endif
    1115
    1216#include <stdio.h>
     
    3640
    3741void mr_log_exit(int errorcode, const char *message) {
    38     mr_msg(0,message);
     42    mr_msg(0, message);
    3943    mr_exit(errorcode, message);
    4044}
  • trunk/mondo/src/lib/mr_mem.c

    r929 r1074  
    1010 */
    1111
     12#ifndef _GNU_SOURCE
     13#define _GNU_SOURCE
     14#endif
     15
    1216#include <stdio.h>
    1317#include <stdlib.h>
    14 #include <stdarg.h>
    1518
    1619#include "mr_err.h"
     
    5255}
    5356
    54 
    55 /* encapsulation function for vasprintf */
    56 void mr_vasprintf(char **strp, const char *fmt, va_list ap) {
    57 
    58     int res = 0;
    59 
    60     res = vasprintf(strp, fmt, ap);
    61     if (res == -1) {
    62         mr_log_exit(-1,"Unable to alloc memory in mr_vasprintf\nExiting...");
    63     }
    64 }
    65 
    6657/* encapsulation function for asprintf */
    6758void mr_asprintf(char **strp, const char *fmt, ...) {
     
    7061    va_list args;
    7162
    72     va_start(args, fmt);
    73     res = asprintf(strp, fmt, args);
     63    va_start(args,fmt);
     64    res = vasprintf(strp, fmt, args);
    7465    if (res == -1) {
    7566        mr_log_exit(-1,"Unable to alloc memory in mr_asprintf\nExiting...");
  • trunk/mondo/src/lib/mr_msg.c

    r900 r1074  
    1010 */
    1111
     12#ifndef _GNU_SOURCE
     13#define _GNU_SOURCE
     14#endif
    1215#include <stdio.h>
    1316#include <stdarg.h>
     17#include <stdlib.h>
    1418
    1519static int mr_loglevel = 0;
    1620static char *mr_logfile = NULL;
    1721
     22/*
     23 * This function is in the lowest part of the tree
     24 * It should not depend on any other function of the mr lib
     25 */
     26
     27/* Cleanup function for messages */
     28void mr_msg_close(void) {
     29    free(mr_logfile);
     30    mr_logfile = NULL;
     31    mr_loglevel = 0;
     32}
     33
    1834/* Initialization function for messages */
    19 void mr_msg_init(void) {
     35void mr_msg_init(const char *configfile, int loglevel) {
     36    FILE *fout = NULL;
     37    int res = 0;
    2038
     39    asprintf(&mr_logfile,configfile);
     40    if ((fout = fopen(mr_logfile, "w")) == NULL) {
     41        fprintf(stderr,"Unable to write to %s\n",mr_logfile);
     42        fprintf(stderr,"Logging desactivated\n");
     43        mr_msg_close();
     44    }
     45    if ((res = fclose(fout)) != 0) {
     46        fprintf(stderr,"Unable to close %s\n",mr_logfile);
     47    }
     48    mr_loglevel = loglevel;
    2149}
    2250
    2351/*
    2452 * Function that log a message. Not called directly
    25  * but through macros in mr_msg.h
     53 * but through other functions
    2654 */
    27 void _mr_msg(int debug, const char *file, const char *function, int line, const char *fmt, ...) {
     55void mr_msg(int debug, const char *fmt, ...) {
    2856
    29     va_list args;
    3057    int i = 0;
    3158    int res = 0;
    3259    FILE *fout = NULL;
     60    va_list args;
     61
     62    if (mr_logfile == NULL) {
     63        return;
     64    }
    3365
    3466    if (debug <= mr_loglevel) {
    35         va_start(args, fmt);
    36         if (!(fout = fopen(mr_logfile, "a"))) {
     67        if ((fout = fopen(mr_logfile, "a")) == NULL) {
    3768            fprintf(stderr,"Unable to append to %s\n",mr_logfile);
    3869            return;
     
    4374            for (i = 1; i < debug; i++)
    4475                fprintf(fout, "  ");
    45             fprintf(fout, "%s->%s#%d: ", file, function, line);
     76            fprintf(fout, "%s->%s#%d: ", __FILE__, __FUNCTION__, __LINE__);
    4677        }
    47         vfprintf(fout, fmt, args);
     78        va_start(args,fmt);
     79        if (vfprintf(fout, fmt, args) < 0) {
     80            fprintf(stderr,"Unable to print to %s\n",mr_logfile);
     81        }
     82        va_end(args);
    4883
    49         va_end(args);
    50         fprintf(fout, "\n");
    5184        if ((res = fclose(fout)) != 0) {
    5285            fprintf(stderr,"Unable to close %s\n",mr_logfile);
  • trunk/mondo/src/lib/mr_str.c

    r969 r1074  
    88#include <stdio.h>
    99#include <string.h>
     10
     11#include "mr_mem.h"
    1012 
    1113/**
     
    3537    strptr += pos2;
    3638    pos1 = strcspn(strptr, delims);
    37     token = malloc(sizeof(*token) * (pos1 + 1));
     39    token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
    3840    strncpy(token, strptr, pos1);
    3941    token[pos1] = '\0';
     
    8082    inptr = instr;
    8183
    82     retstr = (char *) malloc(strlen(inptr) + cnt + 1);
     84    retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
    8385    retptr = retstr;
    8486
Note: See TracChangeset for help on using the changeset viewer.