/*
- * "$Id: file.c 177 2006-06-21 00:20:03Z jlovell $"
+ * "$Id: file.c 6962 2007-09-17 20:35:47Z mike $"
*
* File functions for the Common UNIX Printing System (CUPS).
*
* our own file functions allows us to provide transparent support of
* gzip'd print files, PPD files, etc.
*
- * Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ * Copyright 2007 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
* Contents:
*
#endif /* !O_LARGEFILE */
+/*
+ * Some operating systems don't define O_BINARY, which is used by Microsoft
+ * and IBM to flag binary files...
+ */
+
+#ifndef O_BINARY
+# define O_BINARY 0
+#endif /* !O_BINARY */
+
+
/*
* Types and structures...
*/
while (*path)
{
+#ifdef WIN32
+ if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255))))
+#else
if (*path == ';' || *path == ':')
+#endif /* WIN32 */
{
if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
*bufptr++ = '/';
#else
if (!access(buffer, executable ? X_OK : 0))
#endif /* WIN32 */
+ {
+ DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer));
return (buffer);
+ }
bufptr = buffer;
}
strlcpy(bufptr, filename, bufend - bufptr);
if (!access(buffer, 0))
+ {
+ DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer));
return (buffer);
+ }
else
+ {
+ DEBUG_puts("cupsFileFind: Returning NULL");
return (NULL);
+ }
}
int /* O - 0 on success, -1 on error */
cupsFileFlush(cups_file_t *fp) /* I - CUPS file */
{
- size_t bytes; /* Bytes to write */
+ ssize_t bytes; /* Bytes to write */
DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp));
return (-1);
}
- bytes = fp->ptr - fp->buf;
+ bytes = (ssize_t)(fp->ptr - fp->buf);
DEBUG_printf((" Flushing %ld bytes...\n", (long)bytes));
*/
if (!fp || (fp->mode != 'r' && fp->mode != 's'))
+ {
+ DEBUG_puts("cupsFileGetChar: Bad arguments!");
return (-1);
+ }
/*
* If the input buffer is empty, try to read more data...
if (fp->ptr >= fp->end)
if (cups_fill(fp) < 0)
+ {
+ DEBUG_puts("cupsFileGetChar: Unable to fill buffer!");
return (-1);
+ }
/*
* Return the next character in the buffer...
*/
+ DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp->ptr) & 255));
+
return (*(fp->ptr)++ & 255);
}
*/
*value = NULL;
-
+
while (cupsFileGets(fp, buf, buflen))
{
(*linenum) ++;
if ((ptr = strchr(buf, '#')) != NULL)
{
- while (ptr > buf)
+ if (ptr > buf && ptr[-1] == '\\')
{
- if (!isspace(ptr[-1] & 255))
- break;
-
- ptr --;
+ // Unquote the #...
+ _cups_strcpy(ptr - 1, ptr);
}
+ else
+ {
+ // Strip the comment and any trailing whitespace...
+ while (ptr > buf)
+ {
+ if (!isspace(ptr[-1] & 255))
+ break;
+
+ ptr --;
+ }
- *ptr = '\0';
+ *ptr = '\0';
+ }
}
/*
switch (*mode)
{
case 'a' : /* Append file */
- fd = open(filename, O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE, 0666);
+ fd = open(filename, O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY, 0666);
break;
case 'r' : /* Read file */
- fd = open(filename, O_RDONLY | O_LARGEFILE, 0);
+ fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0);
break;
case 'w' : /* Write file */
- fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE, 0666);
+ fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE | O_BINARY, 0666);
break;
case 's' : /* Read/write socket */
...) /* I - Additional args as necessary */
{
va_list ap; /* Argument list */
- size_t bytes; /* Formatted size */
+ ssize_t bytes; /* Formatted size */
char buf[8192]; /* Formatted text */
cupsFilePuts(cups_file_t *fp, /* I - CUPS file */
const char *s) /* I - String to write */
{
- size_t bytes; /* Bytes to write */
+ ssize_t bytes; /* Bytes to write */
/*
* Write the string...
*/
- bytes = strlen(s);
+ bytes = (int)strlen(s);
if (fp->mode == 's')
{
char *buf, /* O - Buffer */
size_t bytes) /* I - Number of bytes to read */
{
- size_t total, /* Total bytes read */
- count; /* Bytes read */
+ size_t total; /* Total bytes read */
+ ssize_t count; /* Bytes read */
DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
if (fp->ptr >= fp->end)
if (cups_fill(fp) <= 0)
{
- DEBUG_printf((" cups_fill() returned -1, total=%d\n", total));
+ DEBUG_printf((" cups_fill() returned -1, total=%d\n", (int)total));
if (total > 0)
- return (total);
+ return ((ssize_t)total);
else
return (-1);
}
- count = fp->end - fp->ptr;
- if (count > bytes)
- count = bytes;
+ count = (ssize_t)(fp->end - fp->ptr);
+ if (count > (ssize_t)bytes)
+ count = (ssize_t)bytes;
memcpy(buf, fp->ptr, count);
fp->ptr += count;
* Return the total number of bytes read...
*/
- DEBUG_printf((" total=%d\n", total));
+ DEBUG_printf((" total=%d\n", (int)total));
- return (total);
+ return ((ssize_t)total);
}
fp->pos = 0;
fp->ptr = NULL;
fp->end = NULL;
+ fp->eof = 0;
return (0);
}
cupsFileSeek(cups_file_t *fp, /* I - CUPS file */
off_t pos) /* I - Position in file */
{
- size_t bytes; /* Number bytes in buffer */
+ ssize_t bytes; /* Number bytes in buffer */
DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")\n", fp, pos));
*/
if (fp->ptr)
- bytes = fp->end - fp->buf;
+ bytes = (ssize_t)(fp->end - fp->buf);
else
bytes = 0;
if (cups_write(fp, buf, bytes) < 0)
return (-1);
- fp->pos += bytes;
+ fp->pos += (off_t)bytes;
- return (bytes);
+ return ((ssize_t)bytes);
}
if ((fp->ptr + bytes) > fp->end)
if (cupsFileFlush(fp))
return (-1);
- fp->pos += bytes;
+ fp->pos += (off_t)bytes;
if (bytes > sizeof(fp->buf))
{
{
memcpy(fp->ptr, buf, bytes);
fp->ptr += bytes;
- return (bytes);
+ return ((ssize_t)bytes);
}
}
{
ssize_t bytes; /* Number of bytes read */
#ifdef HAVE_LIBZ
+ int status; /* Decompression status */
const unsigned char *ptr, /* Pointer into buffer */
*end; /* End of buffer */
#endif /* HAVE_LIBZ */
*/
if (fp->ptr && fp->end)
- fp->pos += fp->end - fp->buf;
+ fp->pos += (off_t)(fp->end - fp->buf);
#ifdef HAVE_LIBZ
DEBUG_printf((" fp->compressed=%d\n", fp->compressed));
fp->stream.next_out = (Bytef *)fp->buf;
fp->stream.avail_out = sizeof(fp->buf);
- if (inflate(&(fp->stream), Z_NO_FLUSH) == Z_STREAM_END)
+ status = inflate(&(fp->stream), Z_NO_FLUSH);
+
+ if (fp->stream.next_out > (Bytef *)fp->buf)
+ fp->crc = crc32(fp->crc, (Bytef *)fp->buf,
+ fp->stream.next_out - (Bytef *)fp->buf);
+
+ if (status == Z_STREAM_END)
{
/*
* Read the CRC and length...
* Bad CRC, mark end-of-file...
*/
+ DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n",
+ (unsigned int)tcrc, (unsigned int)fp->crc));
+
fp->eof = 1;
return (-1);
for (;;)
{
+#ifdef WIN32
+ if (fp->mode == 's')
+ total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0);
+ else
+ total = (ssize_t)read(fp->fd, buf, (unsigned)bytes);
+#else
if (fp->mode == 's')
total = recv(fp->fd, buf, bytes, 0);
else
total = read(fp->fd, buf, bytes);
+#endif /* WIN32 */
if (total >= 0)
break;
const char *buf, /* I - Buffer */
size_t bytes) /* I - Number bytes */
{
- size_t total, /* Total bytes written */
- count; /* Count this time */
+ size_t total; /* Total bytes written */
+ ssize_t count; /* Count this time */
DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
total = 0;
while (bytes > 0)
{
+#ifdef WIN32
+ if (fp->mode == 's')
+ count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0);
+ else
+ count = (ssize_t)write(fp->fd, buf, (unsigned)bytes);
+#else
if (fp->mode == 's')
count = send(fp->fd, buf, bytes, 0);
else
count = write(fp->fd, buf, bytes);
+#endif /* WIN32 */
if (count < 0)
{
* Return the total number of bytes written...
*/
- return (total);
+ return ((ssize_t)total);
}
/*
- * End of "$Id: file.c 177 2006-06-21 00:20:03Z jlovell $".
+ * End of "$Id: file.c 6962 2007-09-17 20:35:47Z mike $".
*/