]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
2 * "$Id: tempfile.c 6649 2007-07-11 21:46:42Z mike $"
4 * Temp file utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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...
34 #if defined(WIN32) || defined(__EMX__)
38 #endif /* WIN32 || __EMX__ */
42 * 'cupsTempFd()' - Creates a temporary file.
44 * The temporary filename is returned in the filename buffer.
45 * The temporary file is opened for reading and writing.
48 int /* O - New file descriptor or -1 on error */
49 cupsTempFd(char *filename
, /* I - Pointer to buffer */
50 int len
) /* I - Size of buffer */
52 int fd
; /* File descriptor for temp file */
53 int tries
; /* Number of tries */
54 const char *tmpdir
; /* TMPDIR environment var */
56 char tmppath
[1024]; /* Windows temporary directory */
57 DWORD curtime
; /* Current time */
59 struct timeval curtime
; /* Current time */
64 * See if TMPDIR is defined...
68 if ((tmpdir
= getenv("TEMP")) == NULL
)
70 GetTempPath(sizeof(tmppath
), tmppath
);
75 * Previously we put root temporary files in the default CUPS temporary
76 * directory under /var/spool/cups. However, since the scheduler cleans
77 * out temporary files there and runs independently of the user apps, we
78 * don't want to use it unless specifically told to by cupsd.
81 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
83 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
86 # endif /* __APPLE__ */
90 * Make the temporary name using the specified directory...
99 * Get the current time of day...
102 curtime
= GetTickCount() + tries
;
105 * Format a string using the hex time values...
108 snprintf(filename
, len
- 1, "%s/%05lx%08lx", tmpdir
,
109 GetCurrentProcessId(), curtime
);
112 * Get the current time of day...
115 gettimeofday(&curtime
, NULL
);
118 * Format a string using the hex time values...
121 snprintf(filename
, len
- 1, "%s/%08lx%05lx", tmpdir
,
122 (unsigned long)curtime
.tv_sec
, (unsigned long)curtime
.tv_usec
);
126 * Open the file in "exclusive" mode, making sure that we don't
127 * stomp on an existing file or someone's symlink crack...
131 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
132 _S_IREAD
| _S_IWRITE
);
133 #elif defined(O_NOFOLLOW)
134 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
136 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
139 if (fd
< 0 && errno
!= EEXIST
)
144 while (fd
< 0 && tries
< 1000);
147 * Return the file descriptor...
155 * 'cupsTempFile()' - Generates a temporary filename.
157 * The temporary filename is returned in the filename buffer.
158 * This function is deprecated - use @link cupsTempFd@ or
159 * @link cupsTempFile2@ instead.
164 char * /* O - Filename or @code NULL@ on error */
165 cupsTempFile(char *filename
, /* I - Pointer to buffer */
166 int len
) /* I - Size of buffer */
168 int fd
; /* File descriptor for temp file */
169 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
173 * See if a filename was specified...
176 if (filename
== NULL
)
178 filename
= cg
->tempfile
;
179 len
= sizeof(cg
->tempfile
);
183 * Create the temporary file...
186 if ((fd
= cupsTempFd(filename
, len
)) < 0)
190 * Close the temp file - it'll be reopened later as needed...
196 * Return the temp filename...
204 * 'cupsTempFile2()' - Creates a temporary CUPS file.
206 * The temporary filename is returned in the filename buffer.
207 * The temporary file is opened for writing.
212 cups_file_t
* /* O - CUPS file or @code NULL@ on error */
213 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
214 int len
) /* I - Size of buffer */
216 cups_file_t
*file
; /* CUPS file */
217 int fd
; /* File descriptor */
220 if ((fd
= cupsTempFd(filename
, len
)) < 0)
222 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)
234 * End of "$Id: tempfile.c 6649 2007-07-11 21:46:42Z mike $".