|
|
1.1 root 1:
2: /* /sccs/src/cmd/uucp/s.gename.c
3: gename.c 1.1 8/30/84 17:37:19
4: */
5: #include "uucp.h"
6: VERSION(@(#) c gename.c 1.1);
7:
8: static struct {
9: char sys[NAMESIZE];
10: int job;
11: int subjob;
12: } syslst[30]; /* no more than 30 systems per job */
13:
14: static int nsys = 0;
15:
16: /* generate file name
17: * pre -> file prefix
18: * sys -> system name
19: * grade -> service grade
20: * file -> buffer to return filename must be of size DIRSIZ+1
21: * return:
22: * none
23: */
24: gename(pre, sys, grade, file)
25: char pre, *sys, grade, *file;
26: {
27: int n;
28:
29: DEBUG(9, "gename(%c, ", pre);
30: DEBUG(9, "%s, ", sys);
31: DEBUG(9, "%c)\n", grade);
32: if (*sys == '\0') {
33: sys = Myname;
34: DEBUG(9, "null sys -> %s\n", sys);
35: }
36: n = sysseq(sys);
37: if (pre == CMDPRE || pre == XQTPRE) {
38: (void) sprintf(file, "%c.%.*s%c%.4x",
39: pre, SYSNSIZE, sys, grade, syslst[n].job);
40: } else
41: (void) sprintf(file, "%c.%.5s%.4x%.3x",
42: pre, sys, syslst[n].job & 0xffff,
43: ++syslst[n].subjob & 0xfff);
44: DEBUG(4, "file - %s\n", file);
45: return;
46: }
47:
48:
49: #define SLOCKTIME 10
50: #define SLOCKTRIES 5
51: #define SEQLEN 4
52:
53: /*
54: * get next sequence number
55: * returns:
56: * number between 1 and 0xffff
57: *
58: * sequence number 0 is reserved for polling
59: */
60: static int
61: getseq(sys)
62: char *sys;
63: {
64: register FILE *fp;
65: register int i;
66: int n;
67: int seed;
68: char seqlock[MAXFULLNAME], seqfile[MAXFULLNAME];
69:
70: ASSERT(nsys < sizeof (syslst)/ sizeof (syslst[0]),
71: "SYSLST OVERFLOW", "", sizeof (syslst));
72:
73: (void) time(&seed); /* crank up the sequence initializer */
74: srand(seed);
75:
76: (void) sprintf(seqlock, "%s%s", SEQLOCK, sys);
77: BASENAME(seqlock, '/')[MAXBASENAME] = '\0';
78: for (i = 1; i < SLOCKTRIES; i++) {
79: if (!ulockf(seqlock, (time_t) SLOCKTIME))
80: break;
81: sleep(5);
82: }
83:
84: ASSERT(i < SLOCKTRIES, Ct_LOCK, seqlock, 0);
85:
86: (void) sprintf(seqfile, "%s/%s", SEQDIR, sys);
87: if ((fp = fopen(seqfile, "r")) != NULL) {
88: /* read sequence number file */
89: if (fscanf(fp, "%4x", &n) != 1) {
90: n = rand();
91: clearerr(fp);
92: }
93: fp = freopen(seqfile, "w", fp);
94: ASSERT(fp != NULL, Ct_OPEN, seqfile, errno);
95: (void) chmod(seqfile, 0666);
96: } else {
97: /* can not read file - create a new one */
98: ASSERT((fp = fopen(seqfile, "w")) != NULL,
99: Ct_CREATE, seqfile, errno);
100: (void) chmod(seqfile, 0666);
101: n = rand();
102: }
103:
104: n++;
105: n &= 0xffff; /* 4 byte sequence numbers */
106: (void) fprintf(fp, "%.4x\n", n);
107: ASSERT(ferror(fp) == 0, Ct_WRITE, seqfile, errno);
108: (void) fclose(fp);
109: ASSERT(ferror(fp) == 0, Ct_CLOSE, seqfile, errno);
110: rmlock(seqlock);
111: DEBUG(6, "%s seq ", sys); DEBUG(6, "now %x\n", n);
112: (void) strcpy(syslst[nsys].sys, sys);
113: syslst[nsys].job = n;
114: syslst[nsys].subjob = rand() & 0xfff; /* random initial value */
115: return(nsys++);
116: }
117:
118: static int
119: sysseq(sys)
120: char *sys;
121: {
122: int i;
123:
124: for (i = 0; i < nsys; i++)
125: if (strncmp(syslst[i].sys, sys, SYSNSIZE) == SAME)
126: return(i);
127:
128: return(getseq(sys));
129: }
130:
131: /*
132: * initSeq() exists because it is sometimes important to forget any
133: * cached work files. for example, when processing a bunch of spooled X.
134: * files, we must not re-use any C. files used to send back output.
135: */
136: void
137: initSeq()
138: {
139: nsys = 0;
140: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.