Hello,

Andree Leidenfrost said on Mon, Jul 31, 2006 at 09:37:47PM +1000:

> > > To the contrary, e.g. using a function that submits
> > > things to 'sh -c' means we have a sane environment like a PATH and so
> > > forth.
> > 
> > Yeah, well ... that depends on whether you can presume the user does  
> > have a sane PATH variable.  I'm inclined to believe the opposite,  
> > actually.
> 
> Interesting territory we are entering here me thinks. Why would you be
> inclined to say that something as fundamental as the PATH variable can
> not be assumed to be sane?

Well, I would tend to also agree on the fact mondo should avoid trusting
too much users, when it doesn't make sense.

That's why I think we should use a config file to include all the
commands used by mondo, and provide the full path name to these
commands.
That fixes which one is used (and should be LSB/FSH compliant) and allow
for some exotic distro to change that conf file only to make it work.
Definitively for 3.0.x

> char *mr_stresc(const char *instr, const char *toesc, const char escchr) {
> 
>   char *inptr = NULL;
>   char *retstr = NULL;
>   char *retptr = NULL;
>   char *escptr = NULL;
>   int cnt = 0;
> 
>   inptr = (char *)instr;

You don't need to cast here.

Add: // Counting how many char to escape are in instr
>   while (*inptr != '\0') {
>     escptr = (char *)toesc;

You don't need to cast here either.

>     while (*escptr != '\0') {
>       if (*inptr == *escptr++) {

Add: // found it. No need to continue.
Also I would increment escptr separately (I don't like ++ on a same
line as something else ;-)

>       cnt++;
>       break;

>       }
>     }
>     *inptr++;

No * here needed.
>   }
>   inptr = (char *)instr;
You don't need to cast here either.
> 
>   retstr = (char *)malloc(strlen(inptr) + cnt + 1);
>   retptr = (char *)retstr;
You don't need to cast here either.
> 
>   while (*inptr != '\0') {
>     escptr = (char *)toesc;
You don't need to cast here either.
>     while (*escptr != '\0') {
>       if (*inptr == *escptr++) {
>       *retptr++ = escchr;

Rather: *retptr = ESCCHR;
                retptr++;
>       break;
>       }
>     }
>     *retptr++ = *inptr++;
Idem.
>   }
>   *retptr = '\0';
> 
>   return retstr;
> 
> }
> 
> 
> int main() {
> 
>   const char escchr = '\\';
#define ESCCHR '\\'
>   const char escape_list[3] = "`$\\";
>   char string[44] = "These need escaping: `$\\, these don't: abc.";
>   char *result;
> 
>   printf("Before: %s\n", string);
>   result = mr_stresc(string, escape_list, escchr);
>   printf("After:  %s\n", result);
>   free(result);
> 
>   return 0;
> 
> }

Seems good to me.

But not before 2.0.9 ;-)
Also could we begin maybe to put these new revised and correct functions
in new source files rather, so that we can purge the older files during
time ?
I'd suggest mr_string. for this function.

Bruno.
-- 
Linux Profession Lead EMEA  / Open Source Evangelist \        HP C&I EMEA IET
http://www.mondorescue.org / HP/Intel Solution Center \  http://hpintelco.net
Des infos sur Linux?  http://www.HyPer-Linux.org      http://www.hp.com/linux
La musique ancienne?  http://www.musique-ancienne.org http://www.medieval.org

Attachment: pgpUnWOS4Ta5f.pgp
Description: PGP signature

Reply via email to