xref: /illumos-gate/usr/src/cmd/bnu/gnxseq.c (revision 8e718be9d8b2bb15811a2dc0b61cbb5cc5fa3070)
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 #include "uucp.h"
26 
27 /*
28  * get next conversation sequence number
29  *	rmtname	-> name of remote system
30  * returns:
31  *	0	-> no entery
32  *	1	-> 0 sequence number
33  */
34 int
35 gnxseq(char *rmtname)
36 {
37 	register FILE *fp0, *fp1;
38 	register struct tm *tp;
39 	int count = 0, ct, ret;
40 	char buf[BUFSIZ], name[NAMESIZE];
41 	time_t clock;
42 
43 	if (access(SQFILE, 0) != 0)
44 		return (0);
45 
46 	{
47 		int i;
48 		for (i = 0; i < 5; i++)
49 			if ((ret = mklock(SQLOCK)) == SUCCESS)
50 				break;
51 		sleep(5);
52 	}
53 	if (ret != SUCCESS) {
54 		logent("CAN'T LOCK", SQLOCK);
55 		DEBUG(4, "can't lock %s\n", SQLOCK);
56 		return (0);
57 	}
58 	if ((fp0 = fopen(SQFILE, "r")) == NULL)
59 		return (0);
60 	if ((fp1 = fopen(SQTMP, "w")) == NULL) {
61 		fclose(fp0);
62 		return (0);
63 	}
64 	chmod(SQTMP, DFILEMODE);
65 
66 	while (fgets(buf, BUFSIZ, fp0) != NULL) {
67 		ret = sscanf(buf, "%s%d", name, &ct);
68 		if (ret < 2)
69 			ct = 0;
70 		name[7] = '\0';
71 		if (ct > 9998)
72 			ct = 0;
73 		if (strncmp(rmtname, name, SYSNSIZE) != SAME) {
74 			fputs(buf, fp1);
75 			continue;
76 		}
77 
78 		/*
79 		 * found name
80 		 */
81 		count = ++ct;
82 		time(&clock);
83 		tp = localtime(&clock);
84 		fprintf(fp1, "%s %d %d/%d-%d:%2.2d\n", name, ct,
85 		    tp->tm_mon + 1, tp->tm_mday, tp->tm_hour,
86 		    tp->tm_min);
87 
88 		/*
89 		 * write should be checked
90 		 */
91 		while (fgets(buf, BUFSIZ, fp0) != NULL)
92 			fputs(buf, fp1);
93 	}
94 	fclose(fp0);
95 	fclose(fp1);
96 	if (count == 0) {
97 		rmlock(SQLOCK);
98 		unlink(SQTMP);
99 	}
100 	return (count);
101 }
102 
103 /*
104  * commit sequence update
105  * returns:
106  *	0	-> ok
107  *	other	-> link failed
108  */
109 int
110 cmtseq(void)
111 {
112 	int ret;
113 
114 	if ((ret = access(SQTMP, 0)) != 0) {
115 		rmlock(SQLOCK);
116 		return (0);
117 	}
118 	unlink(SQFILE);
119 	ret = link(SQTMP, SQFILE);
120 	unlink(SQTMP);
121 	rmlock(SQLOCK);
122 	return (ret);
123 }
124 
125 /*
126  * unlock sequence file
127  */
128 void
129 ulkseq(void)
130 {
131 	unlink(SQTMP);
132 	rmlock(SQLOCK);
133 }
134