Annotation of embedaddon/rsync/util2.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * Utility routines used in rsync.
                      3:  *
                      4:  * Copyright (C) 1996-2000 Andrew Tridgell
                      5:  * Copyright (C) 1996 Paul Mackerras
                      6:  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
1.1.1.2 ! misho       7:  * Copyright (C) 2003-2015 Wayne Davison
1.1       misho       8:  *
                      9:  * This program is free software; you can redistribute it and/or modify
                     10:  * it under the terms of the GNU General Public License as published by
                     11:  * the Free Software Foundation; either version 3 of the License, or
                     12:  * (at your option) any later version.
                     13:  *
                     14:  * This program is distributed in the hope that it will be useful,
                     15:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17:  * GNU General Public License for more details.
                     18:  *
                     19:  * You should have received a copy of the GNU General Public License along
                     20:  * with this program; if not, visit the http://fsf.org website.
                     21:  */
                     22: 
                     23: #include "rsync.h"
                     24: #include "ifuncs.h"
                     25: #include "itypes.h"
                     26: #include "inums.h"
                     27: 
                     28: extern int checksum_len;
                     29: 
                     30: /**
                     31:  * Sleep for a specified number of milliseconds.
                     32:  *
                     33:  * Always returns TRUE.  (In the future it might return FALSE if
                     34:  * interrupted.)
                     35:  **/
                     36: int msleep(int t)
                     37: {
1.1.1.2 ! misho      38: #ifdef HAVE_USLEEP
        !            39:        usleep(t*1000);
        !            40: #else
1.1       misho      41:        int tdiff = 0;
                     42:        struct timeval tval, t1, t2;
                     43: 
                     44:        gettimeofday(&t1, NULL);
                     45: 
                     46:        while (tdiff < t) {
                     47:                tval.tv_sec = (t-tdiff)/1000;
                     48:                tval.tv_usec = 1000*((t-tdiff)%1000);
                     49: 
                     50:                errno = 0;
                     51:                select(0,NULL,NULL, NULL, &tval);
                     52: 
                     53:                gettimeofday(&t2, NULL);
                     54:                tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
                     55:                        (t2.tv_usec - t1.tv_usec)/1000;
1.1.1.2 ! misho      56:                if (tdiff < 0)
        !            57:                        t1 = t2; /* Time went backwards, so start over. */
1.1       misho      58:        }
1.1.1.2 ! misho      59: #endif
1.1       misho      60: 
                     61:        return True;
                     62: }
                     63: 
                     64: #define MALLOC_MAX 0x40000000
                     65: 
                     66: void *_new_array(unsigned long num, unsigned int size, int use_calloc)
                     67: {
                     68:        if (num >= MALLOC_MAX/size)
                     69:                return NULL;
                     70:        return use_calloc ? calloc(num, size) : malloc(num * size);
                     71: }
                     72: 
                     73: void *_realloc_array(void *ptr, unsigned int size, size_t num)
                     74: {
                     75:        if (num >= MALLOC_MAX/size)
                     76:                return NULL;
                     77:        if (!ptr)
                     78:                return malloc(size * num);
                     79:        return realloc(ptr, size * num);
                     80: }
                     81: 
                     82: const char *sum_as_hex(const char *sum)
                     83: {
                     84:        static char buf[MAX_DIGEST_LEN*2+1];
                     85:        int i, x1, x2;
                     86:        char *c = buf + checksum_len*2;
                     87: 
                     88:        assert(c - buf < (int)sizeof buf);
                     89: 
                     90:        *c = '\0';
                     91: 
                     92:        for (i = checksum_len; --i >= 0; ) {
                     93:                x1 = CVAL(sum, i);
                     94:                x2 = x1 >> 4;
                     95:                x1 &= 0xF;
                     96:                *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
                     97:                *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
                     98:        }
                     99: 
                    100:        return buf;
                    101: }
                    102: 
                    103: NORETURN void out_of_memory(const char *str)
                    104: {
                    105:        rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());
                    106:        exit_cleanup(RERR_MALLOC);
                    107: }
                    108: 
                    109: NORETURN void overflow_exit(const char *str)
                    110: {
                    111:        rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());
                    112:        exit_cleanup(RERR_MALLOC);
                    113: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>