Handling strings in C

Isocene

Senior member
Jan 4, 2001
694
0
71
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);
 

Baileybbk

Member
Mar 7, 2003
114
0
0
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
 

juiio

Golden Member
Feb 28, 2000
1,433
4
81
You will need to allocate more space for kara and to. You're writting past the bounds on both of them.
 

Isocene

Senior member
Jan 4, 2001
694
0
71
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);
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
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';
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |