LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2004, 03:28 AM   #1
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
cstring troubles


Hi,
I've never had this trouble before with this but in this situation ??
it's twilight zone time --
so this code gets SMTP response
rfp is static FILE *rfp; that's the read end of an AF_INET socket
it's a local connection to avmailgate on port 25 that relays to exim on port 825 or something
(can't imagine how that makes a difference)
Code:
int  mySMTP::get_response()
{
    char buf[BUFSIZ];
    char nCode[3]={0};
    while (fgets(buf, sizeof(buf), rfp)) {
        buf[strlen(buf)-1] = 0;
        printf("%s --> %s\n", host, buf);
        if (!isdigit(buf[0]) || buf[0] > '3') {
          return 1;
        }
        if (buf[4] != '-')
            break;
    }
    strncpy(nCode, buf, 3);
    return (atoi(nCode));
}
ok with this server buf is
220 lisa.sourcery.net AvMailGate-2.0.2-2
the entire line from the printf is
0x42ce2904localhost --> 220 lisa.sourcery.net AvMailGate-2.0.2-2
somehow above code is returning
2201
last digit is not an "L" (i copy and paste here to make sure)
i haven't tested this alot becaus it always works
i have no idea where the 1 is comming from ?????????
need c gurus help cause i'm not good with c char arrays
 
Old 09-29-2004, 04:08 AM   #2
dougpotter
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Rep: Reputation: 0
Your buffer, nCode is not null terminated. The function strncpy will not add a null terminating character if it copies the maximum number of characters. Also, your buffer doesn't have space for a null terminating character. Declare nCode as "char nCode[4];", then use the following:

Code:
    strncpy(nCode, buf, 3);
    nCode[3] = 0;
    return (atoi(nCode));
nCode is declared as a local variable (on the stack) so it's unpredictable what nCode[3] would be at run-time. It may have worked in the past because there was a null byte, or some other non-numeric character (atoi stops when it finds the first non-numeric character).
 
Old 09-29-2004, 09:37 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, adding a zero won't work, you need the string null terminator:
nCode[3] = '\0';
 
Old 09-30-2004, 09:43 PM   #4
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Original Poster
Rep: Reputation: 53
yep that fixed it !
thanks for both of your help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
troubles with SATA, troubles with NVIDIA aevangelica Linux - Hardware 6 10-17-2005 02:39 AM
CString in Linux nelnel Programming 2 07-06-2005 07:48 PM
g++ troubles GlassEyeSlim Linux - Newbie 6 02-15-2005 10:51 AM
X Troubles robza Mandriva 2 08-25-2003 04:28 AM
Troubles tryinglinux Linux - Newbie 6 05-06-2003 10:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration