xref: /csrg-svn/usr.bin/uucp/libuu/gename.c (revision 23600)
113645Ssam #ifndef lint
2*23600Sbloom static char sccsid[] = "@(#)gename.c	5.5 (Berkeley) 06/19/85";
313645Ssam #endif
413645Ssam 
513645Ssam #include "uucp.h"
613645Ssam 
713645Ssam #define SEQLEN 4
813645Ssam 
9*23600Sbloom /*LINTLIBRARY*/
10*23600Sbloom 
11*23600Sbloom /*
12*23600Sbloom  *	generate file name
1313645Ssam  */
1413645Ssam gename(pre, sys, grade, file)
1513645Ssam char pre, *sys, grade, *file;
1613645Ssam {
1717835Sralph 	static char sqnum[5];
1813645Ssam 
1913645Ssam 	getseq(sqnum);
20*23600Sbloom 	sprintf(file,"%c.%.*s%c%.*s", pre, SYSNSIZE, sys, grade, SEQLEN, sqnum);
2113645Ssam 	DEBUG(4, "file - %s\n", file);
2213645Ssam }
2313645Ssam 
2413645Ssam #define SLOCKTIME 10L
2513645Ssam #define SLOCKTRIES 5
2613645Ssam 
27*23600Sbloom /*
28*23600Sbloom  *	get next sequence number
2913645Ssam  */
3013645Ssam 
3113645Ssam static
3213645Ssam getseq(snum)
3313645Ssam register char *snum;
3413645Ssam {
3513645Ssam 	/*
3613645Ssam 	 * the alphabet can be anything, but if it's not in ascii order,
3713645Ssam 	 * sequence ordering is not preserved
3813645Ssam 	 */
3913645Ssam 	char	*alphabet =
4013645Ssam 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
4113645Ssam 	register int i, fd;
42*23600Sbloom 	static char *lastchar = NULL;
4313645Ssam 
4413645Ssam 	if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') {
4513645Ssam 		for (i = 0; i < SLOCKTRIES; i++) {
46*23600Sbloom 			if (!ulockf(SEQLOCK, SLOCKTIME))
4713645Ssam 				break;
4813645Ssam 			sleep(5);
4913645Ssam 		}
5013645Ssam 
51*23600Sbloom 		if (i >= SLOCKTRIES) {
52*23600Sbloom 			int alphalen;
53*23600Sbloom 			logent(SEQLOCK, "CAN NOT LOCK");
54*23600Sbloom 			alphalen = strlen(alphabet);
55*23600Sbloom 			srand((int)time((time_t *)0));
56*23600Sbloom 			for (i=1;i<SEQLEN;i++)
57*23600Sbloom 				*snum++ = alphabet[rand() % alphalen];
58*23600Sbloom 			lastchar = alphabet;
59*23600Sbloom 			*snum = *lastchar++;
60*23600Sbloom 			return;
61*23600Sbloom 		}
6213645Ssam 
6313645Ssam 		if ((fd = open(SEQFILE, 2)) >= 0) {
6413645Ssam 			int alphalen;
6513645Ssam 			register char	*p;
6613645Ssam 			char *index();
6713645Ssam 
6813645Ssam 			alphalen = strlen(alphabet);
6913645Ssam 			read(fd, snum, SEQLEN);
7017835Sralph 			/* initialize rand() for possible use */
7117835Sralph 			srand((int)time((time_t *)0));
7213645Ssam 			/* increment the penultimate character */
7313645Ssam 			for (i = SEQLEN - 2; i >= 0; --i) {
7413645Ssam 				if ((p = index(alphabet, snum[i])) == NULL) {
7517835Sralph 					p = &alphabet[rand() % alphalen];
7613645Ssam 					DEBUG(6, "bad seqf: %s\n", snum);
7713645Ssam 				}
7813645Ssam 				if (++p < &alphabet[alphalen]) {
7913645Ssam 					snum[i] = *p;
8013645Ssam 					break;
8113645Ssam 				} else		/* carry */
8213645Ssam 					snum[i] = alphabet[0];	/* continue */
8313645Ssam 			}
8413645Ssam 			snum[SEQLEN-1] = alphabet[0];
8513645Ssam 		} else {
8613645Ssam 			for (i = 0; i < SEQLEN; i++)
8713645Ssam 				snum[i] = alphabet[0];
88*23600Sbloom 			if ((fd = creat(SEQFILE, 0666)) < 0)
89*23600Sbloom 				return;
9013645Ssam 		}
9113645Ssam 
9213991Sgray 		lseek(fd, 0L, 0);
9313645Ssam 		write(fd, snum, SEQLEN);
9413645Ssam 		close(fd);
9513645Ssam 		rmlock(SEQLOCK);
9613645Ssam 		lastchar = alphabet + 1;
9713645Ssam 	}
98*23600Sbloom 	return;
9913645Ssam }
100