home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / contrib / zelk / src-zlib / mktemp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-17  |  3.3 KB  |  181 lines

  1. /* mktemp.c zilla - substitute mktemp. process- and MACHINE-unique filenames.
  2.  * i.e. processes N on two different machines will create different names.
  3.  * Used by cgmm
  4.  * modified
  5.  * 26sep
  6.  * 30apr        logic change in mktemp
  7.  * 18feb        major rewrite
  8.  * aug          port to unix
  9.  */
  10.  
  11. #include <theusual.h>
  12. #include <assert.h>
  13. #include <constants.h>
  14.  
  15. /* use our own local random number generator:
  16.  * 1. dont depend on having ZS available for linking
  17.  * 2. dont want to risk altering the seed of the ZS generator
  18.  *    as a bug/side effect.  - this could mess up other code!
  19.  */
  20.  
  21. static int4 my_rndseed = 31415927;
  22.  
  23. #   define rnd_a  16807
  24. #   define rnd_m  2147483647
  25. #   define rnd_q  127773        /* m div a */
  26. #   define rnd_r  2836        /* m mod a */
  27.  
  28. static int4 myrnd()
  29. {
  30.     register int4 lo,hi,test;
  31.  
  32.     hi = my_rndseed / rnd_q;
  33.     lo = my_rndseed % rnd_q;
  34.     test = rnd_a * lo - rnd_r * hi;
  35.     if (test > 0) 
  36.     my_rndseed = test;
  37.     else
  38.     my_rndseed = test + rnd_m;
  39.  
  40.     return(my_rndseed);
  41. } /*myrnd*/
  42.  
  43.  
  44. #if Emac
  45. /* caller should pass a string with a string of XXXXXXXXXXXs in it.
  46.  * fills in XXXXXX with random characters
  47.  */
  48. char *
  49. Zmktemp(as)
  50.   char *as;
  51. {
  52.   char tmpbuf[CMAXPATH];
  53.   register char *s,*t;
  54.   long savseed;
  55.   
  56.   s = as;
  57.   while(*s && *s != 'X') s++;
  58.   
  59.   if (*s == 'X') {
  60.     my_rndseed = Zcurtime();
  61.     
  62.     while(*s == 'X') {
  63.       *s++ = (char)((int)'A' + (myrnd()%26));
  64.     }    
  65.   }
  66.   else Zquit(1,"Zmktemp bad arg\n");
  67.   
  68.   return(as);
  69. }
  70. #endif /*mac*/
  71.  
  72.  
  73. #if Eunix
  74. /* caller should pass a string with a LONG string of XXXXXXXXXXXs in it.
  75.  * fills in XXXXXX with 1. pid, 2. gethostid, 3. random characters
  76.  * Callers argument string is NOT modified!
  77.  */
  78.  
  79. char *
  80. Zmktemp(template)
  81.   char *template;
  82. {
  83.   char tmpbuf[CMAXPATH];
  84.   static char sbuf[CMAXPATH];
  85.   register char *s,*t,*u;
  86.   
  87.   t = template;
  88.   s = sbuf;
  89.   while(*t != 'X') *s++ = *t++;
  90.   
  91.   sprintf(tmpbuf,"%d.%d.",getpid(),gethostid());
  92.   u = tmpbuf;
  93.   while((*t == 'X') && (*u != (char)0)) {
  94.     *s = *u;
  95.     s++; t++; u++;
  96.   }
  97.   
  98.   if (*t == 'X') {
  99.     my_rndseed = Zcurtime();
  100.     
  101.     while(*t == 'X') {
  102.       *s++ = (char)((int)'A' + (myrnd()%26));
  103.       t++;
  104.     }    
  105.   }
  106.   
  107.   Ztrace(("Zmktemp %s\n",sbuf));
  108.   return(sbuf);
  109. }
  110. #endif /*unix*/
  111.  
  112.  
  113. /* return a string which is unique to the host,
  114.  * and hopefully across the net.
  115.  * The string will even usually be unique within a given prefix,
  116.  * but this is not guaranteed.
  117.  */
  118.  
  119. #if Eunix
  120. char *
  121. Zuniqnam(prefix)
  122.      char *prefix;
  123. {
  124.   static char tmpbuf[CMAXPATH];
  125.   char tmpbuf2[CMAXPATH];
  126.   register char *s;
  127.   int i;
  128.   
  129.   str_cpy(tmpbuf,prefix);
  130.   sprintf(tmpbuf2,"%d%d",getpid(),gethostid());
  131.   str_cat(tmpbuf,tmpbuf2);
  132.   
  133.   /* goto end of string */
  134.   s = tmpbuf;    while(*s) s++;
  135.   
  136.   /* add 4 random characters too! */
  137.   my_rndseed = Zcurtime();
  138.   
  139.   for( i=0; i < 4; i++ ) {
  140.     *s++ = (char)((int)'A' + (myrnd()%26));
  141.   }
  142.   
  143.   assert((s-tmpbuf) < CMAXPATH);
  144.   *s = (char)0;
  145.   
  146.   return(tmpbuf);
  147. }/*uniqnam*/
  148. #endif
  149.  
  150.  
  151. #if Emac
  152. char *
  153. Zuniqnam(prefix)
  154.      char *prefix;
  155. {
  156.   static char tmpbuf[CMAXPATH];
  157.   register char *s;
  158.   int i;
  159.   
  160.   str_cpy(tmpbuf,prefix);
  161.   
  162.   /* goto end of string */
  163.   s = tmpbuf;    while(*s) s++;
  164.   
  165.   /* add 4 random characters too! */
  166.   my_rndseed = Zcurtime();
  167.   
  168.   for( i=0; i < 6; i++ ) {
  169.     *s++ = (char)((int)'A' + (myrnd()%26));
  170.   }
  171.   
  172.   assert((s-tmpbuf) < CMAXPATH);
  173.   *s = (char)0;
  174.   
  175.   return(tmpbuf);
  176. }/*uniqnam*/
  177. #endif /*mac*/
  178.  
  179.  
  180.  
  181.