📄 file.c
字号:
return (total);}/* * 'cupsFileSeek()' - Seek in a file. */int /* O - New file position or -1 */cupsFileSeek(cups_file_t *fp, /* I - CUPS file */ int pos) /* I - Position in file */{ int bytes; /* Number bytes in buffer */ /* * Range check input... */ if (!fp || pos < 0 || fp->mode != 'r') return (-1); /* * Figure out the number of bytes in the current buffer, and then * see if we are outside of it... */ bytes = fp->end - fp->buf; if (pos < fp->pos) { /* * Need to seek backwards... */#if 0#ifdef HAVE_LIBZ if (fp->compressed) { inflateEnd(&fp->stream); lseek(fp->fd, 0, SEEK_SET); fp->pos = 0; fp->ptr = NULL; fp->end = NULL; fp->eof = 0; while ((bytes = cups_fill(fp)) > 0) if (pos >= fp->pos && pos < (fp->pos + bytes)) break; if (bytes <= 0) return (-1); } else#endif /* HAVE_LIBZ */#endif { lseek(fp->fd, pos, SEEK_SET); fp->pos = pos; fp->ptr = NULL; fp->end = NULL;#ifdef HAVE_LIBZ fp->eof = 0;#endif /* HAVE_LIBZ */ } } else if (pos >= (fp->pos + bytes)) { /* * Need to seek forwards... */#if 0#ifdef HAVE_LIBZ if (fp->compressed) { while ((bytes = cups_fill(fp)) > 0) if (pos >= fp->pos && pos < (fp->pos + bytes)) break; if (bytes <= 0) return (-1); } else#endif /* HAVE_LIBZ */#endif { lseek(fp->fd, pos, SEEK_SET); fp->pos = pos; fp->ptr = NULL; fp->end = NULL;#ifdef HAVE_LIBZ fp->eof = 0;#endif /* HAVE_LIBZ */ } } else { /* * Just reposition the current pointer, since we have the right * range... */ fp->ptr = fp->buf + pos - fp->pos;#ifdef HAVE_LIBZ fp->eof = 0;#endif /* HAVE_LIBZ */ } return (pos);}/* * 'cupsFileWrite()' - Write to a file. */int /* O - Number of bytes written */cupsFileWrite(cups_file_t *fp, /* I - CUPS file */ const char *buf, /* I - Buffer */ int bytes) /* I - Number of bytes to write */{ /* * Range check input... */ if (!fp || !buf || bytes < 0 || fp->mode != 'w') return (-1); if (bytes == 0) return (0); /* * Write the buffer... */ if ((fp->ptr + bytes) > fp->end) if (cupsFileFlush(fp)) return (-1); fp->pos += bytes; if (bytes > sizeof(fp->buf)) return (cups_write(fp->fd, buf, bytes)); else { memcpy(fp->ptr, buf, bytes); fp->ptr += bytes; return (bytes); }}/* * 'cups_fill()' - Fill the input buffer... */static int /* O - Number of bytes or -1 */cups_fill(cups_file_t *fp) /* I - CUPS file */{ int bytes; /* Number of bytes read */#if 0#ifdef HAVE_LIBZ const unsigned char *ptr, /* Pointer into buffer */ *end; /* End of buffer */#endif /* HAVE_LIBZ */#endif /* * Update the "pos" element as needed... */ if (fp->ptr) fp->pos += fp->end - fp->buf;#if 0#ifdef HAVE_LIBZ /* * Check to see if we have read any data yet; if not, see if we have a * compressed file... */ if (!fp->ptr) { /* * Reset the file position in case we are seeking... */ fp->compressed = 0; fp->pos = 0; /* * Read the first bytes in the file to determine if we have a gzip'd * file... */ if ((bytes = cups_read(fp->fd, (char *)fp->cbuf, sizeof(fp->cbuf))) < 0) { /* * Can't read from file! */ return (-1); } if (bytes < 10 || fp->cbuf[0] != 0x1f || fp->cbuf[1] != 0x8b || fp->cbuf[2] != 8 || (fp->cbuf[3] & 0xe0) != 0) { /* * Not a gzip'd file! */ memcpy(fp->buf, fp->cbuf, bytes); fp->ptr = fp->buf; fp->end = fp->buf + bytes; return (bytes); } /* * Parse header junk: extra data, original name, and comment... */ ptr = fp->cbuf + 10; end = fp->cbuf + bytes; if (fp->cbuf[3] & 0x04) { /* * Skip extra data... */ if ((ptr + 2) > end) { /* * Can't read from file! */ return (-1); } bytes = ((unsigned char)ptr[1] << 8) | (unsigned char)ptr[0]; ptr += 2 + bytes; if (ptr > end) { /* * Can't read from file! */ return (-1); } } if (fp->cbuf[3] & 0x08) { /* * Skip original name data... */ while (ptr < end && *ptr) ptr ++; if (ptr < end) ptr ++; else { /* * Can't read from file! */ return (-1); } } if (fp->cbuf[3] & 0x10) { /* * Skip comment data... */ while (ptr < end && *ptr) ptr ++; if (ptr < end) ptr ++; else { /* * Can't read from file! */ return (-1); } } if (fp->cbuf[3] & 0x02) { /* * Skip header CRC data... */ ptr += 2; if (ptr > end) { /* * Can't read from file! */ return (-1); } } /* * Setup the decompressor data... */ fp->stream.zalloc = (alloc_func)0; fp->stream.zfree = (free_func)0; fp->stream.opaque = (voidpf)0; fp->stream.next_in = (Bytef *)ptr; fp->stream.next_out = NULL; fp->stream.avail_in = end - ptr; fp->stream.avail_out = 0; if (inflateInit2(&(fp->stream), -15) != Z_OK) return (-1); fp->compressed = 1; } if (fp->compressed) { /* * If we have reached end-of-file, return immediately... */ if (fp->eof) return (-1); /* * Fill the decompression buffer as needed... */ if (fp->stream.avail_in == 0) { if ((bytes = cups_read(fp->fd, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0) return (-1); fp->stream.next_in = fp->cbuf; fp->stream.avail_in = bytes; } /* * Decompress data from the buffer... */ fp->stream.next_out = (Bytef *)fp->buf; fp->stream.avail_out = sizeof(fp->buf); if (inflate(&(fp->stream), Z_NO_FLUSH) == Z_STREAM_END) { /* * Mark end-of-file; note: we do not support concatenated gzip files * like gunzip does... */ fp->eof = 1; } bytes = sizeof(fp->buf) - fp->stream.avail_out; /* * Return the decompressed data... */ fp->ptr = fp->buf; fp->end = fp->buf + bytes; return (bytes); }#endif /* HAVE_LIBZ */#endif /* * Read a buffer's full of data... */ if ((bytes = cups_read(fp->fd, fp->buf, sizeof(fp->buf))) <= 0) { /* * Can't read from file! */ fp->ptr = fp->buf; fp->end = fp->buf; return (-1); } /* * Return the bytes we read... */ fp->ptr = fp->buf; fp->end = fp->buf + bytes; return (bytes);}/* * 'cups_read()' - Read from a file descriptor. */int /* O - Number of bytes read or -1 */cups_read(int fd, /* I - File descriptor */ char *buf, /* I - Buffer */ int bytes) /* I - Number bytes */{ int total; /* Total bytes read */ /* * Loop until we read at least 0 bytes... */ while ((total = read(fd, buf, bytes)) < 0) { /* * Reads can be interrupted by signals and unavailable resources... */ if (errno == EAGAIN || errno == EINTR) continue; else return (-1); } /* * Return the total number of bytes read... */ return (total);}/* * 'cups_write()' - Write to a file descriptor. */int /* O - Number of bytes written or -1 */cups_write(int fd, /* I - File descriptor */ const char *buf, /* I - Buffer */ int bytes) /* I - Number bytes */{ int total, /* Total bytes written */ count; /* Count this time */ /* * Loop until all bytes are written... */ total = 0; while (bytes > 0) { if ((count = write(fd, buf, bytes)) < 0) { /* * Writes can be interrupted by signals and unavailable resources... */ if (errno == EAGAIN || errno == EINTR) continue; else return (-1); } /* * Update the counts for the last write call... */ bytes -= count; total += count; buf += count; } /* * Return the total number of bytes written... */ return (total);}/* * End of "$Id: file.c,v 1.12 2005/01/03 19:29:59 mike Exp $". */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -