[net.sources] source for nmail

mcm (03/01/83)

/*
 * nmail.c -- a network mail interface.
 * This program accepts as arguments the standard network
 * addresses, and expands the section of an argument up to a
 * "!" into the actual path the letter must take to get to that
 * site.  For example, to get to swd at duke, the argument
 * "duke!swd" is expanded to "mcnc!duke!swd".  "decvax!ittvax!swatt"
 * would expand to "mcnc!duke!decvax!ittvax!swatt".
 *
 * This program uses the output of the mkpath program developed by
 * Mike Mitchell at ikonas (duke!mcnc!ikonas!mcm).
 *
 * The input data should look like:
 *	sitename (any number of blanks or tabs) printf-string to get there
 *
 * The input data must be sorted.
 *
 * Version 2.0	10/15/82
 *
 * Written by Mike Mitchell (decvax!duke!mcnc!ikonas!mcm)
 *
 */

#include <stdio.h>

#define MAILER	"/bin/mail"		/* the mail program to use */
#define DATA	"/usr/lib/PATHS"	/* where the data is kept */
#define OK	1	/* all expansions done correctly */
#define BAD	0	/* an expansion done incorrectly */

main(argc,argv,envp)
int argc;
char **argv;
char **envp;
{
	FILE *fopen(),*fd;
	char route[BUFSIZ],tmp[BUFSIZ];
	char site[BUFSIZ],*cp,*index();
	char **args,*pos, *malloc(), **calloc();
	int num,rc,flag;

	args = calloc(argc+1, sizeof(char *));
	if (args == NULL) {
		fprintf(stderr, "Cannot get memory for argument list.\n");
		exit(1);
		}
	args[0] = "mail";
	num = 1;
	flag = OK;
	fd = fopen(DATA,"r");
	while(--argc) {
		argv++;
		if ((cp=index(*argv,'!')) == NULL) {
			pos = malloc(strlen(*argv) + 1);
			if (pos == NULL) {
				fprintf(stderr,
					"Cannot get memory for name %s\n",
					*argv);
				exit(1);
				}
			strcpy(pos,*argv);
			args[num++] = pos;
			}
		else {
			*cp = '\0';
			while( fscanf(fd,"%s%s",site,route) != EOF) {
				rc = strcmp(site, *argv);
				if (rc > 0) break;
				if (rc == 0) {
					sprintf(tmp,route,(cp+1));
					pos = malloc(strlen(tmp)+1);
					if (pos == NULL) {
						fprintf(stderr,
						"Can't get mem for path %s\n",
						tmp);
						exit(1);
						}
					strcpy(pos,tmp);
					args[num++] = pos;
					break;
					}
				}
			if (rc != 0) {
				printf("Can't get to %s from here\n",*argv);
				flag = BAD;
				}
			fseek(fd,0L,0);
			}
		}
	if (flag) {
		args[num] = NULL;
		for (rc = 0; rc < num; rc++)
			printf("%s ",args[rc]);
		printf("\n");
		execve(MAILER,args,envp);
		fprintf(stderr, "execve of %s failed!\n", MAILER);
		exit(1);
		}
	}