xref: /illumos-gate/usr/src/cmd/bnu/gnxseq.c (revision 518f6e4f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "uucp.h"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * get next conversation sequence number
297c478bd9Sstevel@tonic-gate  *	rmtname	-> name of remote system
307c478bd9Sstevel@tonic-gate  * returns:
317c478bd9Sstevel@tonic-gate  *	0	-> no entery
327c478bd9Sstevel@tonic-gate  *	1	-> 0 sequence number
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate int
gnxseq(char * rmtname)35*518f6e4fSToomas Soome gnxseq(char *rmtname)
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	register FILE *fp0, *fp1;
387c478bd9Sstevel@tonic-gate 	register struct tm *tp;
397c478bd9Sstevel@tonic-gate 	int count = 0, ct, ret;
407c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ], name[NAMESIZE];
417c478bd9Sstevel@tonic-gate 	time_t clock;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	if (access(SQFILE, 0) != 0)
44*518f6e4fSToomas Soome 		return (0);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	{
47*518f6e4fSToomas Soome 		int i;
48*518f6e4fSToomas Soome 		for (i = 0; i < 5; i++)
49*518f6e4fSToomas Soome 			if ((ret = mklock(SQLOCK)) == SUCCESS)
50*518f6e4fSToomas Soome 				break;
517c478bd9Sstevel@tonic-gate 		sleep(5);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 	if (ret != SUCCESS) {
547c478bd9Sstevel@tonic-gate 		logent("CAN'T LOCK", SQLOCK);
557c478bd9Sstevel@tonic-gate 		DEBUG(4, "can't lock %s\n", SQLOCK);
56*518f6e4fSToomas Soome 		return (0);
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 	if ((fp0 = fopen(SQFILE, "r")) == NULL)
59*518f6e4fSToomas Soome 		return (0);
607c478bd9Sstevel@tonic-gate 	if ((fp1 = fopen(SQTMP, "w")) == NULL) {
617c478bd9Sstevel@tonic-gate 		fclose(fp0);
62*518f6e4fSToomas Soome 		return (0);
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 	chmod(SQTMP, DFILEMODE);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp0) != NULL) {
677c478bd9Sstevel@tonic-gate 		ret = sscanf(buf, "%s%d", name, &ct);
687c478bd9Sstevel@tonic-gate 		if (ret < 2)
697c478bd9Sstevel@tonic-gate 			ct = 0;
707c478bd9Sstevel@tonic-gate 		name[7] = '\0';
717c478bd9Sstevel@tonic-gate 		if (ct > 9998)
727c478bd9Sstevel@tonic-gate 			ct = 0;
737c478bd9Sstevel@tonic-gate 		if (strncmp(rmtname, name, SYSNSIZE) != SAME) {
747c478bd9Sstevel@tonic-gate 			fputs(buf, fp1);
757c478bd9Sstevel@tonic-gate 			continue;
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 		/*
797c478bd9Sstevel@tonic-gate 		 * found name
807c478bd9Sstevel@tonic-gate 		 */
817c478bd9Sstevel@tonic-gate 		count = ++ct;
827c478bd9Sstevel@tonic-gate 		time(&clock);
837c478bd9Sstevel@tonic-gate 		tp = localtime(&clock);
847c478bd9Sstevel@tonic-gate 		fprintf(fp1, "%s %d %d/%d-%d:%2.2d\n", name, ct,
85*518f6e4fSToomas Soome 		    tp->tm_mon + 1, tp->tm_mday, tp->tm_hour,
86*518f6e4fSToomas Soome 		    tp->tm_min);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		/*
897c478bd9Sstevel@tonic-gate 		 * write should be checked
907c478bd9Sstevel@tonic-gate 		 */
917c478bd9Sstevel@tonic-gate 		while (fgets(buf, BUFSIZ, fp0) != NULL)
927c478bd9Sstevel@tonic-gate 			fputs(buf, fp1);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	fclose(fp0);
957c478bd9Sstevel@tonic-gate 	fclose(fp1);
967c478bd9Sstevel@tonic-gate 	if (count == 0) {
977c478bd9Sstevel@tonic-gate 		rmlock(SQLOCK);
987c478bd9Sstevel@tonic-gate 		unlink(SQTMP);
997c478bd9Sstevel@tonic-gate 	}
100*518f6e4fSToomas Soome 	return (count);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * commit sequence update
1057c478bd9Sstevel@tonic-gate  * returns:
1067c478bd9Sstevel@tonic-gate  *	0	-> ok
1077c478bd9Sstevel@tonic-gate  *	other	-> link failed
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate int
cmtseq(void)110*518f6e4fSToomas Soome cmtseq(void)
1117c478bd9Sstevel@tonic-gate {
112*518f6e4fSToomas Soome 	int ret;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((ret = access(SQTMP, 0)) != 0) {
1157c478bd9Sstevel@tonic-gate 		rmlock(SQLOCK);
116*518f6e4fSToomas Soome 		return (0);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	unlink(SQFILE);
1197c478bd9Sstevel@tonic-gate 	ret = link(SQTMP, SQFILE);
1207c478bd9Sstevel@tonic-gate 	unlink(SQTMP);
1217c478bd9Sstevel@tonic-gate 	rmlock(SQLOCK);
122*518f6e4fSToomas Soome 	return (ret);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * unlock sequence file
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate void
ulkseq(void)129*518f6e4fSToomas Soome ulkseq(void)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	unlink(SQTMP);
1327c478bd9Sstevel@tonic-gate 	rmlock(SQLOCK);
1337c478bd9Sstevel@tonic-gate }
134