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