| 2145 | */ |
| 2146 | |
| 2147 | ssize_t /* O - Number of bytes written or -1 on error */ |
| 2148 | cupsFileWrite(cups_file_t *fp, /* I - CUPS file */ |
| 2149 | const char *buf, /* I - Buffer */ |
| 2150 | size_t bytes) /* I - Number of bytes to write */ |
| 2151 | { |
| 2152 | /* |
| 2153 | * Range check input... |
| 2154 | */ |
| 2155 | |
| 2156 | DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes)); |
| 2157 | |
| 2158 | if (!fp || !buf || (fp->mode != 'w' && fp->mode != 's')) |
| 2159 | return (-1); |
| 2160 | |
| 2161 | if (bytes == 0) |
| 2162 | return (0); |
| 2163 | |
| 2164 | /* |
| 2165 | * Write the buffer... |
| 2166 | */ |
| 2167 | |
| 2168 | if (fp->mode == 's') |
| 2169 | { |
| 2170 | if (cups_write(fp, buf, bytes) < 0) |
| 2171 | return (-1); |
| 2172 | |
| 2173 | fp->pos += (off_t)bytes; |
| 2174 | |
| 2175 | DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); |
| 2176 | |
| 2177 | return ((ssize_t)bytes); |
| 2178 | } |
| 2179 | |
| 2180 | if ((fp->ptr + bytes) > fp->end) |
| 2181 | if (cupsFileFlush(fp)) |
| 2182 | return (-1); |
| 2183 | |
| 2184 | fp->pos += (off_t)bytes; |
| 2185 | |
| 2186 | DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); |
| 2187 | |
| 2188 | if (bytes > sizeof(fp->buf)) |
| 2189 | { |
| 2190 | #ifdef HAVE_LIBZ |
| 2191 | if (fp->compressed) |
| 2192 | return (cups_compress(fp, buf, bytes)); |
| 2193 | else |
| 2194 | #endif /* HAVE_LIBZ */ |
| 2195 | return (cups_write(fp, buf, bytes)); |
| 2196 | } |
| 2197 | else |
| 2198 | { |
| 2199 | memcpy(fp->ptr, buf, bytes); |
| 2200 | fp->ptr += bytes; |
| 2201 | return ((ssize_t)bytes); |
| 2202 | } |
| 2203 | } |
| 2204 | |