#include <mysql/mysql.h>
#include <stdio.h>
#include "conn.h"
void putstr(char *s, FILE *fp, char display)
{
fputs(s, fp);
fputc('\n', fp);
if (display == 1)
puts(s);
}
int main(int argc, char *argv[])
{
char ch;
char IP[16];
char line[100];
char sql[150];
int i = 0;
FILE *fp;
MYSQL conn;
MYSQL_RES *rs;
MYSQL_ROW row;
/* Get IP Address */
if ((fp = fopen("IP.txt", "r")) == NULL)
{
printf("Cannot open the file for IP Address.\n");
return 1;
}
while (ch = fgetc(fp), !feof(fp))
{
if ((ch >= '0' && ch <= '9') || ch == '.')
IP[i++] = ch;
else
break;
if (i == sizeof(IP) - 1)
break;
}
IP[i] = '\0';
fclose(fp);
/* Open Log File */
if ((fp = fopen("powerlog.txt", "w")) == NULL)
{
printf("Cannot open the file.\n");
return 2;
}
printf("Powerlog Version 2.3\n");
putstr("ID\tTIME", fp, 1);
/* Init MySQL Connection */
mysql_init(&conn);
if (!mysql_real_connect(&conn, SERVER_NAME, DB_USER, DB_PASSWORD, DB_NAME, 0, NULL, 0))
{
printf("Cannot connect to the database server.\n");
return 3;
}
mysql_set_character_set(&conn, "utf8");
/* Query */
sprintf(sql, "SELECT LogID, LogTime FROM PowerLog WHERE LogFlag = 'POWER ON' AND ComputerIP = '%s' ORDER BY LogTime DESC", IP);
mysql_query(&conn, sql);
rs = mysql_store_result(&conn);
for (i = 0; row = mysql_fetch_row(rs); i++)
{
sprintf(line, "%d\t%s", atoi(row[0]), row[1]);
putstr(line, fp, (i < 20));
}
mysql_free_result(rs);
fclose(fp);
mysql_close(&conn);
return 0;
}