2 * "$Id: tempfile.c 9042 2010-03-24 00:45:34Z mike $"
4 * Temp file utilities for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsTempFd() - Creates a temporary file.
20 * cupsTempFile() - Generates a temporary filename.
21 * cupsTempFile2() - Creates a temporary CUPS file.
25 * Include necessary headers...
28 #include "cups-private.h"
32 #if defined(WIN32) || defined(__EMX__)
36 #endif /* WIN32 || __EMX__ */
40 * 'cupsTempFd()' - Creates a temporary file.
42 * The temporary filename is returned in the filename buffer.
43 * The temporary file is opened for reading and writing.
46 int /* O - New file descriptor or -1 on error */
47 cupsTempFd(char *filename, /* I - Pointer to buffer */
48 int len) /* I - Size of buffer */
50 int fd; /* File descriptor for temp file */
51 int tries; /* Number of tries */
52 const char *tmpdir; /* TMPDIR environment var */
54 char tmppath[1024]; /* Windows temporary directory */
55 DWORD curtime; /* Current time */
57 struct timeval curtime; /* Current time */
62 * See if TMPDIR is defined...
66 if ((tmpdir = getenv("TEMP")) == NULL)
68 GetTempPath(sizeof(tmppath), tmppath);
73 * Previously we put root temporary files in the default CUPS temporary
74 * directory under /var/spool/cups. However, since the scheduler cleans
75 * out temporary files there and runs independently of the user apps, we
76 * don't want to use it unless specifically told to by cupsd.
79 if ((tmpdir = getenv("TMPDIR")) == NULL)
81 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
84 # endif /* __APPLE__ */
88 * Make the temporary name using the specified directory...
97 * Get the current time of day...
100 curtime = GetTickCount() + tries;
103 * Format a string using the hex time values...
106 snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir,
107 GetCurrentProcessId(), curtime);
110 * Get the current time of day...
113 gettimeofday(&curtime, NULL);
116 * Format a string using the hex time values...
119 snprintf(filename, len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(),
120 (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
124 * Open the file in "exclusive" mode, making sure that we don't
125 * stomp on an existing file or someone's symlink crack...
129 fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
130 _S_IREAD | _S_IWRITE);
131 #elif defined(O_NOFOLLOW)
132 fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
134 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
137 if (fd < 0 && errno != EEXIST)
142 while (fd < 0 && tries < 1000);
145 * Return the file descriptor...
153 * 'cupsTempFile()' - Generates a temporary filename.
155 * The temporary filename is returned in the filename buffer.
156 * This function is deprecated - use @link cupsTempFd@ or
157 * @link cupsTempFile2@ instead.
162 char * /* O - Filename or @code NULL@ on error */
163 cupsTempFile(char *filename, /* I - Pointer to buffer */
164 int len) /* I - Size of buffer */
166 int fd; /* File descriptor for temp file */
167 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
171 * See if a filename was specified...
174 if (filename == NULL)
176 filename = cg->tempfile;
177 len = sizeof(cg->tempfile);
181 * Create the temporary file...
184 if ((fd = cupsTempFd(filename, len)) < 0)
188 * Close the temp file - it'll be reopened later as needed...
194 * Return the temp filename...
202 * 'cupsTempFile2()' - Creates a temporary CUPS file.
204 * The temporary filename is returned in the filename buffer.
205 * The temporary file is opened for writing.
207 * @since CUPS 1.2/Mac OS X 10.5@
210 cups_file_t * /* O - CUPS file or @code NULL@ on error */
211 cupsTempFile2(char *filename, /* I - Pointer to buffer */
212 int len) /* I - Size of buffer */
214 cups_file_t *file; /* CUPS file */
215 int fd; /* File descriptor */
218 if ((fd = cupsTempFd(filename, len)) < 0)
220 else if ((file = cupsFileOpenFd(fd, "w")) == NULL)
232 * End of "$Id: tempfile.c 9042 2010-03-24 00:45:34Z mike $".