xref: /csrg-svn/usr.bin/uucp/libuu/versys.c (revision 33967)
113698Ssam #ifndef lint
2*33967Srick static char sccsid[] = "@(#)versys.c	5.7	(Berkeley) 04/05/88";
313698Ssam #endif
413698Ssam 
513698Ssam #include "uucp.h"
618629Sralph #include <stdio.h>
718629Sralph #include <ctype.h>
813698Ssam 
923693Sbloom /*LINTLIBRARY*/
1013698Ssam 
1125148Sbloom char PhoneNumber[MAXPH];
1225148Sbloom 
1318629Sralph /*
1418629Sralph  *	verify system names n1 and n2
1518629Sralph  *	return codes:  SUCCESS  |  FAIL
1613698Ssam  *
1718629Sralph  *	NOTE:
1818629Sralph  *		the old calling sequence was versys(name) but is
1918629Sralph  *	now versys(&name) so that we can perform aliasing!!!!
2018629Sralph  *	See accompanying changes in uucp.c and uux.c
2118629Sralph  *		-- Ray Essick, April 27, 1984
2213698Ssam  */
2318629Sralph versys(nameptr)
2418629Sralph register char **nameptr;
2513698Ssam {
2613698Ssam 	register FILE *fp;
2718629Sralph 	char line[BUFSIZ];
2818629Sralph 	char *name;
2913698Ssam 
3018629Sralph 	DEBUG (11, "Before Alias: %s\n", *nameptr);
3123693Sbloom 	uualias (nameptr);			/* alias expansion */
3218629Sralph 	DEBUG (11, "After Alias: %s\n", *nameptr);
3318629Sralph 	name = *nameptr;			/* dereference */
3418629Sralph 
3533580Srick 	if (name[0] == '\0' || strncmp(name, Myname, MAXBASENAME) == 0)
3617847Sralph 		return SUCCESS;
3713698Ssam 
3813698Ssam 	fp = fopen(SYSFILE, "r");
39*33967Srick 	if (fp == NULL) {
40*33967Srick 		syslog(LOG_ERR, "fopen(%s) failed: %m", SYSFILE);
41*33967Srick 		cleanup(FAIL);
42*33967Srick 	}
4325148Sbloom 	PhoneNumber[0] = '\0';
4413698Ssam 	while (cfgets(line, sizeof(line), fp) != NULL) {
4513698Ssam 		char *targs[100];
4613698Ssam 
4717847Sralph 		getargs(line, targs, 100);
4823693Sbloom 		if (strncmp(name, targs[0], MAXBASENAME) == SAME) {
4913698Ssam 			fclose(fp);
5033580Srick 			if (targs[F_PHONE])
5133580Srick 				strncpy(PhoneNumber, targs[F_PHONE], MAXPH);
5217847Sralph 			return SUCCESS;
5313698Ssam 		}
5413698Ssam 	}
5513698Ssam 	fclose(fp);
5617847Sralph 	return FAIL;
5713698Ssam }
5818629Sralph 
5918629Sralph /*
6018629Sralph  *	Works (sort of) like rhost(3) on 4.1[abc] Bsd systems.
6118629Sralph  *
6218629Sralph  *	Looks for the host in the L.aliases file and returns the
6318629Sralph  *	"standard" name by modifying the pointer. The returned
6418629Sralph  *	value is saved with malloc(3) so it isn't zapped by
6518629Sralph  *	subsequent calls.
6618629Sralph  *
6718629Sralph  *	Returns:
6818629Sralph  *		FAIL		No L.aliases file
6918629Sralph  *		SUCCESS		Anything else
7018629Sralph  */
7118629Sralph 
7225148Sbloom uualias(hostptr)
7318629Sralph char  **hostptr;			  /* we change it */
7418629Sralph {
7525148Sbloom 	FILE *Aliases;			  /* list of aliases */
7618629Sralph 	char buf[BUFSIZ];
7718629Sralph 	int atend;
7818629Sralph 	char *p, *q;
7918629Sralph 	char *koshername;		 /* "official" name */
8018629Sralph 
8118629Sralph 	if ((Aliases = fopen(ALIASFILE, "r")) == NULL) {
8218629Sralph 		DEBUG(11, "No %s file\n", ALIASFILE);
8318629Sralph 		return FAIL;			  /* no alias file */
8418629Sralph 	}
8518629Sralph 
8618629Sralph 	DEBUG (11, "Alias expansion for %s\n", *hostptr);
8725148Sbloom 	while (cfgets(buf, sizeof (buf), Aliases)) {
8818629Sralph 		p = &buf[0];
8918629Sralph 		atend = 0;
9018629Sralph 		DEBUG(11, "Alias line: %s\n", buf);
9118629Sralph 
9218629Sralph 		while (!atend) {
9318629Sralph 			while (isspace(*p) && *p != '\n')
9418629Sralph 				p++;			  /* skip white space */
9518629Sralph 			q = p;
9618629Sralph 			while (!isspace(*q) && *q != '\n')
9718629Sralph 				q++;			  /* find end */
9818629Sralph 			if (*q == '\n')
9918629Sralph 				atend++;		  /* last entry */
10018629Sralph 			*q = '\0';
10118629Sralph 			DEBUG(11, "Compare against: %s\n", p);
10218629Sralph 			if (strcmp(*hostptr, p) == 0)/* match? */ {
10323693Sbloom 				koshername = malloc((unsigned)strlen(buf) + 1);
10418629Sralph 				strcpy(koshername, buf); /* save it */
10518629Sralph 				fclose(Aliases);
10618629Sralph 				DEBUG(4, "Alias: %s to ", *hostptr);
10718629Sralph 				DEBUG(4, "%s\n", koshername);
10818629Sralph 				*hostptr = koshername;	  /* correct one */
10918629Sralph 				return SUCCESS;		  /* all is well */
11018629Sralph 			}
11118629Sralph 			p = q + 1;			  /* try next entry */
11218629Sralph 		}
11318629Sralph 
11418629Sralph 	}
11518629Sralph 	fclose(Aliases);
11618629Sralph 	DEBUG(11, "Alias doesn't match %s, remains unchanged\n", *hostptr);
11718629Sralph 	return SUCCESS;				  /* unchanged host */
11818629Sralph }
119