• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Handling strings in C

Isocene

Senior member
I'm writing a program that will send mail through an smtp server. I can send the commands fine by themselves but when I try to send them with user input (like asking for the desintation addr instead of hard coding it) I have problems.

I want the output to look like
MAIL From:<mr54561@swt.edu>
RCPT TO:<mr54561@swt.edu>

Here is my output and below is the relevant code, if anyone could help it would be much appreciated. Thanks

Output:

[mr54561@taz3 networking]$ ./usermail mr54561@swt.edu
(From) :lkj@lkj.com
RCPT To:<mr54561@swt.edulkj@lkj.com>

Next is..
@swt.edulkj@lkj.com>



#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>

#define SIZE sizeof (struct sockaddr_in)

main ( int argc, char *argv[] )
{
int sockfd, i;

char *fromer;
char helo[] = "HELO \n";
char kara[] = "MAIL From:<\n";
char to[] = "RCPT To:<";
char return_val[1024];
char input[1024];
char c, rc;
struct sockaddr_in server;

if (argc!=2 || argc>2){
printf("Correct syntax is 'usermail <recipient>'\n");
exit(1);
}

strcat(to,argv[1]);
strcat(to,">\n");



printf("(From) :");
gets(fromer);

strcat(kara,fromer);
strcat(kara,">\n");

printf("%s\n",to);
printf("Next is..\n");
printf("%s\n",kara);
 
Just glanced at it....but remember 'strcat' doesn't allocate any space for your strings!
Lucky you didn't overwrite the end of your stack 🙂
 
You will need to allocate more space for kara and to. You're writting past the bounds on both of them.
 
Ok I gave it more space but it is still giving crazy output.

[mr54561@taz3 networking]$ ./usermail mr54561@swt.edu
(From) :mike@swt.edu
From line
@swt.edu>
mike@swt.edu
Send line
RCPT To:<mr54561@swt.edu>
mike@swt.edu

I tested the output under "From line" and it gives
@swt.edu>
mike@swt.edu
which makes no sense to me. Im expecting "Mail from:<mike@swt.edu>" but it cuts off the first 5 then prints the whole thing?

The send line comes out right at first but then prints out mike@swt.edu which wasnt even supposed to be there! So confusing!

Here is the new code.


#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>

#define SIZE sizeof (struct sockaddr_in)

main ( int argc, char *argv[] )
{
int sockfd, i;

char tmp[30];
char helo[80] = "HELO \n";
char kara[80] = "MAIL From:<";
char to[] = "RCPT To:<";
char return_val[1024];
char input[1024];
char c, rc;
struct sockaddr_in server;

if (argc!=2 || argc>2){
printf("Correct syntax is 'usermail <recipient>'\n");
exit(1);
}

strcat(to,argv[1]);
strcat(to,">\n");



printf("(From) :");
strcat(kara,gets(tmp));

//strcat(kara,fromer);
//strcat(kara,">\n");

printf("From line\n");
printf("%s\n",kara);
printf("Send line\n");
printf("%s\n",to);
 
using static buffers for input strings is asking for a buffer overflow exploit.

You could use the C++ string class to handle the concatenation instead of strcat.
Or make a concatenation function like this:

void concat(char **deststr, char *srcstr)
{
int requiredlen = strlen(*deststr) + strlen(srcstr) + 1;
char *newstr = realloc(*deststr,requiredlen);
if(newstr==NULL) throw "Out of memory";
*deststr = newstr;
strcat(deststr,srcstr);
deststr[requiredlen-1] = '\0';
}
 
Back
Top