xref: /csrg-svn/usr.bin/uucp/libuu/versys.c (revision 18629)
113698Ssam #ifndef lint
2*18629Sralph static char sccsid[] = "@(#)versys.c	5.3 (Berkeley) 04/10/85";
313698Ssam #endif
413698Ssam 
513698Ssam #include "uucp.h"
6*18629Sralph #include <stdio.h>
7*18629Sralph #include <ctype.h>
813698Ssam 
913698Ssam #define SNAMESIZE 7
1013698Ssam 
11*18629Sralph /*
12*18629Sralph  *	verify system names n1 and n2
13*18629Sralph  *	return codes:  SUCCESS  |  FAIL
1413698Ssam  *
15*18629Sralph  *	NOTE:
16*18629Sralph  *		the old calling sequence was versys(name) but is
17*18629Sralph  *	now versys(&name) so that we can perform aliasing!!!!
18*18629Sralph  *	See accompanying changes in uucp.c and uux.c
19*18629Sralph  *		-- Ray Essick, April 27, 1984
2013698Ssam  */
21*18629Sralph versys(nameptr)
22*18629Sralph register char **nameptr;
2313698Ssam {
2413698Ssam 	register FILE *fp;
25*18629Sralph 	char line[BUFSIZ];
26*18629Sralph 	char *name;
2713698Ssam 
28*18629Sralph 	DEBUG (11, "Before Alias: %s\n", *nameptr);
29*18629Sralph 	uualias(nameptr);			/* alias expansion */
30*18629Sralph 	DEBUG (11, "After Alias: %s\n", *nameptr);
31*18629Sralph 	name = *nameptr;			/* dereference */
32*18629Sralph 
33*18629Sralph 	if (strncmp(name, Myname, 7) == 0)
3417847Sralph 		return SUCCESS;
3513698Ssam 
3613698Ssam 	fp = fopen(SYSFILE, "r");
3717847Sralph 	ASSERT(fp != NULL, CANTOPEN, SYSFILE, 0);
3813698Ssam 	while (cfgets(line, sizeof(line), fp) != NULL) {
3913698Ssam 		char *targs[100];
4013698Ssam 
4117847Sralph 		getargs(line, targs, 100);
42*18629Sralph 		if (strncmp(name, targs[0], 7) == SAME) {
4313698Ssam 			fclose(fp);
4417847Sralph 			return SUCCESS;
4513698Ssam 		}
4613698Ssam 	}
4713698Ssam 	fclose(fp);
4817847Sralph 	return FAIL;
4913698Ssam }
50*18629Sralph 
51*18629Sralph /*
52*18629Sralph  *	Works (sort of) like rhost(3) on 4.1[abc] Bsd systems.
53*18629Sralph  *
54*18629Sralph  *	Looks for the host in the L.aliases file and returns the
55*18629Sralph  *	"standard" name by modifying the pointer. The returned
56*18629Sralph  *	value is saved with malloc(3) so it isn't zapped by
57*18629Sralph  *	subsequent calls.
58*18629Sralph  *
59*18629Sralph  *	Returns:
60*18629Sralph  *		FAIL		No L.aliases file
61*18629Sralph  *		SUCCESS		Anything else
62*18629Sralph  */
63*18629Sralph 
64*18629Sralph uualias(hostptr)
65*18629Sralph char  **hostptr;			  /* we change it */
66*18629Sralph {
67*18629Sralph 	FILE * Aliases;			  /* list of aliases */
68*18629Sralph 	char buf[BUFSIZ];
69*18629Sralph 	int atend;
70*18629Sralph 	char *p, *q;
71*18629Sralph 	char *koshername;		 /* "official" name */
72*18629Sralph 
73*18629Sralph 	if ((Aliases = fopen(ALIASFILE, "r")) == NULL) {
74*18629Sralph 		DEBUG(11, "No %s file\n", ALIASFILE);
75*18629Sralph 		return FAIL;			  /* no alias file */
76*18629Sralph 	}
77*18629Sralph 
78*18629Sralph 	DEBUG (11, "Alias expansion for %s\n", *hostptr);
79*18629Sralph 	while (fgets(buf, sizeof (buf), Aliases)) {
80*18629Sralph 		if (buf[0] == '#')		  /* comment line */
81*18629Sralph 			continue;
82*18629Sralph 		p = &buf[0];
83*18629Sralph 		atend = 0;
84*18629Sralph 		DEBUG(11, "Alias line: %s\n", buf);
85*18629Sralph 
86*18629Sralph 		while (!atend) {
87*18629Sralph 			while (isspace(*p) && *p != '\n')
88*18629Sralph 				p++;			  /* skip white space */
89*18629Sralph 			q = p;
90*18629Sralph 			while (!isspace(*q) && *q != '\n')
91*18629Sralph 				q++;			  /* find end */
92*18629Sralph 			if (*q == '\n')
93*18629Sralph 				atend++;		  /* last entry */
94*18629Sralph 			*q = '\0';
95*18629Sralph 			DEBUG(11, "Compare against: %s\n", p);
96*18629Sralph 			if (strcmp(*hostptr, p) == 0)/* match? */ {
97*18629Sralph 				koshername = malloc(strlen(buf) + 1);
98*18629Sralph 				strcpy(koshername, buf); /* save it */
99*18629Sralph 				fclose(Aliases);
100*18629Sralph 				DEBUG(4, "Alias: %s to ", *hostptr);
101*18629Sralph 				DEBUG(4, "%s\n", koshername);
102*18629Sralph 				*hostptr = koshername;	  /* correct one */
103*18629Sralph 				return SUCCESS;		  /* all is well */
104*18629Sralph 			}
105*18629Sralph 			p = q + 1;			  /* try next entry */
106*18629Sralph 		}
107*18629Sralph 
108*18629Sralph 	}
109*18629Sralph 	fclose(Aliases);
110*18629Sralph 	DEBUG(11, "Alias doesn't match %s, remains unchanged\n", *hostptr);
111*18629Sralph 	return SUCCESS;				  /* unchanged host */
112*18629Sralph }
113