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 >
Wrap
C/C++ Source or Header
|
1990-10-07
|
6KB
|
171 lines
; /* Execute me to compile! Nifty stuff, eh? Actual embedded DOS script.
lc -Lt -j73i -v -O SigTune
quit
*/
/**********************************************************/
/* SigTune v1.0 Released 7-24-90 by Ben Scott ©1990 */
/* Written and Compiled with Lattice (SAS?) Amiga C v5.05 */
/* */
/* This program is designed for use with the Dillon/Loftus*/
/* UUCP port. It allows a .signature file to contain any */
/* of a number of quotes, randomly selected from a file */
/* and mapped into the signature according to format codes*/
/* which are user-selectable. Sends resultant to stdout */
/* or to a file. Could theoretically be used elsewhere(?)*/
/* */
/**********************************************************/
/*\* *\
\* Still to add later: Faster (buffered?) operation, and *\
\* auto-centering for quotes shorter than the "$$$" line. *\
\* All this and much, much more coming soon in the amazing*\
\* SigTune v2.0: watch for it at your nearest BBS! - BMS *\
\* *\
\**********************************************************\*/
#include <stdlib.h> /* not awfully necessary, because apparently */
#include <fcntl.h> /* the compiler includes these for you, but */
#include <time.h> /* I like to be thourough so I left them in. */
#define DEFAULT_CHAR '$'
main (int argc,char* argv[])
{
void usage(); /* Function call to print help and usage info */
int readln(); /* Function which will read a file to first EOL */
int lines, x = 0; /* # of lines expected in UULIB:Tune, & counter */
int rndum; /* Random number to decide which line to insert */
int sigfd, tunefd; /* File descriptors for files Sig and Tune */
char line[255]; /* Buffer for the selected line in UULIB:Tune */
char s1; /* char for the insertion procedure, sig char 1 */
char eschar; /* character to designate insertion point in SIG*/
char lnum[3]; /* read buffer for the first line of UULIB:Tune */
if ((argc==2) && (*argv[1]=='?')) usage(); /* if ? is sole arg */
/* Open files and check for errors, exit cleanly if found */
/* Check for the files in current dir first, then UULIB: */
if ((sigfd = open ("Sig", 0, 0)) == -1) /* can't find SIG in CD? */
{ if ((sigfd = open ("UULIB:Sig", 0, 0)) == -1)
{ puts ("Could not open or find file SIG!");
exit (20); }
};
if ((tunefd = open ("Tune", 0, 0)) == -1) /* can't find TUNE in CD? */
{ if ((tunefd = open ("UULIB:Tune", 0, 0)) == -1)
{ close (sigfd);
puts ("Could not open or find file TUNE!");
exit (20); }
};
if ((readln (tunefd, lnum, 3)) < 1 ) /* Read # of lines in Tune */
{ close (sigfd);
close (tunefd);
puts ("Error reading from file TUNE!");
exit (20); }
if ((lines = atoi (lnum)) <= 0) /* Convert #lines to integer value */
{ close (sigfd);
close (tunefd);
puts ("Bad or missing # of lines in TUNE!");
exit (10); }
/* Parse the command args for a different escape char, if any */
if (argc > 1) { eschar = *argv[1]; } else eschar = DEFAULT_CHAR;
srand ((unsigned) time (0)); /* Set random # seed by system clock */
rndum = (rand() % lines + 1); /* get a random number from 1 to lines */
for (; rndum; rndum--) {
if ((readln (tunefd,line,255)) < 1)
{ close (sigfd); /* read to desired */
close (tunefd); /* quote in TUNE, */
puts ("Unexpected EOF in TUNE!"); /* exit if error. */
exit(20); }
} /* end for */
/* while loop reads Sig, replaces all occurances of eschar */
/* with the line read from Tune, otherwise echos the read */
/* character. If Tune line runs out, writes spaces instead */
while (read (sigfd, &s1, (unsigned int) 1) == 1) {
if (s1 == eschar) { if (line[x+1]) putchar(line[x++]);
else putchar(' '); }
else putchar(s1);
} /* end while */
/* All done, no problems, tidy up and exit! */
/* This also is not critical to do, but... */
close (sigfd);
close (tunefd);
exit (0);
}
/********************************************************************/
int readln (int fd, char* buf, int len)
/* function readln returns number of characters read or 0 if error */
/* The idea is to read to an end-of-line character denoted by 0x0A */
/* Note: begins the read from the current position in the file fd. */
{
int i; /* counter variable */
for (i = 0; i < len; i++) {
if ((read (fd, buf, (unsigned int) 1)) != 1)
{ puts ("Error in read!");
return (0); };
if (*buf == 0x0A) break; /* if EOL encountered, exit early */
buf++; } /* end for loop */
*(buf+1) = (char) 0; /* add null termination, and exit */
return (i);
}
/*********************************************************************/
void usage()
/* Prints a bunch of inane crud when you call SigTune with a "?" arg */
{
puts ("SigTune v1.0 ©1990 by Ben Scott");
puts ("");
puts ("Usage: SigTune >output [escape char] (args optional)");
puts ("");
puts ("Requires the files SIG and TUNE to be in the current dir or in UULIB:.");
puts ("The first line in TUNE must be the number of lines of quotes to expect.");
puts ("With no args, SigTune will assume an escape character of \"$\" and will");
puts ("write to the current stdout, which you may redirect to >UULIB:.signature.");
puts ("");
exit (0);
}