xref: /csrg-svn/usr.bin/uucp/libuu/gename.c (revision 17835)
113645Ssam #ifndef lint
2*17835Sralph static char sccsid[] = "@(#)gename.c	5.4 (Berkeley) 01/22/85";
313645Ssam #endif
413645Ssam 
513645Ssam #include "uucp.h"
613991Sgray #include <sys/types.h>
713645Ssam 
813645Ssam #define SEQLEN 4
913645Ssam 
1013645Ssam /*******
1113645Ssam  *	gename(pre, sys, grade, file)	generate file name
1213645Ssam  *	char grade, *sys, pre, *file;
1313645Ssam  *
1413645Ssam  *	return codes:  none
1513645Ssam  */
1613645Ssam gename(pre, sys, grade, file)
1713645Ssam char pre, *sys, grade, *file;
1813645Ssam {
19*17835Sralph 	static char sqnum[5];
2013645Ssam 
2113645Ssam 	getseq(sqnum);
2213645Ssam 	sprintf(file, "%c.%.7s%c%.*s", pre, sys, grade, SEQLEN, sqnum);
2313645Ssam 	DEBUG(4, "file - %s\n", file);
2413645Ssam }
2513645Ssam 
2613645Ssam 
2713645Ssam #define SLOCKTIME 10L
2813645Ssam #define SLOCKTRIES 5
2913645Ssam 
3013645Ssam /*******
3113645Ssam  *	getseq(snum)	get next sequence number
3213645Ssam  *	char *snum;
3313645Ssam  *
3413645Ssam  *	return codes:  none
3513645Ssam  */
3613645Ssam 
3713645Ssam static
3813645Ssam getseq(snum)
3913645Ssam register char *snum;
4013645Ssam {
4113645Ssam 	/*
4213645Ssam 	 * the alphabet can be anything, but if it's not in ascii order,
4313645Ssam 	 * sequence ordering is not preserved
4413645Ssam 	 */
4513645Ssam 	char	*alphabet =
4613645Ssam 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
4713645Ssam 	register int i, fd;
4813645Ssam 	static char *lastchar;
4913645Ssam 
5013645Ssam 	if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') {
5113645Ssam 		for (i = 0; i < SLOCKTRIES; i++) {
5213991Sgray 			if (!ulockf(SEQLOCK, (time_t)SLOCKTIME))
5313645Ssam 				break;
5413645Ssam 			sleep(5);
5513645Ssam 		}
5613645Ssam 
57*17835Sralph 		ASSERT(i < SLOCKTRIES, "CAN NOT GET", SEQLOCK, 0);
5813645Ssam 
5913645Ssam 		if ((fd = open(SEQFILE, 2)) >= 0) {
6013645Ssam 			int alphalen;
6113645Ssam 			register char	*p;
6213645Ssam 			char *index();
6313645Ssam 
6413645Ssam 			alphalen = strlen(alphabet);
6513645Ssam 			read(fd, snum, SEQLEN);
66*17835Sralph 			/* initialize rand() for possible use */
67*17835Sralph 			srand((int)time((time_t *)0));
6813645Ssam 			/* increment the penultimate character */
6913645Ssam 			for (i = SEQLEN - 2; i >= 0; --i) {
7013645Ssam 				if ((p = index(alphabet, snum[i])) == NULL) {
71*17835Sralph 					p = &alphabet[rand() % alphalen];
7213645Ssam 					DEBUG(6, "bad seqf: %s\n", snum);
7313645Ssam 				}
7413645Ssam 				if (++p < &alphabet[alphalen]) {
7513645Ssam 					snum[i] = *p;
7613645Ssam 					break;
7713645Ssam 				} else		/* carry */
7813645Ssam 					snum[i] = alphabet[0];	/* continue */
7913645Ssam 			}
8013645Ssam 			snum[SEQLEN-1] = alphabet[0];
8113645Ssam 		} else {
8213645Ssam 			if ((fd = creat(SEQFILE, 0666)) < 0)
8313645Ssam 				return(FAIL);
8413645Ssam 			for (i = 0; i < SEQLEN; i++)
8513645Ssam 				snum[i] = alphabet[0];
8613645Ssam 		}
8713645Ssam 
8813991Sgray 		lseek(fd, 0L, 0);
8913645Ssam 		write(fd, snum, SEQLEN);
9013645Ssam 		close(fd);
9113645Ssam 		rmlock(SEQLOCK);
9213645Ssam 		lastchar = alphabet + 1;
9313645Ssam 	}
9413645Ssam 	return(0);
9513645Ssam }
96