home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 391.lha / Fortune_v2.04c / fortune.c < prev    next >
C/C++ Source or Header  |  1990-07-03  |  8KB  |  252 lines

  1. /*-------------------------------------------------------------------------*/
  2. /*      Program:  fortune                                                  */
  3. /*   Programmer:  George Kerber                                            */
  4. /*      Written:  06/19/89                                                 */
  5. /*      Purpose:  Similar to the UNIX Fortune, but centers the lines.      */
  6. /*     Compiler:  Lattice 5.04                                             */
  7. /*-------------------------------------------------------------------------*/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <dos.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #define VERSION "v2.04c"
  15. #define WRITTEN "06/19/89 - 01/07/90"
  16.  
  17. void main();
  18. void helpscreen();
  19. void mistake();
  20. void cleanup();
  21. void makeline();
  22. void print_rule();
  23.  
  24. void main(int argc, char *argv[])
  25. {
  26. char officialrule[256]  = "Multiple color options", option, opts[] = "";
  27. char *tempy, node[35], pathtorules[80] = "s:fortunes";
  28. int next, error, character, counter = 0, linesflag = 1, sflag = 0;
  29. int escape_char = '*', cflag = 2, cflag1 = 0;
  30. unsigned char clock[8];
  31. long linecount, rndnumber;
  32. FILE *fp, *voice;
  33.  
  34. /*----- Sets escape value -------- default is * ---------------------------*/
  35. tempy = getenv("ESCAPE");
  36. if(tempy != NULL) escape_char = *tempy;
  37. /*-------------------------------------------------------------------------*/
  38.       
  39. getclk(clock);
  40. stcgfn(node,argv[0]);
  41. if(strcmp(argv[1],"?") == 0 && argc > 1) helpscreen(escape_char,node,cflag);
  42.  
  43. /*----  Error Checking and Option Check  ----------------------------------*/
  44. for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; ) {
  45.    switch(toupper(option)) {
  46.       case '1':  if(cflag1) mistake(officialrule,node); cflag = 1; 
  47.                  cflag1 = 1;
  48.                  break;
  49.       case '2':  if(cflag1) mistake(officialrule,node); cflag = 2; 
  50.                  cflag1 = 1;
  51.                  break;
  52.       case '3':  if(cflag1) mistake(officialrule,node); cflag = 3; 
  53.                  cflag = 1;
  54.                  break;
  55.       case 'S':  sflag = 1;
  56.                  voice = fopen("SPEAK:OPT/d0", "w");
  57.                  if(voice == (FILE *)NULL)
  58.                     mistake("Can't open SPEAK:    Is it MOUNTED?",node);
  59.                  break;
  60.       case 'L':  linesflag = 0; break;
  61.        default:  mistake("Invalid Option",node);
  62.       }
  63.    } 
  64. if(sflag) linesflag = 0;
  65. if(argc > next) {
  66.    strcpy(pathtorules,(argv[next]));
  67.    }
  68.    else {
  69.    if(!access("ram:fortunes",4)) strcpy(pathtorules,"ram:fortunes");
  70.    }
  71.  
  72. if(argc > (next + 1)) mistake("Invalid Option Count",node);
  73.  
  74. if(access(pathtorules,4)) mistake("Can't Locate File",node);
  75.  
  76. /*----  Open "fortunes" file    -------------------------------------------*/
  77. fp = fopen(pathtorules,"r");
  78. if(fp == (FILE *)NULL) mistake("Can't Open File",node);
  79.  
  80. srand(clock[7] + clock[6] + clock[3] + clock[4]);
  81. fseek(fp,-1L,2);
  82. linecount = ftell(fp);
  83. counter = 0;
  84. while(1) {
  85.    rndnumber = ((rand() % linecount) + 1) ;
  86.    error = fseek(fp,rndnumber,0);
  87.    if(error != 0) {
  88.       if(counter++ > 25) mistake("#1",node);  
  89.       }
  90.  
  91.    counter = 0;
  92.    while((character = fgetc(fp)) != EOF) {
  93.       if(character == '\n') break; 
  94.       if(counter++ > 100) mistake("#2",node); 
  95.       continue; 
  96.       }
  97.  
  98.    if(fgets(officialrule,256,fp) == (char *)NULL) {
  99.       continue; 
  100.       }
  101.       else {
  102.       if(officialrule[0] == ' ' || officialrule[0] == '#') continue;
  103.       if(linesflag == 1) makeline(cflag);
  104.       if(!sflag) printf("\n");
  105.       print_rule(81,officialrule,escape_char,sflag,voice);
  106.       }
  107.  
  108.    counter = 0;
  109.    while(1) {
  110.       if(fgets(officialrule,256,fp) == (char *)NULL) {
  111.          cleanup(linesflag,sflag,cflag);
  112.          }
  113.          else {
  114.          if(officialrule[0] != ' ' || officialrule[0] == '\n') {  
  115.             cleanup(linesflag,sflag,cflag); }
  116.          print_rule(80,officialrule,escape_char,sflag,voice);
  117.          if(counter++ >= 10) cleanup(linesflag,sflag,cflag);
  118.          }
  119.       continue; 
  120.       }
  121.    if(linesflag == 1) makeline(cflag);
  122.    }
  123. }
  124.  
  125. /*-------------------------------------------------------------------------*/
  126. /*      End of main program, functions are below                           */
  127. /*-------------------------------------------------------------------------*/
  128.  
  129. /*  CLEANUP FUNCTION  */
  130.  
  131. void cleanup(int linesflag,int sflag,int cflag)
  132. {
  133. if(linesflag == 1) makeline(cflag);
  134. if(!sflag) printf("\n");
  135. fcloseall();
  136. exit(0);
  137. }
  138.  
  139. /*-------------------------------------------------------------------------*/
  140.  
  141. /*  HELPSCREEN FUNCTION  */
  142.  
  143. void helpscreen(char escape_char, char node[],int cflag)
  144. {
  145. printf("\x0c");
  146. makeline(cflag);
  147. printf("\n  %s    George Kerber %10s %25s\n\n",node,VERSION,WRITTEN);
  148. printf("  %s randomly selects a fortune from the s:fortunes file.\n\n",node);
  149. printf("  Escape Character is currently set to \"%c\"\n\n",escape_char);
  150. printf("  SYNTAX:  %s [-l | -s | [-1 | -2 | -3]] [alternative path/name]\n\n",node);
  151. printf("           -s  fortune is spoken using the Amiga SPEAK: device.\n");
  152. printf("           -l  fortune is displayed with no outline.\n");
  153. printf("           -1  fortune is displayed with color 1  (white) outline.\n");
  154. printf("           -2  fortune is displayed with color 2  (black) outline (default)\n");
  155. printf("           -3  fortune is displayed with color 3 (orange) outline.\n\n");
  156. printf("  The fortunes file should be located in the s: directory or provide\n");
  157. printf("  an alternative path/name as an option.\n");
  158. printf("  See program documentation for fortunes file syntax.\n");
  159. makeline(cflag);
  160. printf("\n");
  161. exit(0);
  162. }
  163.  
  164. /*-------------------------------------------------------------------------*/
  165.  
  166. /*  MAKELINE FUNCTION  */
  167.  
  168. void makeline(int cflag)
  169. {
  170.  
  171. printf("");
  172. if(cflag == 1) printf("");
  173. if(cflag == 2) printf("");
  174. if(cflag == 3) printf("");
  175.  
  176. printf("  ___________________________________________________________________________\n");
  177. }
  178.  
  179. /*-------------------------------------------------------------------------*/
  180.  
  181. /*  MISTAKE FUNCTION  */
  182.  
  183. void mistake(char description[],char node[35])
  184. {
  185. printf("\n\n\07     ERROR: %s -->> %s.\n\n",
  186.    node,description);
  187. exit(5);
  188. }
  189.  
  190. /*-------------------------------------------------------------------------*/
  191.  
  192. /*  PRINT_RULE FUNCTION  */
  193.  
  194. void print_rule(int number, char rule[],int escape_char,int sflag,FILE *voice)
  195. {
  196. int counter = -1, offset = 0, i;
  197. char fortune[256];
  198.  
  199. fortune[0] = '\0';
  200. while(rule[++counter] != (char *)NULL) {
  201.    if(rule[counter] == escape_char) {
  202.       if(rule[++counter] == escape_char) { 
  203.          i = strlen(fortune) - 1;
  204.          fortune[i + 1] = escape_char;
  205.          fortune[i + 2] = '\0';
  206.          offset++;
  207.          continue; 
  208.          }
  209.       if(!sflag) {
  210.       switch(rule[counter]) {
  211.          case '0':  strcat(fortune,""); break; /* color 0 char       */
  212.          case '1':  strcat(fortune,""); break; /* color 1 char       */
  213.          case '2':  strcat(fortune,""); break; /* color 2 char       */
  214.          case '3':  strcat(fortune,""); break; /* color 3 char       */
  215.          case '4':  strcat(fortune,"");  break; /* default attr       */
  216.          case '5':  strcat(fortune,"");  break; /* bold char          */
  217.          case '6':  strcat(fortune,"");  break; /* underline char     */
  218.          case '7':  strcat(fortune,"");  break; /* italics char       */
  219.          case ')':  strcat(fortune,""); break; /* color 0 bkgnd      */
  220.          case '!':  strcat(fortune,""); break; /* color 1 bkgnd      */
  221.          case '@':  strcat(fortune,""); break; /* color 2 bkgnd      */
  222.          case '#':  strcat(fortune,""); break; /* color 3 bkgnd */
  223.          }
  224.       }
  225.    }
  226.       else { 
  227.       i = strlen(fortune) - 1;
  228.       fortune[i + 1] = rule[counter];
  229.       fortune[i + 2] = '\0';
  230.       offset++;
  231.       }
  232.    continue;
  233.    }
  234.  
  235. offset = ((number - offset) / 2);
  236. for( ; offset > 0 ; strins(fortune," "),offset--);
  237. if(!sflag) {
  238.    printf("%s",fortune); 
  239.    }
  240.    else {
  241.    fprintf(voice,"%s",fortune); 
  242.    }
  243. }
  244.  
  245. /*-------------------------------------------------------------------------*\
  246.  
  247. 10/07/89 v2.03:  Added escape and color options
  248. 11/01/89 v2.03b: Recompiled with Lattice 5.04
  249. 01/07/90 v2.03c: Removed all global variables
  250.  
  251. \*-------------------------------------------------------------------------*/
  252.