Open a gzip file either by name or file descriptor. */
(path, fd, mode)
| 91 | |
| 92 | /* Open a gzip file either by name or file descriptor. */ |
| 93 | local gzFile gz_open(path, fd, mode) |
| 94 | const |
| 95 | void* path; |
| 96 | int fd; |
| 97 | |
| 98 | const char* mode; |
| 99 | { |
| 100 | gz_statep state; |
| 101 | z_size_t len; |
| 102 | int oflag; |
| 103 | #ifdef O_CLOEXEC |
| 104 | int cloexec = 0; |
| 105 | #endif |
| 106 | #ifdef O_EXCL |
| 107 | int exclusive = 0; |
| 108 | #endif |
| 109 | |
| 110 | /* check input */ |
| 111 | if (path == NULL) |
| 112 | return NULL; |
| 113 | |
| 114 | /* allocate gzFile structure to return */ |
| 115 | state = (gz_statep)malloc(sizeof(gz_state)); |
| 116 | if (state == NULL) |
| 117 | return NULL; |
| 118 | state->size = 0; /* no buffers allocated yet */ |
| 119 | state->want = GZBUFSIZE; /* requested buffer size */ |
| 120 | state->msg = NULL; /* no error message yet */ |
| 121 | |
| 122 | /* interpret mode */ |
| 123 | state->mode = GZ_NONE; |
| 124 | state->level = Z_DEFAULT_COMPRESSION; |
| 125 | state->strategy = Z_DEFAULT_STRATEGY; |
| 126 | state->direct = 0; |
| 127 | while (*mode) |
| 128 | { |
| 129 | if (*mode >= '0' && *mode <= '9') |
| 130 | state->level = *mode - '0'; |
| 131 | else |
| 132 | switch (*mode) |
| 133 | { |
| 134 | case 'r': |
| 135 | state->mode = GZ_READ; |
| 136 | break; |
| 137 | #ifndef NO_GZCOMPRESS |
| 138 | case 'w': |
| 139 | state->mode = GZ_WRITE; |
| 140 | break; |
| 141 | case 'a': |
| 142 | state->mode = GZ_APPEND; |
| 143 | break; |
| 144 | #endif |
| 145 | case '+': /* can't read and write at the same time */ |
| 146 | free(state); |
| 147 | return NULL; |
| 148 | case 'b': /* ignore -- will request binary anyway */ |
| 149 | break; |
| 150 | #ifdef O_CLOEXEC |