home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fred Fish Collection 1.5
/
ffcollection-1-5-1992-11.iso
/
ff_disks
/
300-399
/
ff369.lzh
/
Fortune
/
fortune.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-15
|
8KB
|
301 lines
/*-------------------------------------------------------------------------*/
/* Programmer: George Kerber */
/* Written: 06/19/89 */
/* Purpose: Similar to the UNIX Fortune, but centers the lines. */
/* Compiler: Lattice 5.05 */
/*-------------------------------------------------------------------------*/
#define WRITTEN "06/19/89 - 04/20/90"
#define VERSION "v2.04h"
#define TRUE 1
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
#include <stdlib.h>
void main(), helpscreen(), mistake(), cleanup(), makeline(), print_rule();
void main(int argc, char *argv[])
{
char officialrule[200] = "Multiple color options", option, opts[] = "";
char *tempy, node[35], pathtorules[80] = "s:fortunes";
int next, character, linesflag = 1, sflag = 0;
int escape_char = '*', cflag = 2, cflag1 = 0;
unsigned char clock[8];
unsigned long linecount;
FILE *fp, *voice;
/*----- Sets escape value -------- default is * ---------------------------*/
if((tempy = getenv("ESCAPE")) != NULL) escape_char = *tempy;
/*-------------------------------------------------------------------------*/
getclk(clock);
stcgfn(node,argv[0]);
if(strcmp(argv[1],"?") == 0 && argc > 1)
helpscreen(escape_char,node,pathtorules);
/*---- Error Checking and Option Check ----------------------------------*/
if(argc)
{
for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; )
{
switch(toupper(option))
{
case '1': if(cflag1) mistake(officialrule,node); cflag=1; cflag1=1;
break;
case '2': if(cflag1) mistake(officialrule,node); cflag=2; cflag1=1;
break;
case '3': if(cflag1) mistake(officialrule,node); cflag=3; cflag1=1;
break;
case 'S': sflag = 1; break;
case 'L': linesflag = 0; break;
default: mistake("Invalid Option",node);
}
}
}
if(!argc)
{
*node = NULL;
sflag = 1;
}
if(sflag)
{
if((voice = fopen("SPEAK:OPT/d0", "w")) == (FILE *)NULL)
mistake("Can't open SPEAK: Is it MOUNTED?",node);
}
if(sflag) linesflag = 0;
if(argc > next)
{
strcpy(pathtorules,(argv[next]));
}
else
{
if(!access("ram:fortunes",0))
{
strcpy(pathtorules,"ram:fortunes");
}
else
{
if(!access("fortunes",0)) strcpy(pathtorules,"fortunes");
}
}
if(argc > (next + 1)) mistake("Invalid Option Count",node);
if(access(pathtorules,4)) mistake("Can't Locate File",node);
/*---- Open "fortunes" file -------------------------------------------*/
if((fp = fopen(pathtorules,"r")) == (FILE *)NULL)
mistake("Can't Open File",node);
srand(clock[7] + clock[6] + clock[3] + clock[4]);
fseek(fp,-1L,2);
linecount = ftell(fp);
rewind(fp);
while(TRUE)
{
if(fseek(fp, (rand() % linecount) + 1, 0))
{
mistake("Can't SEEK",node);
}
while((character = fgetc(fp)) != EOF)
{
if(character == '\n') break;
continue;
}
if(fgets(officialrule,200,fp) == (char *)NULL)
{
continue;
}
else
{
if(*officialrule == ' ' || *officialrule == '#') continue;
if(linesflag) makeline(cflag);
if(!sflag) printf("\n");
print_rule(81,officialrule,escape_char,sflag,voice);
}
next = 0;
while(TRUE)
{
if(fgets(officialrule,200,fp) == (char *)NULL)
{
cleanup(linesflag,sflag,cflag);
}
else
{
if(*officialrule != ' ' || *officialrule == '\n')
{
cleanup(linesflag,sflag,cflag);
}
print_rule(80,officialrule,escape_char,sflag,voice);
if(next++ >= 10) cleanup(linesflag,sflag,cflag);
}
continue;
}
if(linesflag) makeline(cflag);
}
}
/*-------------------------------------------------------------------------*/
/* End of main program, functions are below */
/*-------------------------------------------------------------------------*/
/* CLEANUP FUNCTION */
void cleanup(int linesflag,int sflag,int cflag)
{
if(linesflag) makeline(cflag);
if(!sflag) printf("\n");
fcloseall();
exit(0);
}
/*-------------------------------------------------------------------------*/
/* HELPSCREEN FUNCTION */
void helpscreen(char escape_char, char *node, char *def_file)
{
char *display = " displayed w/";
char *outline = " outline.";
char *alter = "alternate path/file";
char *blank = " -";
printf("\x0c");
makeline(2);
printf("\n
%s
George Kerber %10s %25s\n\n",
node,VERSION,WRITTEN);
printf(" Randomly display or speak a
fortune
.\n\n");
printf(" Escape Character = \"
%c
\"\n\n",escape_char);
printf(" SYNTAX:
%s
[-l | -s | [-1 | -2 | -3]] [%s]\n\n",
node,alter);
printf("%ss spoken --> SPEAK: device.\n",blank);
printf("%sl%so%s\n",blank,display,outline);
printf("%s1%scolor 1%s\n",blank,display,outline);
printf("%s2%s
color 2
%s (default)\n", blank,display,outline);
printf("%s3%s
color 3
%s\n\n", blank,display,outline);
printf(" Data file is
%s
or %s.\n",def_file,alter);
makeline(2);
printf("\n");
exit(0);
}
/*-------------------------------------------------------------------------*/
/* MAKELINE FUNCTION */
void makeline(int cflag)
{
switch(cflag)
{
case 1: printf("
"); break;
case 2: printf("
"); break;
case 3: printf("
"); break;
}
printf("
___________________________________________________________________________
\n");
}
/*-------------------------------------------------------------------------*/
/* MISTAKE FUNCTION */
void mistake(char *description, char *node)
{
if(*node != NULL)
{
printf("\n\n\07 ERROR: %s -->
> %s.
\n\n",
node,description);
}
exit(5);
}
/*-------------------------------------------------------------------------*/
/* PRINT_RULE FUNCTION */
void print_rule(int number, char *rule,int escape_char,int sflag,FILE *voice)
{
int offset = 0, i;
char fortune[200];
*fortune = NULL;
rule--;
while(*(++rule) != (char *)NULL)
{
if(*rule == escape_char)
{
if(*(++rule) == escape_char)
{
i = strlen(fortune);
*(fortune + i++) = escape_char;
*(fortune + i) = NULL;
offset++;
continue;
}
if(!sflag)
{
switch(*rule)
{
case '0': strcat(fortune,"
"); break; /* color 0 char */
case '1': strcat(fortune,"
"); break; /* color 1 char */
case '2': strcat(fortune,"
"); break; /* color 2 char */
case '3': strcat(fortune,"
"); break; /* color 3 char */
case '4': strcat(fortune,""); break; /* default attr */
case '5': strcat(fortune,"
"); break; /* bold char */
case '6': strcat(fortune,"
"); break; /* underline char */
case '7': strcat(fortune,"
"); break; /* italics char */
case ')': strcat(fortune,"
"); break; /* color 0 bkgnd */
case '!': strcat(fortune,"
"); break; /* color 1 bkgnd */
case '@': strcat(fortune,"
"); break; /* color 2 bkgnd */
case '#': strcat(fortune,"
"); break; /* color 3 bkgnd */
}
}
}
else
{
i = strlen(fortune);
*(fortune + i++) = *rule;
*(fortune + i) = NULL;
offset++;
}
continue;
}
if(sflag)
{
fprintf(voice,"%s",fortune);
}
else
{
offset = ((number - offset) / 2);
for( ; offset > 0 ; strins(fortune," "),offset--);
printf("%s",fortune);
}
}
/*-------------------------------------------------------------------------*\
10/07/89 v2.03: Added escape and color options
11/01/89 v2.03b: Re-compiled with Lattice 5.04.
01/07/90 v2.03c: Removed all global variables
03/03/90 v2.04e: Re-compiled with Lattice 5.04.
04/16/90 v2.04g: Optimized code and reduced helpscreen for smaller
program size.
\*-------------------------------------------------------------------------*/