1 /*- 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.proprietary.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)gename.c 8.1 (Berkeley) 06/06/93"; 10 #endif /* not lint */ 11 12 #include "uucp.h" 13 14 #define SEQLEN 4 15 #define SLOCKTIME 10L 16 #define SLOCKTRIES 5 17 /* 18 * the alphabet can be anything, but if it's not in ascii order, 19 * sequence ordering is not preserved 20 */ 21 static char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 22 23 #ifdef BSD4_2 24 #include <sys/file.h> 25 #endif BSD4_2 26 27 /*LINTLIBRARY*/ 28 29 /* 30 * generate file name 31 */ 32 gename(pre, sys, grade, file) 33 char pre, *sys, grade, *file; 34 { 35 register int i, fd; 36 static char snum[5]; 37 static char *lastchar = NULL; 38 39 if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') { 40 #ifndef BSD4_2 41 for (i = 0; i < SLOCKTRIES; i++) { 42 if (!ulockf(SEQLOCK, SLOCKTIME)) 43 break; 44 sleep(5); 45 } 46 47 if (i >= SLOCKTRIES) { 48 logent(SEQLOCK, "CAN NOT LOCK"); 49 goto getrandseq; 50 } 51 #endif !BSD4_2 52 53 if ((fd = open(SEQFILE, 2)) >= 0) { 54 register char *p; 55 #ifdef BSD4_2 56 flock(fd, LOCK_EX); 57 #endif !BSD4_2 58 read(fd, snum, SEQLEN); 59 /* increment the penultimate character */ 60 for (i = SEQLEN - 2; i >= 0; --i) { 61 if ((p = index(alphabet, snum[i])) == NULL) 62 goto getrandseq; 63 if (++p < &alphabet[sizeof alphabet - 1]) { 64 snum[i] = *p; 65 break; 66 } else /* carry */ 67 snum[i] = alphabet[0]; /* continue */ 68 } 69 snum[SEQLEN-1] = alphabet[0]; 70 } else { 71 syslog(LOG_WARNING, "open(%s) failed: %m", SEQFILE); 72 fd = creat(SEQFILE, 0666); 73 getrandseq: srand((int)time((time_t *)0)); 74 for (i = 0; i < SEQLEN; i++) 75 snum[i] = alphabet[rand() % (sizeof alphabet - 1)]; 76 snum[SEQLEN-1] = alphabet[0]; 77 } 78 79 if (fd >= 0) { 80 lseek(fd, 0L, 0); 81 write(fd, snum, SEQLEN); 82 close(fd); 83 } 84 #ifndef BSD4_2 85 rmlock(SEQLOCK); 86 #endif !BSD4_2 87 lastchar = alphabet + 1; 88 } 89 sprintf(file,"%c.%.*s%c%.*s", pre, SYSNSIZE, sys, grade, SEQLEN, snum); 90 DEBUG(4, "file - %s\n", file); 91 } 92