xref: /titanic_52/usr/src/cmd/bnu/gnxseq.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:gnxseq.c 2.5 */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "uucp.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * get next conversation sequence number
32*7c478bd9Sstevel@tonic-gate  *	rmtname	-> name of remote system
33*7c478bd9Sstevel@tonic-gate  * returns:
34*7c478bd9Sstevel@tonic-gate  *	0	-> no entery
35*7c478bd9Sstevel@tonic-gate  *	1	-> 0 sequence number
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate int
38*7c478bd9Sstevel@tonic-gate gnxseq(rmtname)
39*7c478bd9Sstevel@tonic-gate char *rmtname;
40*7c478bd9Sstevel@tonic-gate {
41*7c478bd9Sstevel@tonic-gate 	register FILE *fp0, *fp1;
42*7c478bd9Sstevel@tonic-gate 	register struct tm *tp;
43*7c478bd9Sstevel@tonic-gate 	int count = 0, ct, ret;
44*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ], name[NAMESIZE];
45*7c478bd9Sstevel@tonic-gate 	time_t clock;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	if (access(SQFILE, 0) != 0)
48*7c478bd9Sstevel@tonic-gate 		return(0);
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	{
51*7c478bd9Sstevel@tonic-gate 		register int i;
52*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; i++)
53*7c478bd9Sstevel@tonic-gate 		if ( (ret = mklock(SQLOCK)) == SUCCESS )
54*7c478bd9Sstevel@tonic-gate 			break;
55*7c478bd9Sstevel@tonic-gate 		sleep(5);
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 	if (ret != SUCCESS) {
58*7c478bd9Sstevel@tonic-gate 		logent("CAN'T LOCK", SQLOCK);
59*7c478bd9Sstevel@tonic-gate 		DEBUG(4, "can't lock %s\n", SQLOCK);
60*7c478bd9Sstevel@tonic-gate 		return(0);
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 	if ((fp0 = fopen(SQFILE, "r")) == NULL)
63*7c478bd9Sstevel@tonic-gate 		return(0);
64*7c478bd9Sstevel@tonic-gate 	if ((fp1 = fopen(SQTMP, "w")) == NULL) {
65*7c478bd9Sstevel@tonic-gate 		fclose(fp0);
66*7c478bd9Sstevel@tonic-gate 		return(0);
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 	chmod(SQTMP, DFILEMODE);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp0) != NULL) {
71*7c478bd9Sstevel@tonic-gate 		ret = sscanf(buf, "%s%d", name, &ct);
72*7c478bd9Sstevel@tonic-gate 		if (ret < 2)
73*7c478bd9Sstevel@tonic-gate 			ct = 0;
74*7c478bd9Sstevel@tonic-gate 		name[7] = '\0';
75*7c478bd9Sstevel@tonic-gate 		if (ct > 9998)
76*7c478bd9Sstevel@tonic-gate 			ct = 0;
77*7c478bd9Sstevel@tonic-gate 		if (strncmp(rmtname, name, SYSNSIZE) != SAME) {
78*7c478bd9Sstevel@tonic-gate 			fputs(buf, fp1);
79*7c478bd9Sstevel@tonic-gate 			continue;
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		/*
83*7c478bd9Sstevel@tonic-gate 		 * found name
84*7c478bd9Sstevel@tonic-gate 		 */
85*7c478bd9Sstevel@tonic-gate 		count = ++ct;
86*7c478bd9Sstevel@tonic-gate 		time(&clock);
87*7c478bd9Sstevel@tonic-gate 		tp = localtime(&clock);
88*7c478bd9Sstevel@tonic-gate 		fprintf(fp1, "%s %d %d/%d-%d:%2.2d\n", name, ct,
89*7c478bd9Sstevel@tonic-gate 		tp->tm_mon + 1, tp->tm_mday, tp->tm_hour,
90*7c478bd9Sstevel@tonic-gate 		tp->tm_min);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		/*
93*7c478bd9Sstevel@tonic-gate 		 * write should be checked
94*7c478bd9Sstevel@tonic-gate 		 */
95*7c478bd9Sstevel@tonic-gate 		while (fgets(buf, BUFSIZ, fp0) != NULL)
96*7c478bd9Sstevel@tonic-gate 			fputs(buf, fp1);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	fclose(fp0);
99*7c478bd9Sstevel@tonic-gate 	fclose(fp1);
100*7c478bd9Sstevel@tonic-gate 	if (count == 0) {
101*7c478bd9Sstevel@tonic-gate 		rmlock(SQLOCK);
102*7c478bd9Sstevel@tonic-gate 		unlink(SQTMP);
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 	return(count);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate  * commit sequence update
109*7c478bd9Sstevel@tonic-gate  * returns:
110*7c478bd9Sstevel@tonic-gate  *	0	-> ok
111*7c478bd9Sstevel@tonic-gate  *	other	-> link failed
112*7c478bd9Sstevel@tonic-gate  */
113*7c478bd9Sstevel@tonic-gate int
114*7c478bd9Sstevel@tonic-gate cmtseq()
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	register int ret;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if ((ret = access(SQTMP, 0)) != 0) {
119*7c478bd9Sstevel@tonic-gate 		rmlock(SQLOCK);
120*7c478bd9Sstevel@tonic-gate 		return(0);
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	unlink(SQFILE);
123*7c478bd9Sstevel@tonic-gate 	ret = link(SQTMP, SQFILE);
124*7c478bd9Sstevel@tonic-gate 	unlink(SQTMP);
125*7c478bd9Sstevel@tonic-gate 	rmlock(SQLOCK);
126*7c478bd9Sstevel@tonic-gate 	return(ret);
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate  * unlock sequence file
131*7c478bd9Sstevel@tonic-gate  */
132*7c478bd9Sstevel@tonic-gate void
133*7c478bd9Sstevel@tonic-gate ulkseq()
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	unlink(SQTMP);
136*7c478bd9Sstevel@tonic-gate 	rmlock(SQLOCK);
137*7c478bd9Sstevel@tonic-gate 	return;
138*7c478bd9Sstevel@tonic-gate }
139