Annotation of tftpd/src/buf.c, revision 1.1.2.3

1.1.2.1   misho       1: #include "global.h"
                      2: #include "buf.h"
                      3: 
                      4: 
                      5: int
                      6: initBuffer(int siz)
                      7: {
                      8:        ETRACE();
                      9: 
                     10:        bf = rpack_create(NULL, 0);
                     11:        if (!bf)
                     12:                return -1;
                     13:        if (rpack_attach(bf, siz) == -1) {
                     14:                rpack_destroy(&bf);
                     15:                return -1;
                     16:        }
                     17: 
                     18:        return 0;
                     19: }
                     20: 
                     21: void
                     22: endBuffer()
                     23: {
                     24:        flushBuffer(cli.fd);
                     25:        rpack_detach(bf);
                     26:        rpack_destroy(&bf);
                     27: }
                     28: 
                     29: int
                     30: flushBuffer(int fd)
                     31: {
                     32:        int ret = 0;
1.1.2.3 ! misho      33:        const char *m;
1.1.2.1   misho      34: 
1.1.2.2   misho      35:        if (!bf || !cli.addr.sa.sa_len || cli.opc != TFTP_OPC_WRQ)
1.1.2.1   misho      36:                return 0;
                     37: 
1.1.2.3 ! misho      38:        m = cfg_getAttribute(&cfg, "tftpd", "buf_pad");
        !            39:        if (m) {
        !            40:                memset(RPACK_NEXT(bf), *m, RPACK_REMAIN(bf));
        !            41:                EVERBOSE(4, "Padding buffer with '%c' len=%d", *m, RPACK_REMAIN(bf));
        !            42:                rpack_rnext(bf, RPACK_REMAIN(bf));
        !            43:        }
        !            44: 
1.1.2.1   misho      45:        ret = write(fd, RPACK_BUF(bf), RPACK_OFF(bf));
                     46:        if (ret == -1)
                     47:                ESYSERR(0);
                     48:        else
                     49:                RPACK_REWIND(bf);
                     50: 
                     51:        EVERBOSE(3, "Flush buffer ret=%d", ret);
                     52:        return ret;
                     53: }
                     54: 
                     55: int
                     56: bfwrite(int fd, void *buf, int buflen)
                     57: {
                     58:        void *m;
                     59: 
                     60:        if (!buf)
                     61:                return -1;
                     62: 
                     63:        /* flush */
                     64:        if (RPACK_REMAIN(bf) < buflen)
                     65:                flushBuffer(fd);
                     66: 
                     67:        m = rpack_rdata(bf, buf, buflen);
                     68:        if (!m)
                     69:                return -1;
                     70: 
                     71:        e_free(m);
                     72:        return buflen;
                     73: }

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