On Tue, 4 Sep 2001, ahmed wrote:
> i hope that some one send me a programme illustrating how to access
> to mysql database with c api ..
I run a site that has a free e-mail forwarding service. The following
program is what runs .qmail-default.
#define HOST "localhost"
#define USER "username"
#define PASSWORD "password"
#define DATABASE "database"
#include "mysql.h"
#include <stdio.h>
#include <stdlib.h>
/* Returns 1 if s contains any UNSAFE characters.
* Returns 0 otherwise. */
#define UNSAFE "&;`'\"|*?~<>^()[]{}$\n\r"
int unsafe(char *s) {
char *p = s;
while (*p) {
if (strchr(UNSAFE, *p)) {
return 1;
}
p++;
}
return 0;
}
/* This program is called by ~/.qmail-default. It queries the MySQL
* database to forward the e-mail to someone. */
main() {
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
char *user, query[255], email[255], buf[8192];
FILE *pipe;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, HOST, USER, PASSWORD, DATABASE, 0,
NULL, 0)) {
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
exit(111);
}
user = getenv("EXT");
sprintf(query, "SELECT email FROM records WHERE login='%s'", user);
if (mysql_query(&mysql, query)) {
fprintf(stderr, "Unable to query database: %s\n",
mysql_error(&mysql));
exit(111);
}
result = mysql_store_result(&mysql);
if (!mysql_num_rows(result)) {
printf("There is no user '%s' registered here.\n", user);
exit(100);
}
row = mysql_fetch_row(result);
if (!row[0] || !*row[0]) {
printf("User '%s' has not setup a forwarding address.\n", user);
exit(100);
}
strcpy(email, row[0]);
if (unsafe(email)) {
printf("User '%s' has a forwarding address containing unsafe characters (%s).\n",
user, email);
exit(100);
}
/* Everything checks out. Forward the message. */
sprintf(query, "/var/qmail/bin/forward %s", email);
if (pipe = popen(query, "w"))
while (fgets(buf, sizeof(buf), stdin))
fputs(buf, pipe);
exit (0);
}
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php