xref: /csrg-svn/usr.bin/uucp/libuu/gename.c (revision 13645)
1*13645Ssam #ifndef lint
2*13645Ssam static char sccsid[] = "@(#)gename.c	5.1 (Berkeley) 07/02/83";
3*13645Ssam #endif
4*13645Ssam 
5*13645Ssam #include "uucp.h"
6*13645Ssam 
7*13645Ssam #define SEQLEN 4
8*13645Ssam 
9*13645Ssam /*******
10*13645Ssam  *	gename(pre, sys, grade, file)	generate file name
11*13645Ssam  *	char grade, *sys, pre, *file;
12*13645Ssam  *
13*13645Ssam  *	return codes:  none
14*13645Ssam  */
15*13645Ssam 
16*13645Ssam gename(pre, sys, grade, file)
17*13645Ssam char pre, *sys, grade, *file;
18*13645Ssam {
19*13645Ssam 	static char sqnum[5];
20*13645Ssam 
21*13645Ssam 	getseq(sqnum);
22*13645Ssam 	sprintf(file, "%c.%.7s%c%.*s", pre, sys, grade, SEQLEN, sqnum);
23*13645Ssam 	DEBUG(4, "file - %s\n", file);
24*13645Ssam 	return;
25*13645Ssam }
26*13645Ssam 
27*13645Ssam 
28*13645Ssam #define SLOCKTIME 10L
29*13645Ssam #define SLOCKTRIES 5
30*13645Ssam 
31*13645Ssam /*******
32*13645Ssam  *	getseq(snum)	get next sequence number
33*13645Ssam  *	char *snum;
34*13645Ssam  *
35*13645Ssam  *	return codes:  none
36*13645Ssam  */
37*13645Ssam 
38*13645Ssam static
39*13645Ssam getseq(snum)
40*13645Ssam register char *snum;
41*13645Ssam {
42*13645Ssam 	/*
43*13645Ssam 	 * the alphabet can be anything, but if it's not in ascii order,
44*13645Ssam 	 * sequence ordering is not preserved
45*13645Ssam 	 */
46*13645Ssam 	char	*alphabet =
47*13645Ssam 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
48*13645Ssam 	register int i, fd;
49*13645Ssam 	static char *lastchar;
50*13645Ssam 
51*13645Ssam 	if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') {
52*13645Ssam 		for (i = 0; i < SLOCKTRIES; i++) {
53*13645Ssam 			if (!ulockf(SEQLOCK, SLOCKTIME))
54*13645Ssam 				break;
55*13645Ssam 			sleep(5);
56*13645Ssam 		}
57*13645Ssam 
58*13645Ssam 		ASSERT(i < SLOCKTRIES, "CAN NOT GET %s", "", SEQLOCK);
59*13645Ssam 
60*13645Ssam 		if ((fd = open(SEQFILE, 2)) >= 0) {
61*13645Ssam 			int alphalen;
62*13645Ssam 			register char	*p;
63*13645Ssam 			char *index();
64*13645Ssam 
65*13645Ssam 			alphalen = strlen(alphabet);
66*13645Ssam 			read(fd, snum, SEQLEN);
67*13645Ssam 			/* increment the penultimate character */
68*13645Ssam 			for (i = SEQLEN - 2; i >= 0; --i) {
69*13645Ssam 				if ((p = index(alphabet, snum[i])) == NULL) {
70*13645Ssam 					/* drastic but effective */
71*13645Ssam 					snum[i] = alphabet[alphalen - 1];
72*13645Ssam 					DEBUG(6, "bad seqf: %s\n", snum);
73*13645Ssam 				}
74*13645Ssam 				if (++p < &alphabet[alphalen]) {
75*13645Ssam 					snum[i] = *p;
76*13645Ssam 					break;
77*13645Ssam 				} else		/* carry */
78*13645Ssam 					snum[i] = alphabet[0];	/* continue */
79*13645Ssam 			}
80*13645Ssam 			snum[SEQLEN-1] = alphabet[0];
81*13645Ssam 		} else {
82*13645Ssam 			if ((fd = creat(SEQFILE, 0666)) < 0)
83*13645Ssam 				return(FAIL);
84*13645Ssam 			for (i = 0; i < SEQLEN; i++)
85*13645Ssam 				snum[i] = alphabet[0];
86*13645Ssam 		}
87*13645Ssam 
88*13645Ssam 		lseek(fd, 0, 0);
89*13645Ssam 		write(fd, snum, SEQLEN);
90*13645Ssam 		close(fd);
91*13645Ssam 		rmlock(SEQLOCK);
92*13645Ssam 		lastchar = alphabet + 1;
93*13645Ssam 	}
94*13645Ssam 	return(0);
95*13645Ssam }
96