home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
MISC
/
stf.lzh
/
stf.c
< prev
next >
Wrap
Text File
|
1994-04-24
|
11KB
|
422 lines
/************************************************************************
* stfortune.c *
*************************************************************************
* A program designed to print Star Trek quotes from a disk file. This *
* program has successfully been compiled on a variety of platforms and *
* operating systems. *
*************************************************************************
* Original Code: Scott McGee 8/28/92 *
*************************************************************************
* Copyright 1992, 1993 by Scott McGee *
* *
* This program is my own work and is protected by copyright laws. It is *
* provided free of charge on the condition that it not be altered or *
* sold for profit without my prior express written permission. If you *
* make changes you feel improve the program, please send them to me. If *
* I agree, I will add your changes to my code (with comments giving you *
* credit, of course). I can be reached by email at smcgee@microware.com *
************************************************************************/
/************************************************************************
revision history
date who what
12/15/92 sam added -f and -I options
01/07/93 sam added ranges
01/11/93 sam added coco modifications from Boisy Pitre
01/13/93 sam fixed bug in ranges
01/27/93 sam added environment variables
01/29/93 sam fixed bug in ranges when end_range > number of quotes
02/19/93 sam fixed bug with program name in help output
02/19/93 sam made do_index close files (fixed "read error in index file")
02/21/93 sam coco fixes from Boisy Pitre (8 significant chars on #defines!)
03/09/93 sam changes to compile on OS-9000 without Ultra C
03/14/93 sam changes in header format for portability (define capabilities)
************************************************************************/
/* NOTE TO USERS: if you are compiling this on anything other than an OS-9
system, or a sun3 or sun4, you may have to look through the following
definitions to see if you need to define any from the command line for
your compiler. For instance, if you are compiling on a system that has
no string.h header, you should define NO_STRING_H on the compiler command
line. Also, be sure to modify the default paths found below the defines!
*/
/* define some capabilities dealing with portability issues:
NO_STRING_H system DOES NOT HAVE a string.h header (use strings.h)
*/
#ifndef _UCC /* no Ultra C compiler */
# ifdef OSK /* old cc for osk */
# define NO_STRING_H
# endif
# ifdef _OS9000 /* old cc for os-9000 */
# define NO_STRING_H
# endif
#endif
/*
OSK_HELP system CAN use -? for help (otherwise, use -h)
*/
#ifdef OSK /* OS-9 680x0 system */
# define OSK_HELP
#endif
#ifdef _OS9000 /* OS-9000 system */
# define OSK_HELP
#endif
#ifdef OS9 /* OS-9 6809 system */
# define OSK_HELP
#endif
/*
NO_ENVIRONMENT system does NOT support environment variables
*/
#ifdef OS9
# define NO_ENVIRONMENT
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#ifndef NO_STRING_H
# include <string.h> /* good, we can use string.h */
#else
# include <strings.h> /* use strings.h and redefine rindex */
# define strrchr(a,b) rindex(a,b)
#endif
/* platform specific stuff */
/* OS-9 6809 has no void and must define unsigned int as just int */
#ifdef OS9
# define u_int int
# define void int
#else
# define u_int unsigned int
#endif
#define TRUE 1
#define FALSE 0
/*
These are the default paths for the stquote file and the index file. These
locations is valid on MY machine only. If you compile this on your machine,
make sure to change this!
*/
#define QUOTEFILE_DEFAULT "/home/bear/smcgee/usr/lib/stquotes"
#define INDEXFILE_DEFAULT "/home/bear/smcgee/usr/lib/stqindex"
/*
These global variables hold the names of the index and quote file. These
are initialized to the constants defined above.
*/
char *filename;
char *indexname;
void do_help(prog_name)
char *prog_name;
{
char *tmp;
tmp = strrchr(prog_name, '/');
if(tmp != NULL)
prog_name = tmp+1;
fprintf(stderr, "\n%s: a program to print out quotes from Star Trek\n\n", prog_name);
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s (opts) : print a quote at random\n", prog_name);
fprintf(stderr, " %s (opts) n : print quote number n\n", prog_name);
fprintf(stderr, " The value of n is used mod the number of quotes.\n");
fprintf(stderr, " Multiple quote numbers may be specified on one line.\n");
fprintf(stderr, " Ranges may be specified by placing a dash ('-') \n");
fprintf(stderr, " between the two number. (ie 1-4)\n\n");
fprintf(stderr, "Options -i : remake index file\n");
fprintf(stderr, " -u : don't use index file\n");
fprintf(stderr, " -n : print quote number before quote\n");
fprintf(stderr, " -f=file : use 'file' for quotefile\n");
fprintf(stderr, " -I=file : use 'file' for indexfile\n");
#ifdef OSK_HELP
fprintf(stderr, " -? : print this help message\n");
#else
fprintf(stderr, " -h : print this help message\n");
#endif
#ifndef OS9
fprintf(stderr, "\nThe environment variables QUOTEFILE and INDEXFILE can\n");
fprintf(stderr, "be used to specify the quotefile and indexfile used.\n");
#endif
fprintf(stderr, "\n\t(Copyright 1992 by Scott McGee)\n\n");
exit(0);
}
void seek(count, quotefp)
int count;
FILE *quotefp;
{
long fpos;
char buf[255];
FILE *indexfp;
int status;
strcpy(buf, indexname);
if((indexfp = fopen(buf, "r")) == NULL){
fprintf(stderr, "Cannot open index file \"%s\"!\n", buf);
exit(0);
}
status = fseek(indexfp, (long)(count*sizeof(long)), 0);
if(status != 0){
fprintf(stderr, "Seek error in index file!\n");
exit(0);
}
status = fread(&fpos, sizeof(long), 1, indexfp);
if(status == 0){
fprintf(stderr, "Error reading file position from index file!\n");
exit(0);
}
status = fseek(quotefp, fpos, 0);
if(status != 0){
fprintf(stderr, "Seek error in quote file!\n");
exit(0);
}
}
int do_index(){
long fpos;
int entry;
int count;
char buf[255];
char *not_done;
FILE *infp;
FILE *outfp;
strcpy(buf, indexname);
if((outfp=fopen(buf, "w")) == NULL){
fprintf(stderr, "Cannot open output file \"%s\"!\n", buf);
exit(0);
}
strcpy(buf, filename);
if((infp=fopen(buf, "r")) == NULL){
fclose(outfp);
fprintf(stderr, "Cannot open input file \"%s\"!\n", buf);
exit(0);
}
printf("Please wait while index file is created!\n");
count = -1;
not_done = (char *) 1;
fgets(buf, 255, infp); /* skip count */
while(not_done){
fpos = ftell(infp);
not_done = fgets(buf, 255, infp);
if(not_done){
buf[5] = '\0';
entry = atoi(buf);
if(entry != count){
fwrite(&fpos, sizeof(long), 1, outfp);
count = entry;
}
}
}
fclose(outfp);
fclose(infp);
return(count);
}
int read_quote(filename, unindex, got_quote_number, print_number, quote_number)
char *filename;
int unindex;
int got_quote_number;
int print_number;
int quote_number;
{
int quotes;
int number;
u_int rand;
char *not_done;
char line[255];
FILE *stqfile;
if((stqfile = fopen(filename, "r")) != NULL){
not_done = fgets(line, 255, stqfile);
if(not_done){
quotes = atoi(line);
if(!quotes){
fprintf(stderr, "Number of quotes not accessible!\n");
exit(0);
}
if(!got_quote_number){
rand = (unsigned) time(0) & 0xffff;
rand = (rand * 25173 + 13849) % 65536;
quote_number = (abs(rand%quotes)+1);
}else
if((quote_number < 0) || (quote_number > quotes))
quote_number = quote_number%quotes;
if(unindex){
do{
not_done = fgets(line, 255, stqfile);
line[5] = '\0';
number = atoi(line);
}while(not_done && (number < quote_number));
}else{
seek(quote_number, stqfile);
not_done = fgets(line, 255, stqfile);
line[5] = '\0';
number = atoi(line);
}
if(print_number)
printf("Quote number %d\n", quote_number);
while(not_done && (number == quote_number)){
printf("%s", &line[6]);
not_done = fgets(line, 255, stqfile);
line[5] = '\0';
number = atoi(line);
}
printf("\n");
}else{
fprintf(stderr, "Invalid file contents in file \"%s\"!\n", filename);
exit(0);
}
}else{
fprintf(stderr, "Whoops! Can't open \"%s\" !!\n", filename);
exit(0);
}
return(quotes);
}
int main(argc, argv)
int argc;
char *argv[];
{
int quote_number;
int i;
int u_flag;
int number_flag;
int n_flag;
int range;
int range_end;
int env_index;
int quotes;
char* ptr;
/* initialize global filename variables */
env_index = FALSE;
#ifdef NO_ENVIRONMENT
filename = QUOTEFILE_DEFAULT;
indexname = INDEXFILE_DEFAULT;
#else
filename = getenv("QUOTEFILE");
if(filename == NULL)
filename = QUOTEFILE_DEFAULT;
indexname = getenv("INDEXFILE");
if(indexname == NULL)
indexname = INDEXFILE_DEFAULT;
else
env_index = TRUE;
#endif
/* initialize arguement flag variables */
u_flag = FALSE;
number_flag = FALSE;
n_flag = FALSE;
/* parse arguements */
for(i=1;i<argc;i++){
if((argv[i][0] == '-') || (argv[i][0] == '/')){
switch (argv[i][1]){
case '?':
case 'h':
do_help(argv[0]);
case 'i':
quote_number = do_index();
number_flag = TRUE;
n_flag = TRUE;
break;
case 'u':
u_flag = TRUE;
break;
case 'n':
n_flag = TRUE;
break;
case 'f':
filename = &argv[i][3];
if((indexname == INDEXFILE_DEFAULT) || (env_index == TRUE))
u_flag = TRUE;
break;
case 'I':
indexname = &argv[i][3];
u_flag = FALSE;
break;
default :
fprintf(stderr, "stfortune: unknown option \"%s\"\n", argv[i]);
do_help(argv[0]);
}
}else{
range = FALSE;
quote_number = 0;
quotes = 0;
ptr = argv[i];
while(isdigit(*ptr)){
quote_number *= 10;
quote_number += *ptr - '0';
ptr++;
}
if(*ptr == '-'){
ptr++;
range = TRUE;
range_end = atoi(ptr);
if(range_end < quote_number)
range = FALSE;
}
number_flag = TRUE;
if((i<(argc-1)) || (range == TRUE)){
if(range == FALSE){
range_end = quote_number;
range = TRUE;
}
while(range_end > quote_number){
quotes = read_quote(filename, u_flag, number_flag, n_flag, quote_number);
quote_number++;
if((quotes != 0) && (range_end > quotes))
range_end = quotes;
}
if(i<(argc-1))
read_quote(filename, u_flag, number_flag, n_flag, quote_number);
}
}
}
read_quote(filename, u_flag, number_flag, n_flag, quote_number);
exit(0);
}