aboutsummaryrefslogtreecommitdiffstats
path: root/cups/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'cups/file.c')
-rw-r--r--cups/file.c95
1 files changed, 70 insertions, 25 deletions
diff --git a/cups/file.c b/cups/file.c
index 5d150540..5c9ddf8f 100644
--- a/cups/file.c
+++ b/cups/file.c
@@ -6,7 +6,7 @@
* our own file functions allows us to provide transparent support of
* different line endings, gzip'd print files, PPD files, etc.
*
- * Copyright 2007-2017 by Apple Inc.
+ * Copyright 2007-2018 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
@@ -26,6 +26,39 @@
#include <sys/stat.h>
#include <sys/types.h>
+# ifdef HAVE_LIBZ
+# include <zlib.h>
+# endif /* HAVE_LIBZ */
+
+
+/*
+ * Internal structures...
+ */
+
+struct _cups_file_s /**** CUPS file structure... ****/
+
+{
+ int fd; /* File descriptor */
+ char mode, /* Mode ('r' or 'w') */
+ compressed, /* Compression used? */
+ is_stdio, /* stdin/out/err? */
+ eof, /* End of file? */
+ buf[4096], /* Buffer */
+ *ptr, /* Pointer into buffer */
+ *end; /* End of buffer data */
+ off_t pos, /* Position in file */
+ bufpos; /* File position for start of buffer */
+
+#ifdef HAVE_LIBZ
+ z_stream stream; /* (De)compression stream */
+ Bytef cbuf[4096]; /* (De)compression buffer */
+ uLong crc; /* (De)compression CRC */
+#endif /* HAVE_LIBZ */
+
+ char *printf_buffer; /* cupsFilePrintf buffer */
+ size_t printf_size; /* Size of cupsFilePrintf buffer */
+};
+
/*
* Local functions...
@@ -40,7 +73,7 @@ static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes);
static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes);
-#ifndef WIN32
+#ifndef _WIN32
/*
* '_cupsFileCheck()' - Check the permissions of the given filename.
*/
@@ -306,7 +339,7 @@ _cupsFileCheckFilter(
fprintf(stderr, "%s: %s\n", prefix, message);
}
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
/*
@@ -526,22 +559,22 @@ cupsFileFind(const char *filename, /* I - File to find */
while (*path)
{
-#ifdef WIN32
+#ifdef _WIN32
if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255))))
#else
if (*path == ';' || *path == ':')
-#endif /* WIN32 */
+#endif /* _WIN32 */
{
if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
*bufptr++ = '/';
strlcpy(bufptr, filename, (size_t)(bufend - bufptr));
-#ifdef WIN32
+#ifdef _WIN32
if (!access(buffer, 0))
#else
if (!access(buffer, executable ? X_OK : 0))
-#endif /* WIN32 */
+#endif /* _WIN32 */
{
DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer));
return (buffer);
@@ -992,11 +1025,11 @@ cupsFileLock(cups_file_t *fp, /* I - CUPS file */
* Try the lock...
*/
-#ifdef WIN32
+#ifdef _WIN32
return (_locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0));
#else
return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0));
-#endif /* WIN32 */
+#endif /* _WIN32 */
}
@@ -1084,11 +1117,11 @@ cupsFileOpen(const char *filename, /* I - Name of file */
}
if (fd >= 0)
-#ifdef WIN32
+#ifdef _WIN32
_chsize(fd, 0);
#else
ftruncate(fd, 0);
-#endif /* WIN32 */
+#endif /* _WIN32 */
break;
case 's' : /* Read/write socket */
@@ -1255,15 +1288,27 @@ cupsFileOpenFd(int fd, /* I - File descriptor */
* Don't pass this file to child processes...
*/
-#ifndef WIN32
+#ifndef _WIN32
fcntl(fp->fd, F_SETFD, fcntl(fp->fd, F_GETFD) | FD_CLOEXEC);
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
return (fp);
}
/*
+ * '_cupsFilePeekAhead()' - See if the requested character is buffered up.
+ */
+
+int /* O - 1 if present, 0 otherwise */
+_cupsFilePeekAhead(cups_file_t *fp, /* I - CUPS file */
+ int ch) /* I - Character */
+{
+ return (fp && fp->ptr && memchr(fp->ptr, ch, (size_t)(fp->end - fp->ptr)));
+}
+
+
+/*
* 'cupsFilePeekChar()' - Peek at the next character from a file.
*
* @since CUPS 1.2/macOS 10.5@
@@ -2019,11 +2064,11 @@ cupsFileUnlock(cups_file_t *fp) /* I - CUPS file */
* Unlock...
*/
-#ifdef WIN32
+#ifdef _WIN32
return (_locking(fp->fd, _LK_UNLCK, 0));
#else
return (lockf(fp->fd, F_ULOCK, 0));
-#endif /* WIN32 */
+#endif /* _WIN32 */
}
@@ -2553,9 +2598,9 @@ cups_open(const char *filename, /* I - Filename */
{
int fd; /* File descriptor */
struct stat fileinfo; /* File information */
-#ifndef WIN32
+#ifndef _WIN32
struct stat linkinfo; /* Link information */
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
/*
@@ -2583,18 +2628,18 @@ cups_open(const char *filename, /* I - Filename */
return (-1);
}
-#ifdef WIN32
+#ifdef _WIN32
if (fileinfo.st_mode & _S_IFDIR)
#else
if (S_ISDIR(fileinfo.st_mode))
-#endif /* WIN32 */
+#endif /* _WIN32 */
{
close(fd);
errno = EISDIR;
return (-1);
}
-#ifndef WIN32
+#ifndef _WIN32
/*
* Then use lstat to determine whether the filename is a symlink...
*/
@@ -2622,7 +2667,7 @@ cups_open(const char *filename, /* I - Filename */
errno = EPERM;
return (-1);
}
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
return (fd);
}
@@ -2648,7 +2693,7 @@ cups_read(cups_file_t *fp, /* I - CUPS file */
for (;;)
{
-#ifdef WIN32
+#ifdef _WIN32
if (fp->mode == 's')
total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0);
else
@@ -2658,7 +2703,7 @@ cups_read(cups_file_t *fp, /* I - CUPS file */
total = recv(fp->fd, buf, bytes, 0);
else
total = read(fp->fd, buf, bytes);
-#endif /* WIN32 */
+#endif /* _WIN32 */
DEBUG_printf(("9cups_read: total=" CUPS_LLFMT, CUPS_LLCAST total));
@@ -2705,7 +2750,7 @@ cups_write(cups_file_t *fp, /* I - CUPS file */
total = 0;
while (bytes > 0)
{
-#ifdef WIN32
+#ifdef _WIN32
if (fp->mode == 's')
count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0);
else
@@ -2715,7 +2760,7 @@ cups_write(cups_file_t *fp, /* I - CUPS file */
count = send(fp->fd, buf, bytes, 0);
else
count = write(fp->fd, buf, bytes);
-#endif /* WIN32 */
+#endif /* _WIN32 */
DEBUG_printf(("9cups_write: count=" CUPS_LLFMT, CUPS_LLCAST count));