Tilføjelse af mysql forbindelse ødelægger forbindelsen
Hey, jeg sidder og forsøger at lave et program som smider nogle modtagne mails ind i en mysql database, men når jeg tilføjer mysqldelen, så forsvinder det tjek for om det rammer boundary, men udkommenterer jeg mysql koden, så virker det fint.Hvad er der galt?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include </usr/local/include/mysql/mysql.h>
#define MYSQL_HOST "localhost"
#define MYSQL_USERNAME "*****"
#define MYSQL_PASSWORD "*****"
#define MYSQL_DATABASE "*****"
void send_sms( char mailfrom[], char mailto[], char mailtext[] )
{
MYSQL conn;
mysql_init(&conn);
if (!mysql_real_connect(&conn,MYSQL_HOST,MYSQL_USERNAME,MYSQL_PASSWORD,MYSQL_DATABASE,0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&conn));
}
mysql_close(&conn);
printf( "FROM: %s\nTO: %s\nTEXT: %s\n", mailfrom, mailto, mailtext);
}
void read_mail( char file_path[], char to[] )
{
FILE *mail;
char line[200], *boundary, *from, *text;
int readtext = 0;
text = (char *) malloc(1500);
strcpy( text, "" );
mail = fopen( file_path, "r" );
if ( mail == NULL )
{
printf( "Hmm, mails fundet, men kunne ikke åbnes?\n" );
}
else
{
while ( fgets( line, 200, mail ) != NULL )
{
if (strncmp(line,"From:",5) == 0)
{
char name[50 + 1], email[50 + 1];
int rc = sscanf(line, "%50[^<]<%50[^>]", name, email);
int len = strlen( email );
from = (char *) malloc( len );
strcpy( from, email );
}
else if (strncmp(line,"Content-Type: text/plain",24) == 0 && readtext == 0)
{
readtext = 1;
}
else if (strstr(line,"boundary=") != NULL)
{
int ln = strlen(line);
boundary = (char *) malloc( ln-12 );
strncpy( boundary , line+11, ln-13 );
}
else if (strcmp(line,"\n") == 0 && readtext == 1 )
{
readtext = 2;
}
else if (strstr(line, boundary) != NULL && readtext == 2)
{
readtext = 0;
}
else if (readtext == 2)
{
strcat( text , line );
}
}
send_sms( from, to, text );
free( text );
free( from );
free( boundary );
}
fclose( mail );
}
void get_mails( char pathname[], char user[] )
{
char *maildir;
maildir = (char *) malloc( MAXPATHLEN );
if ( maildir == 0 )
{
printf( "Det her er ikke så godt: malloc døde" );
}
strcpy( maildir, pathname );
strcat( maildir, "/Maildir/new" );
DIR *directory;
struct dirent *entry;
char *entryname;
int retval;
struct stat statbuffer;
directory = opendir( maildir );
if (directory == NULL)
{
printf( " ERROR: Couldn't open dir: %s\n" , maildir );
return;
}
while ( ( entry = readdir( directory ) ) != NULL )
{
if ( strcmp( entry->d_name, ".") == 0 || strcmp( entry->d_name, "..") == 0)
{
continue;
}
entryname = (char *) malloc( MAXPATHLEN );
if ( entryname == 0 )
{
printf( "Det her er ikke så godt: malloc døde" );
}
strcpy( entryname, maildir );
strcat( entryname, "/" );
strcat( entryname, entry -> d_name );
retval = stat( entryname, &statbuffer );
if ( retval == 0 && ( statbuffer.st_mode & S_IFDIR ) )
{
/* Kan der opstå mapper så må vi implementere det her */
}
else
{
read_mail( entryname, user );
}
free( entryname );
}
closedir( directory );
}
void get_users( char pathname[] )
{
DIR *directory;
struct dirent *entry;
char *entryname;
int retval;
struct stat statbuffer;
directory = opendir( pathname );
if ( directory == NULL )
{
printf( " ERROR: Couldn't open dir: %s\n" , pathname );
return;
}
while ( ( entry = readdir( directory ) ) != NULL )
{
if ( strcmp( entry->d_name, ".") == 0 || strcmp( entry->d_name, "..") == 0)
{
continue;
}
entryname = (char *) malloc( MAXPATHLEN );
if ( entryname == 0 )
{
printf( "Det her er ikke så godt: malloc døde" );
}
strcpy( entryname, pathname );
strcat( entryname, "/" );
strcat( entryname, entry -> d_name );
retval = stat( entryname, &statbuffer );
if ( retval == 0 && ( statbuffer.st_mode & S_IFDIR ) )
{
get_mails( entryname , entry -> d_name );
}
free( entryname );
}
closedir( directory );
}
int main( void )
{
char path[200] = "/home";
int i = 3;
printf( "Starting SMS-qmailer\n" );
while ( i > 0 )
{
get_users( path );
sleep( 60 );
i--;
}
printf( "Closing SMS-qmailer\n" );
return 0;
}