This code splits 'source' into tokens of nick, ident and host.
It works and compiles without errors (using gcc -W -Wall).
However, if i change the *source to "[email protected] .192.168.0.1.com" it segfaults (buffer overflow?), but the compiler still compiles it without any errors..
Please take in consideration when you're replying that I'm very new to C and still learning the basics.
Any takers?
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *source = "[email protected]";
char *host;
char *nick;
char *ident;
int i = 0;
host = source;
/* nick */
while (host[i] != '!') {
nick[i] = host[i];
i++;
}
int nicklen = (size_t) strlen(nick);
ident[nicklen] = '\0'; // null terminate
host += nicklen +1; // removes the nick + ! from host
/* ident */
i = 0;
while (host[i] != '@') {
ident[i] = host[i];
i++;
}
int identlen = (size_t) strlen(ident);
ident[identlen] = '\0'; // null terminate
host += (size_t) identlen +1; // removes the ident + @ from host
/* ..and the remains of *host is the hostname */
printf(" nick: %s\n", nick);
printf("ident: %s\n", ident);
printf(" host: %s\n", host);
return 0;
}