#include <string.h> #include <stdio.h> #include <stdlib.h> #include "time.h" #include "sqlite3.h" sqlite3 *db; typedef struct { char content[1024]; char year[5]; } event; int rnd=0; event events[10]; int reccount = 0; int file_exists(const char * fn) { FILE *file; file = fopen(fn, "r"); if (file) { fclose(file); return 1; } return 0; } int cb(void *pArg,int argc, char **argv, char **p_col_names) { sprintf(events[reccount].content,"%s",argv[4]); sprintf(events[reccount].year,"%s",argv[1]); reccount++; return(0); } void usage(char *pth) { fprintf(stderr,"This Day in Tech\nBased on Tom Merritt's Chronology of Tech History\n"); fprintf(stderr,"Usage: %s [-d databasefile] [-r] [--help] \n\n",pth); fprintf(stderr,"-d databasefile : specify the database file to use. \n "); fprintf(stderr," Will try /etc/tdit.db or ./tdit.db if not specified or found.\n"); fprintf(stderr,"-r : print a single random entry for the day\n"); fprintf(stderr," (otherwise print all entries)\n"); fprintf(stderr,"--help : print this help\n"); } int main ( int argc, char *argv[] ) { char *path; int n; path=malloc(255); memset(path,0,sizeof(path)); char *msg; if (argc>4) { usage(argv[0]); return(1); } for (n = 1; n < argc; n++) { if (strcmp(argv[n],"-r")==0) rnd=1; if (strcmp(argv[n],"--help")==0) { usage(argv[0]); return(0); } if (strcmp(argv[n],"-d")==0) { if ((argc-1)>n) strcpy(path,argv[n+1]); else { usage(argv[0]); return 0; } } } if (strcmp(path,"")!=0) if (file_exists(path)==0) { printf("Could not find tdit database: %s\n",path); return(1); } if (strcmp(path,"")==0) if (file_exists("/etc/tdit.db")==1) { path="/etc/tdit.db"; } if (strcmp(path,"")==0) if (file_exists("tdit.db")==1) { printf("called it"); path="tdit.db"; } if ((strcmp(path,"")==0)) { fprintf(stderr,"Error: Could not find database: %s\n",path); return(1); } sqlite3_open(path,&db); if (db==NULL) { fprintf(stderr,"Error: Could open database\n"); return(1); } char query[256]; time_t t = time(NULL); struct tm tm = *localtime(&t); tm.tm_mon++; reccount=0; sprintf(query,"SELECT * FROM tdit WHERE day=%d AND month=%d",tm.tm_mday,tm.tm_mon); sqlite3_exec(db,query, cb, 0, &msg); int i; if (rnd==0) { for (i=0;i<reccount;i++) { printf("%s - %s\n",events[i].year,events[i].content); } } else { srand ( time(NULL) ); i=rand() % reccount; printf("%s - %s\n",events[i].year,events[i].content); } sqlite3_close(db); return(0); }