/*
- * "$Id: tempfile.c 7337 2008-02-22 04:44:04Z mike $"
+ * Temp file utilities for CUPS.
*
- * Temp file utilities for CUPS.
+ * Copyright © 2007-2018 by Apple Inc.
+ * Copyright © 1997-2006 by Easy Software Products.
*
- * Copyright 2007-2010 by Apple Inc.
- * Copyright 1997-2006 by Easy Software Products.
- *
- * These coded instructions, statements, and computer programs are the
- * 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/".
- *
- * This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- * cupsTempFd() - Creates a temporary file.
- * cupsTempFile() - Generates a temporary filename.
- * cupsTempFile2() - Creates a temporary CUPS file.
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more
+ * information.
*/
/*
int fd; /* File descriptor for temp file */
int tries; /* Number of tries */
const char *tmpdir; /* TMPDIR environment var */
+#if defined(__APPLE__) || defined(WIN32)
+ char tmppath[1024]; /* Temporary directory */
+#endif /* __APPLE__ || WIN32 */
#ifdef WIN32
- char tmppath[1024]; /* Windows temporary directory */
DWORD curtime; /* Current time */
#else
struct timeval curtime; /* Current time */
GetTempPath(sizeof(tmppath), tmppath);
tmpdir = tmppath;
}
+
+#elif defined(__APPLE__)
+ /*
+ * On macOS and iOS, the TMPDIR environment variable is not always the best
+ * location to place temporary files due to sandboxing. Instead, the confstr
+ * function should be called to get the proper per-user, per-process TMPDIR
+ * value. Currently this only happens if TMPDIR is not set or is set to
+ * "/var/folders/...".
+ */
+
+ if ((tmpdir = getenv("TMPDIR")) != NULL && !strncmp(tmpdir, "/var/folders/", 13))
+ tmpdir = NULL;
+
+ if (!tmpdir)
+ {
+ if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath)))
+ tmpdir = tmppath;
+ else
+ tmpdir = "/private/tmp"; /* This should never happen */
+ }
+
#else
/*
* Previously we put root temporary files in the default CUPS temporary
*/
if ((tmpdir = getenv("TMPDIR")) == NULL)
-# ifdef __APPLE__
- tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
-# else
tmpdir = "/tmp";
-# endif /* __APPLE__ */
#endif /* WIN32 */
/*
* Format a string using the hex time values...
*/
- snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir,
- GetCurrentProcessId(), curtime);
+ snprintf(filename, (size_t)len - 1, "%s/%05lx%08lx", tmpdir, GetCurrentProcessId(), curtime);
#else
/*
* Get the current time of day...
* Format a string using the hex time values...
*/
- snprintf(filename, len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(),
- (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
+ snprintf(filename, (size_t)len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(), (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
#endif /* WIN32 */
/*
* 'cupsTempFile()' - Generates a temporary filename.
*
* The temporary filename is returned in the filename buffer.
- * This function is deprecated - use @link cupsTempFd@ or
- * @link cupsTempFile2@ instead.
+ * This function is deprecated and will no longer generate a temporary
+ * filename - use @link cupsTempFd@ or @link cupsTempFile2@ instead.
*
* @deprecated@
*/
cupsTempFile(char *filename, /* I - Pointer to buffer */
int len) /* I - Size of buffer */
{
- int fd; /* File descriptor for temp file */
- _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
-
-
- /*
- * See if a filename was specified...
- */
-
- if (filename == NULL)
- {
- filename = cg->tempfile;
- len = sizeof(cg->tempfile);
- }
-
- /*
- * Create the temporary file...
- */
-
- if ((fd = cupsTempFd(filename, len)) < 0)
- return (NULL);
-
- /*
- * Close the temp file - it'll be reopened later as needed...
- */
-
- close(fd);
+ (void)len;
- /*
- * Return the temp filename...
- */
+ if (filename)
+ *filename = '\0';
- return (filename);
+ return (NULL);
}
* The temporary filename is returned in the filename buffer.
* The temporary file is opened for writing.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
cups_file_t * /* O - CUPS file or @code NULL@ on error */
else
return (file);
}
-
-
-/*
- * End of "$Id: tempfile.c 7337 2008-02-22 04:44:04Z mike $".
- */