[BACK]Return to nullopen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / compress

Annotation of src/usr.bin/compress/nullopen.c, Revision 1.3

1.3     ! otto        1: /*     $OpenBSD: nullopen.c,v 1.2 2004/01/22 18:49:35 millert Exp $    */
1.1       deraadt     2:
                      3: /*
                      4:  * Copyright (c) 2003 Can Erkin Acar
                      5:  * Copyright (c) 1997 Michael Shalayeff
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     18:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     19:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  *
                     29:  */
                     30:
                     31: const char null_rcsid[] =
1.3     ! otto       32:     "$OpenBSD: nullopen.c,v 1.2 2004/01/22 18:49:35 millert Exp $";
1.1       deraadt    33:
                     34: #include <sys/types.h>
                     35: #include <stdio.h>
                     36: #include <stdlib.h>
                     37: #include <errno.h>
                     38: #include <unistd.h>
                     39: #include "compress.h"
                     40:
                     41: typedef struct {
                     42:        off_t     total_in;
                     43:        off_t     total_out;
                     44:        int       fd;
                     45:        int       gotmagic;
                     46:        char      mode;
                     47: } null_stream;
                     48:
                     49: char null_magic[2];
                     50:
                     51:
                     52: void *
                     53: null_open(int fd, const char *mode, char *name, int bits,
                     54:     u_int32_t mtime, int gotmagic)
                     55: {
                     56:        null_stream *s;
                     57:
                     58:        if (fd < 0 || !mode)
                     59:                return NULL;
                     60:
                     61:        if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0') {
                     62:                errno = EINVAL;
                     63:                return NULL;
                     64:        }
                     65:
                     66:        if ((s = (null_stream *) calloc(1, sizeof(null_stream))) == NULL)
                     67:                return NULL;
                     68:
                     69:        s->fd = fd;
                     70:        s->gotmagic = gotmagic;
                     71:        s->total_in = s->total_out = 0;
                     72:        s->mode = mode[0];
                     73:
                     74:        return s;
                     75: }
                     76:
                     77: int
1.3     ! otto       78: null_close(void *cookie, struct z_info *info, const char *name, struct stat *sb)
1.1       deraadt    79: {
                     80:        null_stream *s = (null_stream*) cookie;
                     81:        int err = 0;
                     82:
                     83:        if (s == NULL)
                     84:                return -1;
                     85:
                     86:
                     87:        if (info != NULL) {
                     88:                info->mtime = 0;
                     89:                info->crc =  (u_int32_t)-1;
                     90:                info->hlen = 0;
                     91:                info->total_in = (off_t) s->total_in;
                     92:                info->total_out = (off_t) s->total_out;
                     93:        }
                     94:
1.3     ! otto       95:        setfile(name, s->fd, sb);
1.1       deraadt    96:        err = close(s->fd);
                     97:
                     98:        free(s);
                     99:
                    100:        return err;
                    101: }
                    102:
                    103: int
                    104: null_flush(void *cookie, int flush)
                    105: {
                    106:        null_stream *s = (null_stream*)cookie;
                    107:
                    108:        if (s == NULL || s->mode != 'w') {
                    109:                errno = EBADF;
                    110:                return (-1);
                    111:        }
                    112:
                    113:        return 0;
                    114: }
                    115:
                    116: int
                    117: null_read(void *cookie, char *buf, int len)
                    118: {
                    119:        null_stream *s = (null_stream*)cookie;
                    120:
                    121:        if (s == NULL) {
                    122:                errno = EBADF;
                    123:                return (-1);
                    124:        }
                    125:        if (s->gotmagic) {
                    126:                if (len < 2) {
                    127:                        errno = EBADF;
                    128:                        return (-1);
                    129:                }
                    130:
                    131:                buf[0] = null_magic[0];
                    132:                buf[1] = null_magic[1];
                    133:                s->gotmagic = 0;
                    134:
                    135:                return (2);
                    136:        }
                    137:
                    138:        return read(s->fd, buf, len);
                    139: }
                    140:
                    141: int
                    142: null_write(void *cookie, const char *buf, int len)
                    143: {
                    144:        null_stream *s = (null_stream*)cookie;
                    145:
                    146:        if (s == NULL) {
                    147:                errno = EBADF;
                    148:                return (-1);
                    149:        }
                    150:
                    151:        return write(s->fd, buf, len);
                    152: }