xref: /titanic_51/usr/src/cmd/bnu/gename.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "uucp.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static struct {
36*7c478bd9Sstevel@tonic-gate 	char	sys[NAMESIZE];
37*7c478bd9Sstevel@tonic-gate 	int	job;
38*7c478bd9Sstevel@tonic-gate 	int	subjob;
39*7c478bd9Sstevel@tonic-gate } syslst[30];		/* no more than 30 systems per job */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int nsys = 0;
42*7c478bd9Sstevel@tonic-gate static int sysseq();
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate  /* generate file name
45*7c478bd9Sstevel@tonic-gate   *	pre	-> file prefix
46*7c478bd9Sstevel@tonic-gate   *	sys	-> system name
47*7c478bd9Sstevel@tonic-gate   *	grade	-> service grade
48*7c478bd9Sstevel@tonic-gate   *	file	-> buffer to return filename must be of size MAXBASENAME+1
49*7c478bd9Sstevel@tonic-gate   * return:
50*7c478bd9Sstevel@tonic-gate   *	none
51*7c478bd9Sstevel@tonic-gate   */
52*7c478bd9Sstevel@tonic-gate void
53*7c478bd9Sstevel@tonic-gate gename(pre, sys, grade, file)
54*7c478bd9Sstevel@tonic-gate char pre, *sys, grade, *file;
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int	n;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "gename(%c, ", pre);
59*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "%s, ", sys);
60*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "%c)\n", grade);
61*7c478bd9Sstevel@tonic-gate 	if (*sys == '\0') {
62*7c478bd9Sstevel@tonic-gate 		sys = Myname;
63*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "null sys -> %s\n", sys);
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 	n = sysseq(sys);
66*7c478bd9Sstevel@tonic-gate 	if (pre == CMDPRE || pre == XQTPRE) {
67*7c478bd9Sstevel@tonic-gate 		(void) sprintf(file, "%c.%.*s%c%.4x",
68*7c478bd9Sstevel@tonic-gate 			pre, SYSNSIZE, sys, grade, syslst[n].job);
69*7c478bd9Sstevel@tonic-gate 	} else
70*7c478bd9Sstevel@tonic-gate 		(void) sprintf(file, "%c.%.5s%.4x%.3x",
71*7c478bd9Sstevel@tonic-gate 			pre, sys, syslst[n].job & 0xffff,
72*7c478bd9Sstevel@tonic-gate 				++syslst[n].subjob & 0xfff);
73*7c478bd9Sstevel@tonic-gate 	DEBUG(4, "file - %s\n", file);
74*7c478bd9Sstevel@tonic-gate 	return;
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define SLOCKTIME 10
79*7c478bd9Sstevel@tonic-gate #define SLOCKTRIES 25
80*7c478bd9Sstevel@tonic-gate #define SEQLEN 4
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate  /*
83*7c478bd9Sstevel@tonic-gate   * get next sequence number
84*7c478bd9Sstevel@tonic-gate   * returns:
85*7c478bd9Sstevel@tonic-gate   *	number between 1 and 0xffff
86*7c478bd9Sstevel@tonic-gate   *
87*7c478bd9Sstevel@tonic-gate   * sequence number 0 is reserved for polling
88*7c478bd9Sstevel@tonic-gate   */
89*7c478bd9Sstevel@tonic-gate static int
90*7c478bd9Sstevel@tonic-gate getseq(sys)
91*7c478bd9Sstevel@tonic-gate char	*sys;
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	register FILE *fp;
94*7c478bd9Sstevel@tonic-gate 	register int i;
95*7c478bd9Sstevel@tonic-gate 	unsigned int n;
96*7c478bd9Sstevel@tonic-gate 	time_t	seed;
97*7c478bd9Sstevel@tonic-gate 	char seqlock[MAXFULLNAME], seqfile[MAXFULLNAME];
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	ASSERT(nsys < sizeof (syslst)/ sizeof (syslst[0]),
100*7c478bd9Sstevel@tonic-gate 	    "SYSLST OVERFLOW", "", sizeof (syslst));
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	(void) time(&seed);	/* crank up the sequence initializer */
103*7c478bd9Sstevel@tonic-gate 	srand((unsigned)seed);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	(void) sprintf(seqlock, "%s%s", SEQLOCK, sys);
106*7c478bd9Sstevel@tonic-gate 	BASENAME(seqlock, '/')[MAXBASENAME] = '\0';
107*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < SLOCKTRIES; i++) {
108*7c478bd9Sstevel@tonic-gate 		if ( mklock(seqlock) == SUCCESS )
109*7c478bd9Sstevel@tonic-gate 			break;
110*7c478bd9Sstevel@tonic-gate 		sleep(5);
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	ASSERT(i < SLOCKTRIES, Ct_LOCK, seqlock, 0);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	(void) sprintf(seqfile, "%s/%s", SEQDIR, sys);
116*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(seqfile, "r")) != NULL) {
117*7c478bd9Sstevel@tonic-gate 		/* read sequence number file */
118*7c478bd9Sstevel@tonic-gate 		if (fscanf(fp, "%4x", &n) != 1) {
119*7c478bd9Sstevel@tonic-gate 		    n = rand();
120*7c478bd9Sstevel@tonic-gate 		    clearerr(fp);
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 		fp = freopen(seqfile, "w", fp);
123*7c478bd9Sstevel@tonic-gate 		ASSERT(fp != NULL, Ct_OPEN, seqfile, errno);
124*7c478bd9Sstevel@tonic-gate 		(void) chmod(seqfile, PUB_FILEMODE);
125*7c478bd9Sstevel@tonic-gate 	} else {
126*7c478bd9Sstevel@tonic-gate 		/* can not read file - create a new one */
127*7c478bd9Sstevel@tonic-gate 		ASSERT((fp = fopen(seqfile, "w")) != NULL,
128*7c478bd9Sstevel@tonic-gate 		    Ct_CREATE, seqfile, errno);
129*7c478bd9Sstevel@tonic-gate 		(void) chmod(seqfile, PUB_FILEMODE);
130*7c478bd9Sstevel@tonic-gate 		n = rand();
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	n++;
134*7c478bd9Sstevel@tonic-gate 	n &= 0xffff;	/* 4 byte sequence numbers */
135*7c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%.4x\n", n);
136*7c478bd9Sstevel@tonic-gate 	ASSERT(ferror(fp) == 0, Ct_WRITE, seqfile, errno);
137*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
138*7c478bd9Sstevel@tonic-gate 	ASSERT(ferror(fp) == 0, Ct_CLOSE, seqfile, errno);
139*7c478bd9Sstevel@tonic-gate 	rmlock(seqlock);
140*7c478bd9Sstevel@tonic-gate 	DEBUG(6, "%s seq ", sys); DEBUG(6, "now %x\n", n);
141*7c478bd9Sstevel@tonic-gate 	(void) strcpy(syslst[nsys].sys, sys);
142*7c478bd9Sstevel@tonic-gate 	syslst[nsys].job = n;
143*7c478bd9Sstevel@tonic-gate 	syslst[nsys].subjob = rand() & 0xfff;	/* random initial value */
144*7c478bd9Sstevel@tonic-gate 	return(nsys++);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  *	initSeq() exists because it is sometimes important to forget any
149*7c478bd9Sstevel@tonic-gate  *	cached work files.  for example, when processing a bunch of spooled X.
150*7c478bd9Sstevel@tonic-gate  *	files, we must not re-use any C. files used to send back output.
151*7c478bd9Sstevel@tonic-gate  */
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate void
154*7c478bd9Sstevel@tonic-gate initSeq()
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	nsys = 0;
157*7c478bd9Sstevel@tonic-gate 	return;
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate /*
161*7c478bd9Sstevel@tonic-gate  * 	retseq() is used to get the sequence number of a job
162*7c478bd9Sstevel@tonic-gate  *	for functions outside of this file.
163*7c478bd9Sstevel@tonic-gate  *
164*7c478bd9Sstevel@tonic-gate  *	returns
165*7c478bd9Sstevel@tonic-gate  *
166*7c478bd9Sstevel@tonic-gate  *		the sequence number of the job for the system.
167*7c478bd9Sstevel@tonic-gate  */
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate int
170*7c478bd9Sstevel@tonic-gate retseq(sys)
171*7c478bd9Sstevel@tonic-gate char *sys;
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	int i;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys; i++)
176*7c478bd9Sstevel@tonic-gate 		if (EQUALSN(syslst[i].sys, sys, MAXBASENAME))
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	return(syslst[i].job);
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static int
183*7c478bd9Sstevel@tonic-gate sysseq(sys)
184*7c478bd9Sstevel@tonic-gate char	*sys;
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate 	int	i;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsys; i++)
189*7c478bd9Sstevel@tonic-gate 		if (strncmp(syslst[i].sys, sys, MAXBASENAME) == SAME)
190*7c478bd9Sstevel@tonic-gate 			return(i);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	return(getseq(sys));
193*7c478bd9Sstevel@tonic-gate }
194