[Asterisk-Dev] fun with valgrind

Gilad Ben-Yossef gilad at codefidence.com
Tue May 10 08:46:23 MST 2005


Matthew Boehm wrote:

> /* substitute strings \t and \n into the apropriate characters */
> tmpread = tmpwrite = emailbody;
> while ((tmpwrite = strchr(tmpread,'\\'))) {
>     int len = strlen("\n");
>     switch (tmpwrite[1]) {
>         case 'n':
>             strncpy(tmpwrite+len,tmpwrite+2,strlen(tmpwrite+2)+1); <=== line
> 5545
>             strncpy(tmpwrite,"\n",len);
>             break;
>         case 't':
>             strncpy(tmpwrite+len,tmpwrite+2,strlen(tmpwrite+2)+1); <=== line
> 5549
>             strncpy(tmpwrite,"\t",len);
>             break;
>         default:
>             ast_log(LOG_NOTICE, "Substitution routine does not support this
> character: %c\n",tmpwrite[1]);
>     }
>     tmpread = tmpwrite+len;
> }
> 
> Could somebody explain what is going on there? It seems as if the code is
> replacing "\n" with "\n" and "\t" with "\t".
> 

That is exactly what it is doing - except that it replaces the literal 
characters '\' and 't', for example, with the ASCII code for tab, which 
in C is written '\t'.

In other words, the code in Asterisk that replcaes the characters "\t" 
with the ASCII value for a tab works thanks to the fact that  the C 
compiler does exactly the same thing in a kind of recursive way that 
reminds me of Scheme programs... :-)

Gilad

-- 
Gilad Ben-Yossef <gilad at codefidence.com>
Codefidence. A name you can trust(tm)
Web: http://codefidence.com  | SIP: gilad at pbx.codefidence.com
IL: +972.9.8650475 ext. 201  | Fax:            +972.9.8850643
US: +1.360.2275194 ext. 201  | Cel:           +972.52.8260388

"I am Jack's Overwritten Stack Pointer"
	-- Hackers Club, the movie



More information about the asterisk-dev mailing list