[NTLUG:Discuss] Continuation of pointer questions

Greg Edwards greg at nas-inet.com
Thu Nov 28 12:10:53 CST 2002


Fred James wrote:


>     pC = (char*) malloc(strlen(pAttach) + strlen(pMessage) +10);
> [omitted code for brevity]
>         sprintf(mailCmd,
>             "/usr/sbin/mailx -s '%s' %s", pSubject, pRecipientList);
> [omitted code for brevity]
>     strcpy (pC,"~| uuencode ");
>     strcat (pC,pAttach);
>     strcat (pC,"\n");
>     strcat (pC,pMessage);
>         if ( fwrite (pC, strlen (pC), 1, pMailPipe) == strlen (pC) )
> [omitted code for brevity]
> 
> The "except" comes in the "strcpy() line.  What I am trying to do is put 
> a line at the top of the message body that begins with "~|uuencode" and 
> everything is OK until I put the tilde in, at which point the entire 
> tilde command line disappears, leaving only the message body.  The 
> "~|uuencode ..." line does work when tested at the shell level with: 
> "cat $messagefile | mailx -s $subjectline $address"  I haven't been able 
> to figure out how to quote it to get it through.
> Thank you in advance for any help you can offer.
> 

Couple things, your exact code above will buffer overflow cause your 
strcpy() is 12 chars and your last strcat() includes a null terminator 
which means len+len+13 would be correct in your malloc.

JMO, I'd change to sprintf(pC,"~| uuencode %s\n%s",pAttach,pMessage);

strcat() is an expensive operation in terms of time, especially over 
multiple calls.

On the mailx call, shells do not like "~" unless your meaning it. 
First, does the "~" make it to the messagefile?  I would hazard to guess 
that your problem is in shell command line expansion in the cat 
operation.  Run a test of the cat to the console (from the C prog) and 
see what mailx is really receiving.  You also might consider "mailx -s 
$subjectline $address < $messagefile" instead of cat.  Either way I 
don't think your problem is in the C code (except buffer overflow) from 
what I see here.

-- 
Greg Edwards
New Age Software, Inc.
http://www.nas-inet.com





More information about the Discuss mailing list