#include <mysql/mysql.h>
#include "auto_break.h"
#include "stralloc.h"

extern MYSQL_RES *result;
extern int do_query(stralloc *query, MYSQL_ROW *row);

int checkuser_mysql(char *username, int i) {
  MYSQL_ROW row;
  int j, rows;
  stralloc qmail_mysql_query = { 0 };
  stralloc user = { 0 };

  if (! stralloc_ready(&user, i)) return -1;
  if (! stralloc_catb(&user, username, i)) {
    stralloc_free(&user);
    return -1;
  }
  user.s[i] = '\0';
  if (! make_query(&qmail_mysql_query, user.s)) {
    stralloc_free(&user);
    return -1;
  }

  stralloc_free(&user);
  rows = do_query(&qmail_mysql_query, &row);

  switch (rows) {
    case -1: return -1; break;
    case 0:
      for (j = i - 1; j > 0; j--) {
        if (*(username + j) == *auto_break) return checkuser_mysql(username, j);
      }
      return 0;
      break;
  }
  mysql_free_result(result);
  return 1;
}


syntax highlighted by Code2HTML, v. 0.9.1