home *** CD-ROM | disk | FTP | other *** search
- echo; /* Execute to compile with SAS/C v6.x * /
-
- SC PFortune.c LINK STARTUP=cres PARAMS=REGS NOCHECKABORT NOSTACKCHECK STRINGMERGE OPTIMIZE OPTIMIZERSIZE OPTIMIZERINLINELOCAL OPTIMIZERSCHEDULER OPTIMIZERCOMPLEXITY=3 OPTIMIZERDEPTH=3 OPTIMIZERRECURDEPTH=3
- delete PFortune.lnk PFortune.o
- quit
- */
-
- /*
- * $Filename: PFortune.c $
- * $Revision: 3.0 $
- * $Date: 1994/09/12 $
- *
- * written by Peter Simons <simons@peti.GUN.de>
- *
- * Prints a random fortune out of a database. This program
- * is public domain.
- */
-
- /**************************************************************************
- * *
- * Sektion: Macros, Definitions, Includes, Structures *
- * *
- **************************************************************************/
-
- /************************************* Includes ***********/
- #include <stdlib.h>
- #include <string.h>
- #include <dos/dos.h>
- #include <exec/memory.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <proto/intuition.h>
-
- /************************************* Defines ************/
- #define USAGE "USAGE: %s <database>\n"
- #define BUFFERSIZE 2047
- #define MARGIN 80 /*
- * Customize this defines to fulfill
- * your needs. */
-
- /************************************* Prototypes *********/
- unsigned int rangerand(unsigned int);
- void print_centered(int, char *);
-
- /************************************* Global Variables ***/
- static const char __OSVer[] = "$VER: PFortune 3.0 (12.09.94) "
- "written by Peter Simons <simons@peti.GUN.de>";
- char PRGNAME[] = "PFortune";
-
- /**************************************************************************
- * *
- * Sektion: Hauptprogramm *
- * *
- **************************************************************************/
-
- int main(int argc, char *argv[])
- {
- BPTR fp;
- char linebuf[BUFFERSIZE + 1];
- STRPTR rc;
- ULONG dummy, micros, linecount;
-
- if (argc > 1) {
- if (!strcmp("?", argv[1]) || argc > 2) {
- Printf(USAGE, PRGNAME);
- return 0;
- }
- }
-
- CurrentTime(&dummy, µs);
- srand(micros);
-
- if (fp = Open((argc == 2) ? argv[1] : "S:Fortune.data", MODE_OLDFILE)) {
- Seek(fp, 0L, OFFSET_END);
- linecount = Seek(fp, 0L, OFFSET_CURRENT);
-
- while (TRUE) {
- Seek(fp, rangerand(linecount), OFFSET_BEGINNING);
-
- if (!(FGets(fp, linebuf, BUFFERSIZE)))
- continue;
-
- while (rc = FGets(fp, linebuf, BUFFERSIZE)) {
- if (*linebuf == ' ' || *linebuf == '#')
- continue;
- Printf("\n");
- print_centered(MARGIN, linebuf);
- break;
- }
- if (!rc)
- continue;
-
- while (TRUE) {
- if (FGets(fp, linebuf, BUFFERSIZE)) {
- if (*linebuf == ' ')
- print_centered(MARGIN, linebuf + 1);
- else
- break;
- }
- else
- break;
- }
- break;
- }
-
- Close(fp);
- Printf("\n");
- }
- else {
- PrintFault(IoErr(), PRGNAME);
- return 10;
- }
-
- }
-
- /**************************************************************************
- * *
- * Sektion: Unterprogramme *
- * *
- **************************************************************************/
-
- void print_centered(int number, char *string)
- {
- int i;
-
- i = (number - strlen(string)) / 2;
- for ( ; i > 0; i--)
- Printf(" ");
- Printf("%s", string);
-
- }
-
- unsigned int rangerand(unsigned int maximum)
- {
- return (rand() % maximum);
- }
-