/[cvs]/ccvs/src/main.c
ViewVC logotype

Diff of /ccvs/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.257 by dprice, Mon Sep 26 00:33:39 2005 UTC revision 1.258 by mdb, Wed Sep 28 23:59:02 2005 UTC
# Line 458  extern char *gConfigPath; Line 458  extern char *gConfigPath;
458    
459    
460    
461    
462    enum {RANDOM_BYTES = 8};
463    enum {N = (sizeof(time_t) + RANDOM_BYTES)};
464    
465    static char const alphabet[62] =
466      "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
467    
468    /* Divide BUF by D, returning the remainder.  Replace BUF by the
469       quotient.  BUF[0] is the most significant part of BUF.
470       D must not exceed UINT_MAX >> CHAR_BIT.  */
471    static unsigned int
472    divide_by (unsigned char buf[N], unsigned int d)
473    {
474        unsigned int carry = 0;
475        int i;
476        for (i = 0; i < N; i++)
477        {
478            unsigned int byte = buf[i];
479            unsigned int dividend = (carry << CHAR_BIT) + byte;
480            buf[i] = dividend / d;
481            carry = dividend % d;
482        }
483        return carry;
484    }
485    
486    static void
487    convert (char const input[N], char *output)
488    {
489        static char const zero[N] = { 0, };
490        unsigned char buf[N];
491        size_t o = 0;
492        memcpy (buf, input, N);
493        while (memcmp (buf, zero, N) != 0)
494            output[o++] = alphabet[divide_by (buf, sizeof alphabet)];
495        if (! o)
496            output[o++] = '0';
497        output[o] = '\0';
498    }
499    
500    
501  int  int
502  main (int argc, char **argv)  main (int argc, char **argv)
503  {  {
# Line 733  cause intermittent sandbox corruption.") Line 773  cause intermittent sandbox corruption.")
773    
774      /* Calculate the cvs global session ID */      /* Calculate the cvs global session ID */
775    
776      global_session_id = Xasprintf ("%x%08lx%04x", (int)getpid(),      {
777                                    (long)time (NULL), rand()&0xFFFF);          char buf[N] = { 0, };
778            char out[N*2];
779            ssize_t len = 0;
780            time_t rightnow = time (NULL);
781            unsigned char *p = (unsigned char *) (buf + sizeof (time_t));
782            int fd = open ("/dev/urandom", O_RDONLY, O_NOCTTY);
783            if (fd >= 0) {
784                len = read (fd, buf + sizeof (time_t), RANDOM_BYTES);
785                close (fd);
786            }
787            if (len > 0 && rightnow >= 0) {
788                while (rightnow != 0) {
789                    *--p = rightnow % (UCHAR_MAX + 1);
790                    rightnow /= UCHAR_MAX + 1;
791                }
792                convert(buf, out);
793                global_session_id = strdup (out);
794            } else
795                global_session_id = Xasprintf ("%x%08lx%04x", (int)getpid(),
796                                               (long)time (NULL), rand()&0xFFFF);
797        }
798    
799    
800      TRACE (TRACE_FUNCTION, "main: Session ID is %s", global_session_id);      TRACE (TRACE_FUNCTION, "main: Session ID is %s", global_session_id);
801    

Legend:
Removed from v.1.257  
changed lines
  Added in v.1.258

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26