Annotation of researchv10no/cmd/dist/lib/util.c, revision 1.1

1.1     ! root        1: #include <stddef.h>
        !             2: #include <stdio.h>
        !             3: #include <stdlib.h>
        !             4: #include <time.h>
        !             5: #include "lib.h"
        !             6: 
        !             7: #define MIN(A, B) ((A) < (B) ? (A) : (B))
        !             8: 
        !             9: void *
        !            10: xmalloc(size_t size)
        !            11: {
        !            12:        void *result;
        !            13: 
        !            14:        result = malloc(size);
        !            15:        if (size && !result) {
        !            16:                eprintf("malloc failure");
        !            17:                exit(1);
        !            18:        }
        !            19:        return result;
        !            20: }
        !            21: 
        !            22: void *
        !            23: xrealloc(void *ptr, size_t size)
        !            24: {
        !            25:        void *result;
        !            26: 
        !            27:        result = realloc(ptr, size);
        !            28:        if (size && !result) {
        !            29:                eprintf("realloc failure");
        !            30:                exit(1);
        !            31:        }
        !            32:        return result;
        !            33: }
        !            34: 
        !            35: char *
        !            36: xstrdup(char *s)
        !            37: {
        !            38:        char *t;
        !            39: 
        !            40:        t = xmalloc(strlen(s) + 1);
        !            41:        strcpy(t, s);
        !            42:        return t;
        !            43: }
        !            44: 
        !            45: long
        !            46: fpcopy(FILE *dst, FILE *src, long size)
        !            47: {
        !            48:        char buf[4096];
        !            49:        long cc, total;
        !            50: 
        !            51:        total = 0;
        !            52:        while (total < size) {
        !            53:                cc = fread(buf, 1, MIN(sizeof buf, size - total), src);
        !            54:                if (cc <= 0)
        !            55:                        break;
        !            56:                if (fwrite(buf, 1, cc, dst) != cc)
        !            57:                        break;
        !            58:                total += cc;
        !            59:        }
        !            60:        if (ferror(dst) || ferror(src))
        !            61:                return -1;
        !            62:        return total;
        !            63: }
        !            64: 
        !            65: void
        !            66: discard(FILE *fp, long n)
        !            67: {
        !            68:        char buf[4096];
        !            69:        long cc;
        !            70: 
        !            71:        while (n >= sizeof buf) {
        !            72:                cc = fread(buf, 1, sizeof buf, fp);
        !            73:                if (cc <= 0)
        !            74:                        return;
        !            75:                n -= cc;
        !            76:        }
        !            77:        fread(buf, 1, n, fp);
        !            78: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.