[comp.sys.mac.misc] .signature randomizer source

252u3129@fergvax.unl.edu (Mike Gleason) (10/29/90)

I got several requests from folks wanting the .signature randomizer source, so
here it is.  Comments, bug reports welcome.



#!/bin/sh
# to extract, remove the header and type "sh filename"
if `test ! -d ./SigRnd`
then
  mkdir ./SigRnd
  echo "mkdir ./SigRnd"
fi
if `test ! -s ./SigRnd/makefile`
then
echo "writing ./SigRnd/makefile"
cat > ./SigRnd/makefile << '\End\Of\Shar\'
all:
	cc -O sigrnd.c -o sigrnd

gcc:
	gcc -O sigrnd.c -o sigrnd
# if you can, compile with gcc; the executable will be about 10% smaller.
\End\Of\Shar\
else
  echo "will not over write ./SigRnd/makefile"
fi
if `test ! -s ./SigRnd/sigrnd.1`
then
echo "writing ./SigRnd/sigrnd.1"
cat > ./SigRnd/sigrnd.1 << '\End\Of\Shar\'
.TH SIGRND 1 "25 Oct 1990"
.SH NAME
sigrnd \- select a .signature file at random
.SH SYNOPSIS
.B sigrnd
home_directory signature_subdir/prefix 
[
\-v
]
.SH DESCRIPTION
.LP
A .signature file is used by news readers, so that when you write a
message, the .signature file is appended to your message.  The
.signature file is used primarily to display your name and email
address, but often it contains quotes, disclaimers, and anecdotes.
Sigrnd picks a file at random from a group of files, so you have a
different .signature each time you read news.   
.SH PARAMETERS
.LP
Sigrnd needs to know where to put your randomized .signature file, and
also where the group of files that you want to pick the .signature from
is.  For the first parameter simply pass the pathname to your home
directory.  The second parameter is a bit tricky.  First you must name
all the files that you want to randomize to begin with the same prefix.
Let's say you have made three signatures and they are in a subdirectory
called "misc."  Since all the files must begin with the same prefix, you
choose something logical for a prefix, "sig."  Sigrnd looks in the
"misc" subdirectory for files beginning with "sig".  In addition to the
requirement that your files must begin with a prefix, they must also end
with a digit from 0-9 (In other words, you can have up to ten
signatures).  So, you should name your files "sig0", "sig1", and "sig2".
Now to specify the second parameter, you would pass "misc/sig" since
they reside in the "misc" directory and they all begin with "sig."
Lastly, if you pass \-v as the third parameter, sigrnd will tell you how
many signatures it found, and which one it picked.
.SH EXAMPLES
.ta 2.0i
.nf
sigrnd /usr/myaccount mysubdir/sigprefix -v
sigrnd $HOME junk/sig
sigrnd ~ junk/.sig -v
sigrnd $HOME .sig
.fi
.SH TIPS
.LP
Define an alias in your .login file that will call sigrnd before your
news reader; that way a new one will be picked each time you read news
(i.e. alias r 'sigrnd $HOME junk/sig; rn').  Or, if you are not running
csh, call sigrnd from your .profile.
.SH CREDITS
.LP
Version 1.0   25 Oct 90  Mike Gleason of NCEMRSoft, current email
address: 252u3129@fergvax.unl.edu
.SH SEE ALSO
.LP
rn(1), nn(1)
\End\Of\Shar\
else
  echo "will not over write ./SigRnd/sigrnd.1"
fi
if `test ! -s ./SigRnd/sigrnd.c`
then
echo "writing ./SigRnd/sigrnd.c"
cat > ./SigRnd/sigrnd.c << '\End\Of\Shar\'
/* .signatureRandomizer (sigrnd)

	v1.0  10-22-90  Mike Gleason
						 252u3129@fergvax.unl.edu
						 NCEMRSoft

	Public Domain.  Revise and repost as you wish. */

#define VERSION "1.0"
#define AUTHOR "Mike Gleason of NCEMRSoft"

/* what's a .signature file?

	when you write messages to post on usenet, (readnews, rn, nn...)
	if the mailer finds a file called ".signature" in your home
	directory, it will append that file to your messages.  that is
	a good place to put your name and email address, along with
	other stuff like quotes, disclaimers, etc. */




/* how to install:

	1.	 'make' me.  If you have gcc, compile it with that by typing
		 'make gcc', otherwise use cc and just type 'make'.

	2.  stick the compiled executable in a place where unix can
		 find it.

	3.	 make all the signature files you want to randomize (up to 10)
		 and name them with the same prefix. (i.e. let's say you
		 pick "sig" as your prefix.  then make each random signature
		 file start with "sig", and then tack on a digit after it,
		 like "sig0", "sig1", "sig2", etc., up to "sig9".  you
		 don't need to have all 10 files, so if you only had 3 files
		 to randomize you would just have "sig0", "sig1", and "sig2".

	4.  the program needs to know atleast 2 (atmost 3) things:
			a. what your home directory is (ie "/usr/myaccount");
			b. and what directory to look in to find the files
				to randomize AND the prefix that each random sig file
				has.  for this parameter you might pass
				"sig_dir/sig", and have your files called "sig0",
				"sig1", "sig2", etc; or, if you want to put your
				files right in your home directory rather than a
				sub directory, you could just pass "sig".
			c. an optional third parameter, which i will call
				verbose mode.  if you pass "-v" as the THIRD param,
				the program will tell you how many signature files
				it found, AND which signature file it picked to be
				your ".signature" file.
		 so, in summary, you might run the program like this:
			sigrnd /usr/myaccount tmp/sig_dir/sig -v
			(or) sigrnd ~ tmp/sig -v
			(or) sigrnd $HOME tmp/sig -v
			(or) sigrnd ~ sig

	5.  it would be a pain in the #$@ to type that everytime you
		 wanted to randomize your signatures, so i recommend that you
		 make an alias for your news reader to call sigrnd before it 
		 actually reads the news, like:
			alias r 'sigrnd /usr/myaccount tmp/sig_dir/sig; rn'
		 and put that in your .login file, *OR* just call sigrnd from
		 your .login (or .profile) file (I prefer the first method
		 because the login process takes awhile on my machine).
*/

#include <stdio.h>
#include <time.h>
 


main(argc, argv)
int argc;
char *argv[];
{
	int verbose = argc>3 ? 1 : 0;
								/* if we want info to print when run */
  	FILE *in, *out;		/* input and output file pointers. */
	char filename[80];	/* to open the sig files with */
	char signame[80];    /* to open .signature with */
	char digit = 0;		/* we'll substitute the last char
									of a filename with a digit (0-9) */
	int i, howmany = 0;	/* misc. counter; number of sigs found */
	int lastchar;			/* we will replace the last char with the
									'digit' char. */

	 
	if (argc < 3   ||   argc > 4)
	{
		fprintf(stderr, "\n%s home_directory signature_prefix [-v]\nVersion %s by %s \n\n", argv[0], VERSION, AUTHOR);
		exit(1);
	}

   strcpy(filename, argv[1]);
	strcat(filename, "/");
	strcat(filename, argv[2]);
		/* the previous makes home/local/sigPrefix */
	strcat(filename, "x");
		/* we can replace the 'x' character with a digit from 0-9 */
	lastchar = strlen(filename) - 1;
		/* keep track of the 'x' char */

	/* count how many sig files there should be.  If we get 'howmany'
		files in the next routine, we'll assume the user has sig0, sig1,
		etc, all the way to sigHowmany.  Even if the user wants to be a
		jerk and not have a contigious list of files, the pick routine
		will work since it found atleast one sig file.  If we don't
		find any, put up the little help text. */

	for (i=9; i>=0; i--) {
		filename[lastchar] = '0' + (char) i;
		if ((in = fopen(filename, "r")) != NULL) {
			howmany = i + 1;
			break;
		}
	}

	if (howmany == 0) {
		fprintf(stderr,
			"%s: I couldn't find a list of .signatures to randomize.\n",
			argv[0]);
		exit(1);
	}

	fclose(in);

	if (verbose) printf("%s: %d .signatures found.\n", argv[0],  howmany);

	/* pick a new signature file each time */
	srand( (unsigned int) time(NULL) );

	/* pick a sig file, and see if we can open it. */
	for (;;) {
		digit = rand() % howmany;
		filename[lastchar] = '0' + (char) digit;
		in = fopen(filename, "r");
		if (in) break;
	}

	if (verbose) printf("%s: selected number %d.\n", argv[0], digit);

	strcpy(signame, argv[1]);
	strcat(signame, "/.signature");	/* get full path */
	out = fopen(signame, "w");
	if (!out) {
		fprintf(stderr, "%s: error creating new signature file.\n",
			argv[0]);
		exit(1);
	}

	while ((i = getc(in)) != EOF) putc(i, out);
		/* copy the sig file into the .signature file */

	fclose(in);  fclose(out);
}
/* That's all folks! */
\End\Of\Shar\
else
  echo "will not over write ./SigRnd/sigrnd.c"
fi
echo "Finished archive 1 of 1"
exit
 mike gleason                                "How I've waited for you to come
 252u3129@fergvax.unl.edu                            I've been here all alone
                                  Now that you've arrived, please stay awhile
       And I promise not to keep you long... I'll keep you forever" -- Slayer