TWAIN-L Archives

Mark Twain Forum

TWAIN-L@YORKU.CA

Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Tony Verhulst <[log in to unmask]>
Reply To:
Mark Twain Forum <[log in to unmask]>
Date:
Fri, 19 Nov 2010 17:05:00 -0500
Content-Type:
multipart/mixed
Parts/Attachments:
text/plain (1251 bytes) , twain_quote.c (2057 bytes)
Being a professional software engineer (my academic background [MS 
Computer Science] is likely a bit different than yours :-) ), I just had 
to cook up something Twain wise. I wrote a small program that randomly 
selects a quote from a file. I then set up my computer to run this 
program automatically every 10 minutes. Lastly my mailer then uses this 
quote as my signature - as seen below. Anyway, attached is the program 
source (I wouldn't send the binary because you may (should) have virus 
concerns). Perhaps you or a computer savvy friend could set this up on 
your computer.

The quotes file is a simple text file with each quote separated by a 
blank line like:

    Yesterday Mr. Hall wrote that the printer's proof-reader was improving
    my punctuation for me, & I telegraphed orders to have him shot without
    giving him time to pray.

    Travel is fatal to prejudice, bigotry, and narrow-mindedness,
    and many of our people need it sorely on these accounts.

I can send the quotes file that I use if there is interest. I can't 
include it here due to the 400 line message limit.

regards/Tony

-- 

A robber is much more high-toned than what a pirate is- as a
general thing. In most countries they're awful high up in
the nobility- dukes and such.

Mark Twain





#include <sys/stat.h> #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> char src_file[] = "/home/averhuls/twain/quotes"; main(argc, argv) int argc; char **argv; {   int i, j, len, num_entries, index, blank_lines;   struct stat stat_buf;   char *buffer;   char line[128];   int entry_index_array[200];   time_t tloc;   FILE *fp;   /*    * Open the file containing the quotes.    */   if ((fp = fopen(src_file, "r")) == (FILE *) 0)   {     char buf[40];     sprintf(buf, "can't open %s\n", src_file);     perror(buf);     exit(1);   }   /*    * Get the size of the quotes file and allocate space for it.    */   if (stat(src_file, &stat_buf) == -1)   {     perror("fstat");     exit(1);   }   buffer = (char *) malloc(stat_buf.st_size);   /*    * Find number of entries in src_file and build the    * entry_index_array.    */   i = 0;   num_entries = 1;   while (fgets(line, 128,fp))   {     len = strlen(line);     strncpy(&buffer[i], line, len);     i += len;     buffer[i-1] = 0; // terminate the line     if (line[0] == '\n')     {       num_entries++;       entry_index_array[num_entries] = i;     }   }   fclose(fp);   /*    * If an output file name is specified, open it.    */   if (argc > 1)   {     if ((fp = fopen(argv[1], "w+")) == (FILE *)0)     {       printf("can't open %s\n", argv[1]);       perror("");       exit(1);     }     if (strcmp(argv[1], "/etc/motd") == 0)     {       blank_lines = 9;     }     else     {       blank_lines = 1;     }   }   else   {     fp = stdout;     blank_lines = 1;   }   /*    * Find entry to generate.    */   time(&tloc);   srand(tloc);   index = rand() % num_entries;   /*    * Index into buffer for the chosen (pseudo random) quote    */   i = entry_index_array[index];   /*    * Write output.    */   for (j = 0; j < blank_lines; j++)   {     fputs("\n", fp);   }   /* write until we get to a blank line */   while (buffer[i] != '\0')   {     fputs(&buffer[i], fp);     fputs("\n", fp);     i += strlen(&buffer[i]) + 1; // skip past line terminator   }   fputs("\nMark Twain\n", fp);   for (j = 0; j < blank_lines; j++)   {     fputs("\n", fp);   }   free(buffer);   fclose(fp); }

ATOM RSS1 RSS2