echo;                           /* Execute to compile with SAS/C v6.x * /

SC Fortune.c LINK STARTUP=cres NOSTDIO PARAMETERS=REGISTERS NOSTACKCHECK STRINGMERGE OPTIMIZE OPTIMIZERSIZE
delete Fortune.lnk Fortune.o
quit
*/

/**
 **     $Filename: Fortune.c $
 **     $Revision: 2.1 $
 **     $Date: 1993/09/18 $
 **
 **     Prints a random fortune out of a database.
 **
 **     © Copyright 1993 Peter Simons, Germany
 **                      All Rights Reserved.
 **/

/**************************************************************************
 *                                                                        *
 * Sektion: Macros, Definitions, Includes, Structures                     *
 *                                                                        *
 **************************************************************************/

/************************************* Includes ***********/
#include <stdlib.h>
#include <string.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/ptool.h>
#include <dos/dos.h>
#include <libraries/ptoollibrary.h>

/************************************* Defines ************/
#define TRUE 1
#define EOF -1

#define PRGNAME "Fortune"

#define USAGE "%s v2.1 -- written by Peter Simons <simons@peti.GUN.de>\n" \
              "Randomly display a random fortune from a chosen database.\n\n" \
              "USAGE: %s <database>\n"
#define BUFFERSIZE 256
#define MARGIN 80               /* Customize this defines to fulfill
                                 * your needs. */

/************************************* Prototypes *********/
void print_rule(int, char *);
void my_exit(int);
int FGetS(char *, BPTR);

/************************************* Global Variables ***/
struct PToolLibrary *PToolBase;
BPTR fp = NULL;

/**************************************************************************
 *                                                                        *
 * Sektion: Hauptprogramm                                                 *
 *                                                                        *
 **************************************************************************/

#ifdef __SASC                   /* Control-C Handling abschalten */
void _CXBRK(void) { }
void chkabort(void) { }
#endif

int main(int argc, char *argv[])
{
        char *path = "S:Fortune.data", officialrule[BUFFERSIZE];
        int next, character;
        unsigned long linecount;

        if (!(PToolBase = (struct PToolLibrary *) OpenLibrary(PTOOLNAME, 2L)))
                return 10;

        if (argc > 1) {
                if (!strcmp("?", argv[1]) || argc > 2) {
                        PrintFSimple(USAGE, PRGNAME, PRGNAME);
                        exit(0);
                }
        }

/*---- Open "fortunes" file -------------------------------------------*/
        if (argc == 2)
                path = argv[1]; /* commandline has higher priority */
        if (!((fp = Open(path, MODE_OLDFILE)))) {
                ErrorHandleDos(PRGNAME, 1, 0L, path);
                my_exit(5);
        }

        Seek(fp, 0L, OFFSET_END);
        linecount = Seek(fp, 0L, OFFSET_CURRENT);

        while (TRUE) {
                Seek(fp, Random(linecount), OFFSET_BEGINNING);

                while ((character = FGetC(fp)) != EOF) {
                        if (character == '\n')
                                break;
                        continue;
                }

                if (!(FGetS(officialrule, fp)))
                        continue;
                else {
                        if (*officialrule == ' ' || *officialrule == '#')
                                continue;
                        PrintFSimple("\n");
                        print_rule(MARGIN, officialrule);
                }

                next = 0;
                while (TRUE) {
                        if (!(FGetS(officialrule, fp)))
                                my_exit(0);
                        else {
                                if (*officialrule != ' ' || *officialrule == '\n')
                                        my_exit(0);
                                print_rule(MARGIN, officialrule);
                                if (next++ >= 10)
                                        my_exit(0);
                        }
                        continue;
                }
        }
}

/**************************************************************************
 *                                                                        *
 * Sektion: Unterprogramme                                                *
 *                                                                        *
 **************************************************************************/

void my_exit(int num)
{
        if (fp)
                Close(fp);
        PrintFSimple("\n");
        CloseLibrary((struct Library *) PToolBase); PToolBase = NULL;
        exit(num);
}

void print_rule(int number, char *rule)
{
        int offset = 0, i;
        char fortune[BUFFERSIZE];

        *fortune = '\0';

        rule--;
        while (*(++rule)) {
                i = strlen(fortune);
                *(fortune + i++) = *rule;
                *(fortune + i) = NULL;
                offset++;
        }

        offset = ((number - offset) / 2);
        for (; offset > 0; strins(fortune, " "), offset--) ;
        PrintFSimple("%s", fortune);
}

int FGetS(char *string, BPTR fp)
{
        int c;

        do {
                if ((c = FGetC(fp)) == EOF)
                        return 0;
                *(string++) = c;
        } while (c != '\n');

        *string = '\0';

        return 1;
}

