home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 426.lha / SigTune_v1.0 / SigTune.c < prev    next >
C/C++ Source or Header  |  1990-10-07  |  6KB  |  171 lines

  1. ; /* Execute me to compile!  Nifty stuff, eh?  Actual embedded DOS script.
  2.  
  3. lc -Lt -j73i -v -O SigTune
  4.  
  5. quit
  6. */
  7.              /**********************************************************/
  8.             /*  SigTune v1.0    Released 7-24-90 by Ben Scott ©1990   */
  9.            /* Written and Compiled with Lattice (SAS?) Amiga C v5.05 */
  10.           /*                                                        */
  11.          /* This program is designed for use with the Dillon/Loftus*/
  12.         /* UUCP port.  It allows a .signature file to contain any */
  13.        /* of a number of quotes, randomly selected from a file   */
  14.       /* and mapped into the signature according to format codes*/
  15.      /* which are user-selectable.  Sends resultant to stdout  */
  16.     /* or to a file.  Could theoretically be used elsewhere(?)*/
  17.    /*                                                        */
  18.   /**********************************************************/
  19. /*\*                                                        *\
  20.    \* Still to add later:  Faster (buffered?) operation, and *\
  21.     \* auto-centering for quotes shorter than the "$$$" line. *\
  22.      \* All this and much, much more coming soon in the amazing*\
  23.       \* SigTune v2.0: watch for it at your nearest BBS! - BMS  *\
  24.        \*                                                        *\
  25.         \**********************************************************\*/
  26.  
  27.  
  28. #include <stdlib.h>     /* not awfully necessary, because apparently */
  29. #include <fcntl.h>     /* the compiler includes these for you, but  */
  30. #include <time.h>     /* I like to be thourough so I left them in. */
  31.  
  32. #define DEFAULT_CHAR '$'
  33.  
  34.  
  35. main (int argc,char* argv[])
  36.  
  37. {
  38.  
  39. void usage();              /* Function call to print help and usage info   */
  40. int  readln();            /* Function which will read a file to first EOL */
  41.  
  42. int  lines, x = 0;      /* # of lines expected in UULIB:Tune, & counter */
  43. int  rndum;            /* Random number to decide which line to insert */
  44. int  sigfd, tunefd;   /* File descriptors for files Sig and Tune      */
  45. char line[255];      /* Buffer for the selected line in UULIB:Tune   */
  46. char s1;            /* char for the insertion procedure, sig char 1 */
  47. char eschar;       /* character to designate insertion point in SIG*/
  48. char lnum[3];     /* read buffer for the first line of UULIB:Tune */
  49.  
  50.  
  51. if ((argc==2) && (*argv[1]=='?')) usage(); /* if ? is sole arg */
  52.  
  53.    /* Open files and check for errors, exit cleanly if found */
  54.   /* Check for the files in current dir first, then UULIB:  */
  55.  
  56. if ((sigfd =  open ("Sig",  0, 0)) == -1)  /* can't find SIG in CD? */
  57.  
  58.     { if ((sigfd =  open ("UULIB:Sig",  0, 0)) == -1)
  59.             { puts ("Could not open or find file SIG!");
  60.               exit (20); }
  61.     };
  62.  
  63. if ((tunefd = open ("Tune", 0, 0)) == -1)  /* can't find TUNE in CD? */
  64.  
  65.     { if ((tunefd = open ("UULIB:Tune", 0, 0)) == -1)
  66.             { close (sigfd);
  67.               puts ("Could not open or find file TUNE!");
  68.               exit (20); }
  69.     };
  70.  
  71. if ((readln (tunefd, lnum, 3)) < 1 )      /* Read # of lines in Tune */
  72.         { close (sigfd);
  73.           close (tunefd);
  74.           puts ("Error reading from file TUNE!");
  75.           exit (20); }
  76.  
  77.  
  78. if ((lines = atoi (lnum)) <= 0)   /* Convert #lines to integer value */
  79.         { close (sigfd);
  80.           close (tunefd);
  81.           puts ("Bad or missing # of lines in TUNE!");
  82.           exit (10); }
  83.  
  84.  /* Parse the command args for a different escape char, if any */
  85.  
  86. if (argc > 1) { eschar = *argv[1]; } else eschar = DEFAULT_CHAR;
  87.  
  88.  
  89. srand ((unsigned) time (0));    /* Set random # seed by system clock   */
  90. rndum = (rand() % lines + 1);  /* get a random number from 1 to lines */
  91.  
  92. for (; rndum; rndum--)  {
  93.              if ((readln (tunefd,line,255)) < 1)
  94.         { close (sigfd);                       /* read to desired */
  95.           close (tunefd);                     /* quote in TUNE,  */
  96.           puts ("Unexpected EOF in TUNE!");  /* exit if error.  */
  97.           exit(20);  }
  98.                         }      /* end for */
  99.  
  100.   /* while loop reads Sig, replaces all occurances of eschar */
  101.  /* with the line read from Tune, otherwise echos the read  */
  102. /* character. If Tune line runs out, writes spaces instead */
  103.  
  104.  
  105. while (read (sigfd, &s1, (unsigned int) 1) == 1)  {
  106.  
  107.     if (s1 == eschar) { if (line[x+1]) putchar(line[x++]);
  108.                           else putchar(' ');               }
  109.       else putchar(s1);
  110.                                                   }  /* end while */
  111.  
  112.  /* All done, no problems, tidy up and exit! */
  113. /* This also is not critical to do, but...  */
  114.  
  115. close (sigfd);
  116. close (tunefd);
  117.  
  118. exit (0);
  119.  
  120. }
  121.  
  122. /********************************************************************/
  123.  
  124. int readln (int fd, char* buf, int len)
  125.  
  126.   /* function readln returns number of characters read or 0 if error */
  127.  /* The idea is to read to an end-of-line character denoted by 0x0A */
  128. /* Note: begins the read from the current position in the file fd. */
  129.  
  130. {
  131.  
  132. int i;              /* counter variable */
  133.  
  134. for (i = 0; i < len; i++)  {
  135.  
  136.   if ((read (fd, buf, (unsigned int) 1)) != 1)
  137.         { puts ("Error in read!");
  138.           return (0);      };
  139.  
  140.   if (*buf == 0x0A) break;        /* if EOL encountered, exit early */
  141.  
  142.   buf++;                  }     /* end for loop */
  143.  
  144. *(buf+1) = (char) 0;          /* add null termination, and exit */
  145. return (i);
  146.  
  147. }
  148.  
  149.  
  150. /*********************************************************************/
  151.  
  152. void usage()
  153.  
  154. /* Prints a bunch of inane crud when you call SigTune with a "?" arg */
  155.  
  156. {
  157.  
  158. puts ("SigTune v1.0   ©1990 by Ben Scott");
  159. puts ("");
  160. puts ("Usage: SigTune >output [escape char]  (args optional)");
  161. puts ("");
  162. puts ("Requires the files SIG and TUNE to be in the current dir or in UULIB:.");
  163. puts ("The first line in TUNE must be the number of lines of quotes to expect.");
  164. puts ("With no args, SigTune will assume an escape character of \"$\" and will");
  165. puts ("write to the current stdout, which you may redirect to >UULIB:.signature.");
  166. puts ("");
  167.  
  168. exit (0);
  169.  
  170. }
  171.