add Berkeley specific copyrights
[unix-history] / usr / src / usr.bin / uucp / libuu / systat.c
CommitLineData
2b09f0cf 1#ifndef lint
38b40595 2static char sccsid[] = "@(#)systat.c 5.5 (Berkeley) %G%";
2b09f0cf
SL
3#endif
4
5#include "uucp.h"
2b09f0cf 6
a075b7ef 7#define STATNAME(f, n) sprintf(f, "%s/%s/%s", Spool, "STST", n)
2b09f0cf
SL
8#define S_SIZE 100
9
a075b7ef
JB
10/*LINTLIBRARY*/
11
1a85e9d2
RC
12/*
13 * make system status entry
2b09f0cf
SL
14 * return codes: none
15 */
2b09f0cf
SL
16systat(name, type, text)
17char *name, *text;
18int type;
19{
20 char filename[MAXFULLNAME], line[S_SIZE];
46b15d8a 21 int count, oldtype;
2b09f0cf 22 register FILE *fp;
46b15d8a 23 time_t prestime, rtry;
2b09f0cf
SL
24
25 if (type == 0)
26 return;
27 line[0] = '\0';
28 time(&prestime);
29 count = 0;
30 STATNAME(filename, name);
31
32 fp = fopen(filename, "r");
33 if (fp != NULL) {
34 fgets(line, S_SIZE, fp);
46b15d8a 35 sscanf(line, "%d %d", &oldtype, &count);
2b09f0cf
SL
36 if (count <= 0)
37 count = 0;
38 fclose(fp);
46b15d8a
RC
39 /* If merely 'wrong time', don't change existing STST */
40 if (type == SS_WRONGTIME && oldtype != SS_INPROGRESS)
41 return;
2b09f0cf
SL
42 }
43
46b15d8a
RC
44 rtry = Retrytime;
45 /* if failures repeat, don't try so often,
46 * to forstall a 'MAX RECALLS' situation.
47 */
48 if (type == SS_FAIL) {
2b09f0cf 49 count++;
46b15d8a
RC
50 if (count > 5) {
51 rtry = rtry * (count-5);
52 if (rtry > ONEDAY/2)
53 rtry = ONEDAY/2;
54 }
55 }
56
2b09f0cf 57
46b15d8a
RC
58#ifdef VMS
59 unlink(filename);
60#endif VMS
2b09f0cf 61 fp = fopen(filename, "w");
38b40595
RA
62 if (fp == NULL) {
63 syslog(LOG_ERR, "fopen(%s) failed: %m", filename);
64 cleanup(FAIL);
65 }
46b15d8a 66 fprintf(fp, "%d %d %ld %ld %s %s\n", type, count, prestime, rtry, text, name);
2b09f0cf 67 fclose(fp);
2b09f0cf
SL
68}
69
38b40595
RA
70/*
71 * remove system status entry
2b09f0cf
SL
72 *
73 * return codes: none
74 */
2b09f0cf
SL
75rmstat(name)
76char *name;
77{
78 char filename[MAXFULLNAME];
79
80 STATNAME(filename, name);
81 unlink(filename);
82}
83
a075b7ef
JB
84/*
85 * check system status for call
2b09f0cf
SL
86 *
87 * return codes 0 - ok | >0 system status
88 */
2b09f0cf
SL
89callok(name)
90char *name;
91{
92 char filename[MAXFULLNAME], line[S_SIZE];
93 register FILE *fp;
46b15d8a
RC
94 time_t lasttime, prestime, retrytime;
95 long t1, t2;
2b09f0cf
SL
96 int count, type;
97
98 STATNAME(filename, name);
99 fp = fopen(filename, "r");
100 if (fp == NULL)
101 return(SS_OK);
102
103 if (fgets(line, S_SIZE, fp) == NULL) {
104 /* no data */
105 fclose(fp);
106 unlink(filename);
107 return(SS_OK);
108 }
109
110 fclose(fp);
111 time(&prestime);
46b15d8a
RC
112 sscanf(line, "%d%d%ld%ld", &type, &count, &t1, &t2);
113 lasttime = t1;
114 retrytime = t2;
2b09f0cf
SL
115
116 switch(type) {
117 case SS_BADSEQ:
118 case SS_CALLBACK:
119 case SS_NODEVICE:
120 case SS_INPROGRESS: /*let LCK take care of it */
121 return(SS_OK);
122
123 case SS_FAIL:
124 if (count > MAXRECALLS) {
125 logent("MAX RECALLS", "NO CALL");
126 DEBUG(4, "MAX RECALL COUNT %d\n", count);
46b15d8a
RC
127 if (Debug) {
128 logent("debugging", "continuing anyway");
129 return SS_OK;
130 }
131 return type;
2b09f0cf
SL
132 }
133
134 if (prestime - lasttime < retrytime) {
135 logent("RETRY TIME NOT REACHED", "NO CALL");
46b15d8a
RC
136 DEBUG(4, "RETRY TIME (%ld) NOT REACHED\n", retrytime);
137 if (Debug) {
138 logent("debugging", "continuing anyway");
139 return SS_OK;
140 }
141 return type;
2b09f0cf
SL
142 }
143
46b15d8a 144 return SS_OK;
2b09f0cf 145 default:
46b15d8a 146 return SS_OK;
2b09f0cf
SL
147 }
148}