Merge branch 'less_closed'
[unleashed.git] / usr / src / cmd / bnu / gnxseq.c
blobc7ab630692ff5234c0ff2d3e71fe6fe601854611
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
26 #ident "%Z%%M% %I% %E% SMI" /* from SVR4 bnu:gnxseq.c 2.5 */
28 #include "uucp.h"
31 * get next conversation sequence number
32 * rmtname -> name of remote system
33 * returns:
34 * 0 -> no entery
35 * 1 -> 0 sequence number
37 int
38 gnxseq(rmtname)
39 char *rmtname;
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;
47 if (access(SQFILE, 0) != 0)
48 return(0);
51 register int i;
52 for (i = 0; i < 5; i++)
53 if ( (ret = mklock(SQLOCK)) == SUCCESS )
54 break;
55 sleep(5);
57 if (ret != SUCCESS) {
58 logent("CAN'T LOCK", SQLOCK);
59 DEBUG(4, "can't lock %s\n", SQLOCK);
60 return(0);
62 if ((fp0 = fopen(SQFILE, "r")) == NULL)
63 return(0);
64 if ((fp1 = fopen(SQTMP, "w")) == NULL) {
65 fclose(fp0);
66 return(0);
68 chmod(SQTMP, DFILEMODE);
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;
83 * found name
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);
93 * write should be checked
95 while (fgets(buf, BUFSIZ, fp0) != NULL)
96 fputs(buf, fp1);
98 fclose(fp0);
99 fclose(fp1);
100 if (count == 0) {
101 rmlock(SQLOCK);
102 unlink(SQTMP);
104 return(count);
108 * commit sequence update
109 * returns:
110 * 0 -> ok
111 * other -> link failed
114 cmtseq()
116 register int ret;
118 if ((ret = access(SQTMP, 0)) != 0) {
119 rmlock(SQLOCK);
120 return(0);
122 unlink(SQFILE);
123 ret = link(SQTMP, SQFILE);
124 unlink(SQTMP);
125 rmlock(SQLOCK);
126 return(ret);
130 * unlock sequence file
132 void
133 ulkseq()
135 unlink(SQTMP);
136 rmlock(SQLOCK);
137 return;