]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
2 * "$Id: tempfile.c 6039 2006-10-17 02:24:34Z mike $"
4 * Temp file utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsTempFd() - Creates a temporary file.
29 * cupsTempFile() - Generates a temporary filename.
30 * cupsTempFile2() - Creates a temporary CUPS file.
34 * Include necessary headers...
43 #if defined(WIN32) || defined(__EMX__)
47 #endif /* WIN32 || __EMX__ */
51 * 'cupsTempFd()' - Creates a temporary file.
53 * The temporary filename is returned in the filename buffer.
54 * The temporary file is opened for reading and writing.
57 int /* O - New file descriptor or -1 on error */
58 cupsTempFd(char *filename
, /* I - Pointer to buffer */
59 int len
) /* I - Size of buffer */
61 int fd
; /* File descriptor for temp file */
62 int tries
; /* Number of tries */
63 const char *tmpdir
; /* TMPDIR environment var */
65 char tmppath
[1024]; /* Windows temporary directory */
66 DWORD curtime
; /* Current time */
68 struct timeval curtime
; /* Current time */
73 * See if TMPDIR is defined...
77 if ((tmpdir
= getenv("TEMP")) == NULL
)
79 GetTempPath(sizeof(tmppath
), tmppath
);
83 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
86 * Put root temp files in restricted temp directory...
90 tmpdir
= CUPS_REQUESTS
"/tmp";
97 * Make the temporary name using the specified directory...
106 * Get the current time of day...
109 curtime
= GetTickCount() + tries
;
112 * Format a string using the hex time values...
115 snprintf(filename
, len
- 1, "%s/%05lx%08lx", tmpdir
,
116 GetCurrentProcessId(), curtime
);
119 * Get the current time of day...
122 gettimeofday(&curtime
, NULL
);
125 * Format a string using the hex time values...
128 snprintf(filename
, len
- 1, "%s/%08lx%05lx", tmpdir
,
129 (unsigned long)curtime
.tv_sec
, (unsigned long)curtime
.tv_usec
);
133 * Open the file in "exclusive" mode, making sure that we don't
134 * stomp on an existing file or someone's symlink crack...
138 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
139 _S_IREAD
| _S_IWRITE
);
140 #elif defined(O_NOFOLLOW)
141 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
143 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
146 if (fd
< 0 && errno
!= EEXIST
)
151 while (fd
< 0 && tries
< 1000);
154 * Return the file descriptor...
162 * 'cupsTempFile()' - Generates a temporary filename.
164 * The temporary filename is returned in the filename buffer.
165 * This function is deprecated - use cupsTempFd() or cupsTempFile2()
171 char * /* O - Filename or NULL on error */
172 cupsTempFile(char *filename
, /* I - Pointer to buffer */
173 int len
) /* I - Size of buffer */
175 int fd
; /* File descriptor for temp file */
176 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
180 * See if a filename was specified...
183 if (filename
== NULL
)
185 filename
= cg
->tempfile
;
186 len
= sizeof(cg
->tempfile
);
190 * Create the temporary file...
193 if ((fd
= cupsTempFd(filename
, len
)) < 0)
197 * Close the temp file - it'll be reopened later as needed...
203 * Return the temp filename...
211 * 'cupsTempFile2()' - Creates a temporary CUPS file.
213 * The temporary filename is returned in the filename buffer.
214 * The temporary file is opened for writing.
219 cups_file_t
* /* O - CUPS file or NULL on error */
220 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
221 int len
) /* I - Size of buffer */
223 cups_file_t
*file
; /* CUPS file */
224 int fd
; /* File descriptor */
227 if ((fd
= cupsTempFd(filename
, len
)) < 0)
229 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)
241 * End of "$Id: tempfile.c 6039 2006-10-17 02:24:34Z mike $".