Mailing List Archive



Back to the month index Back to the list index

thoth@purplefrog.com
Wed, 02 Apr 1997 11:17:18 EST


Message-Id: <199704021617.LAA25866@aviator.cis.ufl.edu>
From: thoth@purplefrog.com
Subject: Re: [mSQL] answer: escaping chars in C 
Date: Wed, 02 Apr 1997 11:17:18 EST

David Sklar <sklar@student.net> ,in message <Pine.LNX.3.95.970401135337.5502E-1
        00000@juan-epstein.student.net>, wrote:

> ok, i answered my own question. here's the code I'm using...
> -dave
> http://www.student.net

  !Your program will die horribly after a few minutes or seconds of
operation! Well, in this application it is more likely to work fine in
almost every case. It is probable that the terminating 0 will not be
overwritten before the string is used and disposed of.

> --------
> #define CHARS_TO_ESCAPE "'\\"
> char *msqlEscape(char *pcOld) {
> char *pcNew = NULL;
> int iOld = 0, iNew = 0;
>
> pcNew = (char *) malloc(2*strlen(pcOld));
>
> while(iOld < strlen(pcOld)) {
> if (strchr(CHARS_TO_ESCAPE, pcOld[iOld]))
> pcNew[iNew++] = '\\';
> pcNew[iNew++] = pcOld[iOld++];
> }
>
> pcNew[iNew] = '\0';
> pcNew = realloc(pcNew,strlen(pcNew));

                          strlen(pcNew)+1, or iNew+1

> return(pcNew);
> }
> ----------

-- 
Bob Forsman                                   thoth@gainesville.fl.us
           http://www.gainesville.fl.us/~thoth/
--------------------------------------------------------------------------
To remove yourself from the Mini SQL mailing list send a message containing
"unsubscribe" to "unsubscribe" to msql-list-request@bunyip.com.  Send a message containing
"info msql-list" to majordomo@bunyip.com for info on monthly archives of
the list. For more help, mail owner-msql-list@bunyip.com NOT the msql-list!