xref: /csrg-svn/usr.bin/uucp/libuu/gename.c (revision 13991)
113645Ssam #ifndef lint
2*13991Sgray static char sccsid[] = "@(#)gename.c	5.2 (Berkeley) 07/19/83";
313645Ssam #endif
413645Ssam 
513645Ssam #include "uucp.h"
6*13991Sgray #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 
1713645Ssam gename(pre, sys, grade, file)
1813645Ssam char pre, *sys, grade, *file;
1913645Ssam {
2013645Ssam 	static char sqnum[5];
2113645Ssam 
2213645Ssam 	getseq(sqnum);
2313645Ssam 	sprintf(file, "%c.%.7s%c%.*s", pre, sys, grade, SEQLEN, sqnum);
2413645Ssam 	DEBUG(4, "file - %s\n", file);
2513645Ssam 	return;
2613645Ssam }
2713645Ssam 
2813645Ssam 
2913645Ssam #define SLOCKTIME 10L
3013645Ssam #define SLOCKTRIES 5
3113645Ssam 
3213645Ssam /*******
3313645Ssam  *	getseq(snum)	get next sequence number
3413645Ssam  *	char *snum;
3513645Ssam  *
3613645Ssam  *	return codes:  none
3713645Ssam  */
3813645Ssam 
3913645Ssam static
4013645Ssam getseq(snum)
4113645Ssam register char *snum;
4213645Ssam {
4313645Ssam 	/*
4413645Ssam 	 * the alphabet can be anything, but if it's not in ascii order,
4513645Ssam 	 * sequence ordering is not preserved
4613645Ssam 	 */
4713645Ssam 	char	*alphabet =
4813645Ssam 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
4913645Ssam 	register int i, fd;
5013645Ssam 	static char *lastchar;
5113645Ssam 
5213645Ssam 	if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') {
5313645Ssam 		for (i = 0; i < SLOCKTRIES; i++) {
54*13991Sgray 			if (!ulockf(SEQLOCK, (time_t)SLOCKTIME))
5513645Ssam 				break;
5613645Ssam 			sleep(5);
5713645Ssam 		}
5813645Ssam 
59*13991Sgray 		ASSERT(i < SLOCKTRIES, "CAN NOT GET", "SEQLOCK", 0);
6013645Ssam 
6113645Ssam 		if ((fd = open(SEQFILE, 2)) >= 0) {
6213645Ssam 			int alphalen;
6313645Ssam 			register char	*p;
6413645Ssam 			char *index();
6513645Ssam 
6613645Ssam 			alphalen = strlen(alphabet);
6713645Ssam 			read(fd, snum, SEQLEN);
6813645Ssam 			/* increment the penultimate character */
6913645Ssam 			for (i = SEQLEN - 2; i >= 0; --i) {
7013645Ssam 				if ((p = index(alphabet, snum[i])) == NULL) {
7113645Ssam 					/* drastic but effective */
7213645Ssam 					snum[i] = alphabet[alphalen - 1];
7313645Ssam 					DEBUG(6, "bad seqf: %s\n", snum);
7413645Ssam 				}
7513645Ssam 				if (++p < &alphabet[alphalen]) {
7613645Ssam 					snum[i] = *p;
7713645Ssam 					break;
7813645Ssam 				} else		/* carry */
7913645Ssam 					snum[i] = alphabet[0];	/* continue */
8013645Ssam 			}
8113645Ssam 			snum[SEQLEN-1] = alphabet[0];
8213645Ssam 		} else {
8313645Ssam 			if ((fd = creat(SEQFILE, 0666)) < 0)
8413645Ssam 				return(FAIL);
8513645Ssam 			for (i = 0; i < SEQLEN; i++)
8613645Ssam 				snum[i] = alphabet[0];
8713645Ssam 		}
8813645Ssam 
89*13991Sgray 		lseek(fd, 0L, 0);
9013645Ssam 		write(fd, snum, SEQLEN);
9113645Ssam 		close(fd);
9213645Ssam 		rmlock(SEQLOCK);
9313645Ssam 		lastchar = alphabet + 1;
9413645Ssam 	}
9513645Ssam 	return(0);
9613645Ssam }
97