home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / PFortune3_0.lha / PFortune / PFortune.c next >
Encoding:
C/C++ Source or Header  |  1994-09-12  |  4.7 KB  |  137 lines

  1. echo;                           /* Execute to compile with SAS/C v6.x * /
  2.  
  3. SC PFortune.c LINK STARTUP=cres PARAMS=REGS NOCHECKABORT NOSTACKCHECK STRINGMERGE OPTIMIZE OPTIMIZERSIZE OPTIMIZERINLINELOCAL OPTIMIZERSCHEDULER OPTIMIZERCOMPLEXITY=3 OPTIMIZERDEPTH=3 OPTIMIZERRECURDEPTH=3
  4. delete PFortune.lnk PFortune.o
  5. quit
  6.                                  */
  7.  
  8. /*
  9.  *      $Filename: PFortune.c $
  10.  *      $Revision: 3.0 $
  11.  *      $Date: 1994/09/12 $
  12.  *
  13.  *      written by Peter Simons <simons@peti.GUN.de>
  14.  *
  15.  *      Prints a random fortune out of a database. This program
  16.  *      is public domain.
  17.  */
  18.  
  19. /**************************************************************************
  20.  *                                                                        *
  21.  * Sektion: Macros, Definitions, Includes, Structures                     *
  22.  *                                                                        *
  23.  **************************************************************************/
  24.  
  25. /************************************* Includes ***********/
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <dos/dos.h>
  29. #include <exec/memory.h>
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <proto/intuition.h>
  33.  
  34. /************************************* Defines ************/
  35. #define USAGE "USAGE: %s <database>\n"
  36. #define BUFFERSIZE 2047
  37. #define MARGIN 80               /*
  38.                                  * Customize this defines to fulfill
  39.                                  * your needs. */
  40.  
  41. /************************************* Prototypes *********/
  42. unsigned int rangerand(unsigned int);
  43. void print_centered(int, char *);
  44.  
  45. /************************************* Global Variables ***/
  46. static const char __OSVer[] = "$VER: PFortune 3.0 (12.09.94) "
  47. "written by Peter Simons <simons@peti.GUN.de>";
  48. char PRGNAME[] = "PFortune";
  49.  
  50. /**************************************************************************
  51.  *                                                                        *
  52.  * Sektion: Hauptprogramm                                                 *
  53.  *                                                                        *
  54.  **************************************************************************/
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58.         BPTR fp;
  59.         char linebuf[BUFFERSIZE + 1];
  60.         STRPTR rc;
  61.         ULONG dummy, micros, linecount;
  62.  
  63.         if (argc > 1) {
  64.                 if (!strcmp("?", argv[1]) || argc > 2) {
  65.                         Printf(USAGE, PRGNAME);
  66.                         return 0;
  67.                 }
  68.         }
  69.  
  70.         CurrentTime(&dummy, µs);
  71.         srand(micros);
  72.  
  73.         if (fp = Open((argc == 2) ? argv[1] : "S:Fortune.data", MODE_OLDFILE)) {
  74.                 Seek(fp, 0L, OFFSET_END);
  75.                 linecount = Seek(fp, 0L, OFFSET_CURRENT);
  76.  
  77.                 while (TRUE) {
  78.                         Seek(fp, rangerand(linecount), OFFSET_BEGINNING);
  79.  
  80.                         if (!(FGets(fp, linebuf, BUFFERSIZE)))
  81.                                 continue;
  82.  
  83.                         while (rc = FGets(fp, linebuf, BUFFERSIZE)) {
  84.                                 if (*linebuf == ' ' || *linebuf == '#')
  85.                                         continue;
  86.                                 Printf("\n");
  87.                                 print_centered(MARGIN, linebuf);
  88.                                 break;
  89.                         }
  90.                         if (!rc)
  91.                                 continue;
  92.  
  93.                         while (TRUE) {
  94.                                 if (FGets(fp, linebuf, BUFFERSIZE)) {
  95.                                         if (*linebuf == ' ')
  96.                                                 print_centered(MARGIN, linebuf + 1);
  97.                                         else
  98.                                                 break;
  99.                                 }
  100.                                 else
  101.                                         break;
  102.                         }
  103.                         break;
  104.                 }
  105.  
  106.                 Close(fp);
  107.                 Printf("\n");
  108.         }
  109.         else {
  110.                 PrintFault(IoErr(), PRGNAME);
  111.                 return 10;
  112.         }
  113.  
  114. }
  115.  
  116. /**************************************************************************
  117.  *                                                                        *
  118.  * Sektion: Unterprogramme                                                *
  119.  *                                                                        *
  120.  **************************************************************************/
  121.  
  122. void print_centered(int number, char *string)
  123. {
  124.         int i;
  125.  
  126.         i = (number - strlen(string)) / 2;
  127.         for ( ; i > 0; i--)
  128.                 Printf(" ");
  129.         Printf("%s", string);
  130.  
  131. }
  132.  
  133. unsigned int rangerand(unsigned int maximum)
  134. {
  135.         return (rand() % maximum);
  136. }
  137.