BSD 4_4 release
[unix-history] / usr / src / usr.bin / uucp / libuu / gename.c
index 3eded76..a370e6b 100644 (file)
@@ -1,77 +1,68 @@
+/*-
+ * Copyright (c) 1985, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This module is believed to contain source code proprietary to AT&T.
+ * Use and redistribution is subject to the Berkeley Software License
+ * Agreement and your Software Agreement with AT&T (Western Electric).
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)gename.c   5.4 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)gename.c   8.1 (Berkeley) 6/6/93";
+#endif /* not lint */
 
 #include "uucp.h"
 
 #include "uucp.h"
-#include <sys/types.h>
 
 #define SEQLEN 4
 
 #define SEQLEN 4
-
-/*******
- *     gename(pre, sys, grade, file)   generate file name
- *     char grade, *sys, pre, *file;
- *
- *     return codes:  none
+#define SLOCKTIME 10L
+#define SLOCKTRIES 5
+/*
+ * the alphabet can be anything, but if it's not in ascii order,
+ * sequence ordering is not preserved
  */
  */
-gename(pre, sys, grade, file)
-char pre, *sys, grade, *file;
-{
-       static char sqnum[5];
-
-       getseq(sqnum);
-       sprintf(file, "%c.%.7s%c%.*s", pre, sys, grade, SEQLEN, sqnum);
-       DEBUG(4, "file - %s\n", file);
-}
+static char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
 
+#ifdef BSD4_2
+#include <sys/file.h>
+#endif BSD4_2
 
 
-#define SLOCKTIME 10L
-#define SLOCKTRIES 5
+/*LINTLIBRARY*/
 
 
-/*******
- *     getseq(snum)    get next sequence number
- *     char *snum;
- *
- *     return codes:  none
+/*
+ *     generate file name
  */
  */
-
-static
-getseq(snum)
-register char *snum;
+gename(pre, sys, grade, file)
+char pre, *sys, grade, *file;
 {
 {
-       /*
-        * the alphabet can be anything, but if it's not in ascii order,
-        * sequence ordering is not preserved
-        */
-       char    *alphabet =
-           "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        register int i, fd;
        register int i, fd;
-       static char *lastchar;
+       static char snum[5];
+       static char *lastchar = NULL;
 
        if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') {
 
        if (lastchar == NULL || (snum[SEQLEN-1] = *(lastchar++)) == '\0') {
+#ifndef BSD4_2
                for (i = 0; i < SLOCKTRIES; i++) {
                for (i = 0; i < SLOCKTRIES; i++) {
-                       if (!ulockf(SEQLOCK, (time_t)SLOCKTIME))
+                       if (!ulockf(SEQLOCK, SLOCKTIME))
                                break;
                        sleep(5);
                }
 
                                break;
                        sleep(5);
                }
 
-               ASSERT(i < SLOCKTRIES, "CAN NOT GET", SEQLOCK, 0);
+               if (i >= SLOCKTRIES) {
+                       logent(SEQLOCK, "CAN NOT LOCK");
+                       goto getrandseq;
+               }
+#endif !BSD4_2
 
                if ((fd = open(SEQFILE, 2)) >= 0) {
 
                if ((fd = open(SEQFILE, 2)) >= 0) {
-                       int alphalen;
-                       register char   *p;
-                       char *index();
-
-                       alphalen = strlen(alphabet);
+                       register char *p;
+#ifdef BSD4_2
+                       flock(fd, LOCK_EX);
+#endif !BSD4_2
                        read(fd, snum, SEQLEN);
                        read(fd, snum, SEQLEN);
-                       /* initialize rand() for possible use */
-                       srand((int)time((time_t *)0));
                        /* increment the penultimate character */
                        for (i = SEQLEN - 2; i >= 0; --i) {
                        /* increment the penultimate character */
                        for (i = SEQLEN - 2; i >= 0; --i) {
-                               if ((p = index(alphabet, snum[i])) == NULL) {
-                                       p = &alphabet[rand() % alphalen];
-                                       DEBUG(6, "bad seqf: %s\n", snum);
-                               }
-                               if (++p < &alphabet[alphalen]) {
+                               if ((p = index(alphabet, snum[i])) == NULL)
+                                       goto getrandseq;
+                               if (++p < &alphabet[sizeof alphabet - 1]) {
                                        snum[i] = *p;
                                        break;
                                } else          /* carry */
                                        snum[i] = *p;
                                        break;
                                } else          /* carry */
@@ -79,17 +70,24 @@ register char *snum;
                        }
                        snum[SEQLEN-1] = alphabet[0];
                } else {
                        }
                        snum[SEQLEN-1] = alphabet[0];
                } else {
-                       if ((fd = creat(SEQFILE, 0666)) < 0)
-                               return(FAIL);
+                       syslog(LOG_WARNING, "open(%s) failed: %m", SEQFILE);
+                       fd = creat(SEQFILE, 0666);
+getrandseq:            srand((int)time((time_t *)0));
                        for (i = 0; i < SEQLEN; i++)
                        for (i = 0; i < SEQLEN; i++)
-                               snum[i] = alphabet[0];
+                               snum[i] = alphabet[rand() % (sizeof alphabet - 1)];
+                       snum[SEQLEN-1] = alphabet[0];
                }
 
                }
 
-               lseek(fd, 0L, 0);
-               write(fd, snum, SEQLEN);
-               close(fd);
+               if (fd >= 0) {
+                       lseek(fd, 0L, 0);
+                       write(fd, snum, SEQLEN);
+                       close(fd);
+               }
+#ifndef BSD4_2
                rmlock(SEQLOCK);
                rmlock(SEQLOCK);
+#endif !BSD4_2
                lastchar = alphabet + 1;
        }
                lastchar = alphabet + 1;
        }
-       return(0);
+       sprintf(file,"%c.%.*s%c%.*s", pre, SYSNSIZE, sys, grade, SEQLEN, snum);
+       DEBUG(4, "file - %s\n", file);
 }
 }