1*48652Sbostic /*- 2*48652Sbostic * Copyright (c) 1985 The Regents of the University of California. 3*48652Sbostic * All rights reserved. 4*48652Sbostic * 5*48652Sbostic * %sccs.include.proprietary.c% 6*48652Sbostic */ 7*48652Sbostic 813645Ssam #ifndef lint 9*48652Sbostic static char sccsid[] = "@(#)gename.c 5.8 (Berkeley) 04/24/91"; 10*48652Sbostic #endif /* not lint */ 1113645Ssam 1213645Ssam #include "uucp.h" 1313645Ssam 1413645Ssam #define SEQLEN 4 1525131Sbloom #define SLOCKTIME 10L 1625131Sbloom #define SLOCKTRIES 5 1725131Sbloom /* 1825131Sbloom * the alphabet can be anything, but if it's not in ascii order, 1925131Sbloom * sequence ordering is not preserved 2025131Sbloom */ 2125131Sbloom static char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 2213645Ssam 2325131Sbloom #ifdef BSD4_2 2425131Sbloom #include <sys/file.h> 2525131Sbloom #endif BSD4_2 2625131Sbloom 2723600Sbloom /*LINTLIBRARY*/ 2823600Sbloom 2923600Sbloom /* 3023600Sbloom * generate file name 3113645Ssam */ 3213645Ssam gename(pre, sys, grade, file) 3313645Ssam char pre, *sys, grade, *file; 3413645Ssam { 3513645Ssam register int i, fd; 3625131Sbloom static char snum[5]; 3723600Sbloom static char *lastchar = NULL; 3813645Ssam 3913645Ssam if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') { 4025131Sbloom #ifndef BSD4_2 4113645Ssam for (i = 0; i < SLOCKTRIES; i++) { 4223600Sbloom if (!ulockf(SEQLOCK, SLOCKTIME)) 4313645Ssam break; 4413645Ssam sleep(5); 4513645Ssam } 4613645Ssam 4723600Sbloom if (i >= SLOCKTRIES) { 4823600Sbloom logent(SEQLOCK, "CAN NOT LOCK"); 4925131Sbloom goto getrandseq; 5023600Sbloom } 5125131Sbloom #endif !BSD4_2 5213645Ssam 5313645Ssam if ((fd = open(SEQFILE, 2)) >= 0) { 5425131Sbloom register char *p; 5525131Sbloom #ifdef BSD4_2 5625131Sbloom flock(fd, LOCK_EX); 5725131Sbloom #endif !BSD4_2 5813645Ssam read(fd, snum, SEQLEN); 5913645Ssam /* increment the penultimate character */ 6013645Ssam for (i = SEQLEN - 2; i >= 0; --i) { 6125131Sbloom if ((p = index(alphabet, snum[i])) == NULL) 6225131Sbloom goto getrandseq; 6325131Sbloom if (++p < &alphabet[sizeof alphabet - 1]) { 6413645Ssam snum[i] = *p; 6513645Ssam break; 6613645Ssam } else /* carry */ 6713645Ssam snum[i] = alphabet[0]; /* continue */ 6813645Ssam } 6913645Ssam snum[SEQLEN-1] = alphabet[0]; 7013645Ssam } else { 7133949Srick syslog(LOG_WARNING, "open(%s) failed: %m", SEQFILE); 7225131Sbloom fd = creat(SEQFILE, 0666); 7325131Sbloom getrandseq: srand((int)time((time_t *)0)); 7413645Ssam for (i = 0; i < SEQLEN; i++) 7525131Sbloom snum[i] = alphabet[rand() % (sizeof alphabet - 1)]; 7625131Sbloom snum[SEQLEN-1] = alphabet[0]; 7713645Ssam } 7813645Ssam 7925131Sbloom if (fd >= 0) { 8025131Sbloom lseek(fd, 0L, 0); 8125131Sbloom write(fd, snum, SEQLEN); 8225131Sbloom close(fd); 8325131Sbloom } 8425131Sbloom #ifndef BSD4_2 8513645Ssam rmlock(SEQLOCK); 8625131Sbloom #endif !BSD4_2 8713645Ssam lastchar = alphabet + 1; 8813645Ssam } 8925131Sbloom sprintf(file,"%c.%.*s%c%.*s", pre, SYSNSIZE, sys, grade, SEQLEN, snum); 9025131Sbloom DEBUG(4, "file - %s\n", file); 9113645Ssam } 92