Changeset 3614 in MondoRescue for branches


Ignore:
Timestamp:
Nov 18, 2016, 5:31:43 PM (7 years ago)
Author:
Bruno Cornec
Message:

mr_strip_spaces now returns a dynamically allocated string

Location:
branches/3.2/mondo
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mondo/src/common/libmondo-devices.c

    r3610 r3614  
    2424#include "libmondo-stream-EXT.h"
    2525
    26 extern void mr_strip_spaces(char *);
    27 
    28 #include <sys/types.h>
    2926#ifdef __FreeBSD__
    3027#define DKTYPENAMES
     
    10401037    } else {
    10411038        mr_getline(p, pin);
    1042         mr_asprintf(good_formats, " %s",p);
     1039        good_formats = mr_strip_spaces(p);
    10431040        mr_free(p);
    10441041        (void)pclose(pin);
    1045         mr_strip_spaces(good_formats);
    10461042        mr_strcat(good_formats, " swap lvm raid ntfs-3g ntfs 7 ");  // " ntfs 7 " -- um, cheating much? :)
    10471043        if (strstr(good_formats, format_sz)) {
     
    24612457            }
    24622458            mr_free(bkpinfo->netfs_mount);
    2463             mr_asprintf(bkpinfo->netfs_mount, "%s", p);
     2459            // check whether already mounted - we better remove
     2460            // surrounding spaces and trailing '/' for this
     2461            bkpinfo->netfs_mount = mr_strip_spaces(p);
    24642462            if (!bkpinfo->restore_data) {
    24652463                if ((compression_type = which_compression_type()) == NULL) {
     
    24732471                }
    24742472            }
    2475             // check whether already mounted - we better remove
    2476             // surrounding spaces and trailing '/' for this
    2477             mr_strip_spaces(bkpinfo->netfs_mount);
    24782473            if (bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] == '/')
    24792474                bkpinfo->netfs_mount[strlen(bkpinfo->netfs_mount) - 1] = '\0';
     
    26072602        }
    26082603        mr_free(bkpinfo->netfs_remote_dir);
    2609         mr_asprintf(bkpinfo->netfs_remote_dir, "%s", tmp1);
    2610 
    26112604        // check whether writable - we better remove surrounding spaces for this
    2612         mr_strip_spaces(bkpinfo->netfs_remote_dir);
     2605        bkpinfo->netfs_remote_dir = mr_strip_spaces(tmp1);
    26132606
    26142607        if (!popup_and_get_string("Prefix.", "Please enter the prefix that will be prepended to your ISO filename.  Example: machine1 to obtain machine1-[1-9]*.iso files", bkpinfo->prefix, MAX_STR_LEN / 4)) {
  • branches/3.2/mondo/src/common/libmondo-fork-EXT.h

    r1999 r3614  
    11/* libmondo-fork-EXT.h */
    22
    3 extern char *call_program_and_get_last_line_of_output(char *call);
     3extern char *call_program_and_get_last_line_of_output(const char *call);
    44extern int run_program_and_log_to_screen(char *basic_call,
    55                                         char *what_i_am_doing);
  • branches/3.2/mondo/src/common/libmondo-raid.c

    r3610 r3614  
    426426    char *incoming = NULL;
    427427    char *p = NULL;
     428    char *res = NULL;
    428429
    429430    assert(fin != NULL);
     
    433434    }
    434435
    435     for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
    436         log_msg(10,"Found in raidtab line: %s",incoming);
    437         mr_strip_spaces(incoming);
     436    for (mr_getline(res, fin); !feof(fin); mr_getline(res, fin)) {
     437        log_msg(10,"Found in raidtab line: %s",res);
     438        incoming = mr_strip_spaces(res);
     439        mr_free(res);
    438440        p = strchr(incoming, ' ');
    439441        if (strlen(incoming) < 3 || incoming[0] == '#' || !p) {
  • branches/3.2/mondo/src/common/libmondo-string.c

    r3611 r3614  
    9292    }
    9393    /*  skip initial spaces */
    94     mr_asprintf(scratch, "%s", in);
    95     mr_strip_spaces(scratch);
     94    scratch = mr_strip_spaces(in);
    9695    len = (int)strlen(scratch);
    9796    mid = width / 2;
  • branches/3.2/mondo/src/common/newt-specific.c

    r3508 r3614  
    12341234        static char *possible_responses[] = { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev", "netfs", "iso", NULL };
    12351235        char *outstr = NULL;
     1236        char *instr = NULL;
    12361237        t_bkptype backup_type;
    12371238        int i;
     
    12561257                }
    12571258                printf(")\n--> ");
    1258                 mr_getline(outstr, stdin);
    1259                 mr_strip_spaces(outstr);
     1259                mr_getline(instr, stdin);
     1260                outstr = mr_strip_spaces(instr);
     1261                mr_free(instr);
    12601262                for (i = 0; possible_responses[i]; i++) {
    12611263                    if (!strcmp(possible_responses[i], outstr)) {
  • branches/3.2/mondo/src/include/mr_str.h

    r3374 r3614  
    1818extern char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr);
    1919extern inline char *mr_date(void);
    20 extern void mr_strip_spaces(char *in_out);
     20extern char *mr_strip_spaces(const char *instr);
    2121extern char *mr_str_substitute(const char *in, const char *token, const char *subst);
    2222/*
  • branches/3.2/mondo/src/lib/mr_str.c

    r3566 r3614  
    2525 * @note this function allocates memory that needs to be freed by caller
    2626 **/
    27 char *mr_strtok(char *instr, const char *delims, int *lastpos)
    28 {
    29 
    30     char *token = NULL;
    31     char *strptr = NULL;
    32     size_t pos1 = 0;
    33     size_t pos2 = 0;
    34 
    35     if (instr == NULL) {
    36         *lastpos = 0;
    37         return token;
    38     }
    39 
    40     if (delims == NULL) {
    41         *lastpos = 0;
    42         return token;
    43     }
    44 
    45     if (strlen(instr) <= *lastpos) {
    46         *lastpos = 0;
    47         return token;
    48     }
    49 
    50     strptr = instr + *lastpos;
    51     pos2 = strspn(strptr, delims);
    52     strptr += pos2;
    53     pos1 = strcspn(strptr, delims);
    54     token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
    55     strncpy(token, strptr, pos1);
    56     token[pos1] = '\0';
    57     *lastpos = *lastpos + pos1 + pos2 + 1;
    58 
     27char *mr_strtok(char *instr, const char *delims, int *lastpos) {
     28
     29char *token = NULL;
     30char *strptr = NULL;
     31size_t pos1 = 0;
     32size_t pos2 = 0;
     33
     34if (instr == NULL) {
     35    *lastpos = 0;
    5936    return token;
     37}
     38
     39if (delims == NULL) {
     40    *lastpos = 0;
     41    return token;
     42}
     43
     44if (strlen(instr) <= *lastpos) {
     45    *lastpos = 0;
     46    return token;
     47}
     48
     49strptr = instr + *lastpos;
     50pos2 = strspn(strptr, delims);
     51strptr += pos2;
     52pos1 = strcspn(strptr, delims);
     53token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
     54strncpy(token, strptr, pos1);
     55token[pos1] = '\0';
     56*lastpos = *lastpos + pos1 + pos2 + 1;
     57
     58return token;
    6059}
    6160
     
    7170 **/
    7271char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
    73     char *inptr = NULL;
    74     char *retstr = NULL;
    75     char *retptr = NULL;
    76     char *escptr = NULL;
    77     int cnt = 0;
    78     bool found = FALSE;
    79 
    80     inptr = instr;
    81     // Count how many characters need escaping.
    82     while (*inptr != '\0') {
    83         escptr = toesc;
    84         while (*escptr != '\0') {
    85             if (*inptr == *escptr) {
    86                 // Found it, skip the rest.
    87                 cnt++;
    88                 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
    89                 if (*inptr == specialchr) {
    90                     cnt += 2;
    91                 }
    92                 break;
     72
     73char *inptr = NULL;
     74char *retstr = NULL;
     75char *retptr = NULL;
     76char *escptr = NULL;
     77int cnt = 0;
     78bool found = FALSE;
     79
     80inptr = instr;
     81// Count how many characters need escaping.
     82while (*inptr != '\0') {
     83    escptr = toesc;
     84    while (*escptr != '\0') {
     85        if (*inptr == *escptr) {
     86            // Found it, skip the rest.
     87            cnt++;
     88            // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
     89            if (*inptr == specialchr) {
     90                cnt += 2;
    9391            }
    94             escptr++;
     92            break;
    9593        }
    96         inptr++;
    97     }
    98 
    99     inptr = instr;
    100     retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
    101     retptr = retstr;
    102 
    103     // Prepend specified characters with escape character.
    104     while (*inptr != '\0') {
    105         escptr = toesc;
    106         while (*escptr != '\0') {
    107             if (*inptr == *escptr) {
    108                 // Found it, skip the rest.
    109                 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
    110                 if (*inptr == specialchr) {
    111                     *retptr = specialchr;
    112                     retptr++;
    113                     *retptr = escchr;
    114                     retptr++;
    115                     found = TRUE;
    116                 } else {
    117                     *retptr = escchr;
    118                     retptr++;
    119                 }
    120                 break;
     94        escptr++;
     95    }
     96    inptr++;
     97}
     98
     99inptr = instr;
     100retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
     101retptr = retstr;
     102
     103// Prepend specified characters with escape character.
     104while (*inptr != '\0') {
     105    escptr = toesc;
     106    while (*escptr != '\0') {
     107        if (*inptr == *escptr) {
     108            // Found it, skip the rest.
     109            // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
     110            if (*inptr == specialchr) {
     111                *retptr = specialchr;
     112                retptr++;
     113                *retptr = escchr;
     114                retptr++;
     115                found = TRUE;
     116            } else {
     117                *retptr = escchr;
     118                retptr++;
    121119            }
    122             escptr++;
     120            break;
    123121        }
    124         *retptr = *inptr;
     122        escptr++;
     123    }
     124    *retptr = *inptr;
     125    retptr++;
     126    inptr++;
     127    if (found) {
     128        // finish to put the remaining specialchr
     129        *retptr = specialchr;
    125130        retptr++;
    126         inptr++;
    127         if (found) {
    128             // finish to put the remaining specialchr
    129             *retptr = specialchr;
    130             retptr++;
    131             found = FALSE;
    132         }
    133     }
    134     *retptr = '\0';
    135 
    136     return retstr;
     131        found = FALSE;
     132    }
     133}
     134*retptr = '\0';
     135
     136return(retstr);
    137137}
    138138
    139139/* Return a string containing the date */
    140140char *mr_date(void) {
    141    
    142     time_t tcurr;
    143 
    144     tcurr = time(NULL);
    145     return(ctime(&tcurr));
     141
     142time_t tcurr;
     143
     144tcurr = time(NULL);
     145return(ctime(&tcurr));
    146146}
    147147
     
    161161/**
    162162 * Remove all characters whose ASCII value is less than or equal to 32
    163  * (spaces and control characters) from both sides of @p in_out.
    164  * @param in_out The string to strip spaces/control characters from (modified).
     163 * (spaces and control characters) from both sides of @p instr.
     164 * @param instr The string to strip spaces/control characters from
     165 * returns a newly allocated string without the spaces that needs to be freed
     166 * by the caller
    165167 */
    166 void mr_strip_spaces(char *in_out) {
    167     int i = 0;
    168     int j = 0;
    169     size_t length;
    170 
    171     if (in_out == NULL) {
    172         return;
    173     }
    174     length = strlen(in_out);
    175 
    176     /* Skip initial spaces and special chars */
    177     for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
    178     /* Shift the string to the begining if needed */
    179     if (i != 0) {
    180         for (j = 0; i < (int)length ; i++, j++) {
    181             in_out[j] = in_out[i];
    182         }
    183         /* Erase the end of the string if needed */
    184         in_out[j] = '\0';
    185     }
    186 
    187     /* Skip final spaces and special chars */
    188     for (i = (int)strlen(in_out) - 1; i >= 0  && in_out[i] <= ' '; i--);
    189 
    190     /* The string now ends after that char */
    191     i++;
    192     in_out[i] = '\0';
     168char *mr_strip_spaces(const char *instr) {
     169
     170char *p = NULL;
     171char *q = NULL;
     172char *outstr = NULL;
     173
     174if (instr == NULL) {
     175    return(NULL);
     176}
     177p = instr;
     178
     179/* Skip initial spaces and special chars */
     180while (*p <= ' ' && *p != '\0') {
     181    p++;
     182}
     183mr_asprintf(outstr, "%s", p);
     184q = outstr + strlen(outstr) -1;
     185
     186/* Skip final spaces and special chars */
     187while (*q <= ' ' && q != outstr) {
     188    q--;
     189}
     190
     191/* The string now ends after that char */
     192q++;
     193*q = '\0';
     194return(outstr);
    193195}
    194196
  • branches/3.2/mondo/src/mondorestore/mondo-rstr-newt.c

    r3611 r3614  
    198198        b_res = newtRunForm(myForm);
    199199        mr_free(device_str);
    200         mr_asprintf(device_str, "%s", device_here);
     200        device_str = mr_strip_spaces(device_here);
    201201
    202202        mr_free(mountpoint_str);
    203         mr_asprintf(mountpoint_str, "%s", mountpoint_here);
    204         mr_strip_spaces(mountpoint_str);
     203        mountpoint_str = mr_strip_spaces(mountpoint_here);
    205204
    206205        mr_free(format_str);
    207         mr_asprintf(format_str, "%s", format_here);
    208         mr_strip_spaces(format_str);
     206        format_str = mr_strip_spaces(format_here);
    209207
    210208        mr_free(size_str);
    211         mr_asprintf(size_str, "%s", size_here);
    212         mr_strip_spaces(size_str);
    213 
    214         mr_strip_spaces(device_str);
     209        size_str = mr_strip_spaces(size_here);
     210
    215211        if (b_res == bOK) {
    216212            if (device_str[strlen(device_str) - 1] == '/') {
     
    12081204        b_res = newtRunForm(myForm);
    12091205        mr_free(device_str);
    1210         mr_asprintf(device_str, "%s", device_here);
    1211         mr_strip_spaces(device_str);
     1206        device_str = mr_strip_spaces(device_here);
    12121207
    12131208        mr_free(mountpoint_str);
    1214         mr_asprintf(mountpoint_str, "%s", mountpoint_here);
    1215         mr_strip_spaces(mountpoint_str);
     1209        mountpoint_str = mr_strip_spaces(mountpoint_here);
    12161210
    12171211        mr_free(format_str);
    1218         mr_asprintf(format_str, "%s", format_here);
    1219         mr_strip_spaces(format_str);
     1212        format_str = mr_strip_spaces(format_here);
    12201213
    12211214        if (b_res == bOK && strstr(device_str, RAID_DEVICE_STUB)
     
    12341227            && strcmp(mountlist->el[currline].mountpoint, "image")) {
    12351228            mr_free(size_str);
    1236             mr_asprintf(size_str, "%s", size_here);
    1237             mr_strip_spaces(size_str);
     1229            size_str = mr_strip_spaces(size_here);
    12381230        } else {
    12391231            mr_asprintf(size_str, "%ld", calculate_raid_device_size(mountlist, raidlist, mountlist->el[currline].device) / 1024);
  • branches/3.2/mondo/src/mondorestore/mondo-rstr-tools.c

    r3613 r3614  
    140140    fatal_error("Cannot openin outfname");
    141141}
    142 for (mr_getline(incoming, fin); !feof(fin) && (incoming != NULL); mr_getline(incoming, fin)) {
    143     mr_strip_spaces(incoming);
     142for (mr_getline(p, fin); !feof(fin) && (p != NULL); mr_getline(p, fin)) {
     143    incoming = mr_strip_spaces(p);
     144    mr_free(p);
    144145
    145146    if (incoming[0] == '\0') {
  • branches/3.2/mondo/test/Makefile.am

    r3564 r3614  
    44## The program
    55mrtestdir                 = $(pkglibdir)/test
    6 mrtest_PROGRAMS           = mrtest_mountlist mrtest_truncname mrtest_stresc mrtest_str_substitute mrtest_mr_system
     6mrtest_PROGRAMS           = mrtest_mountlist mrtest_truncname mrtest_stresc mrtest_str_substitute mrtest_strip_spaces mrtest_mr_system
    77
    88mrtest_mountlist_SOURCES  = test-mountlist.c ${top_builddir}/src/mondorestore/mondo-prep.c ${top_builddir}/src/mondorestore/mondo-rstr-newt.c ${top_builddir}/src/mondorestore/mondo-rstr-tools.c ${top_builddir}/src/mondorestore/mondo-rstr-mountlist.c
     
    1818mrtest_str_substitute_LDADD   = ${top_builddir}/src/lib/libmr.a @MONDORESTORE_STATIC@
    1919
     20mrtest_strip_spaces_SOURCES = test-mr_strip_spaces.c
     21mrtest_strip_spaces_LDADD   = ${top_builddir}/src/lib/libmr.a @MONDORESTORE_STATIC@
     22
    2023mrtest_mr_system_SOURCES  = test-mr_system.c
    2124mrtest_mr_system_LDADD    = ${top_builddir}/src/lib/libmr.a @MONDORESTORE_STATIC@
Note: See TracChangeset for help on using the changeset viewer.