]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
2 * "$Id: tempfile.c 4918 2006-01-12 05:14:40Z mike $"
4 * Temp file utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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() - Create a temporary file.
29 * cupsTempFile() - Generate a temporary filename.
30 * cupsTempFile2() - Create a temporary CUPS file.
34 * Include necessary headers...
43 #if defined(WIN32) || defined(__EMX__)
47 #endif /* WIN32 || __EMX__ */
51 * 'cupsTempFd()' - Create a temporary file.
53 * The temporary filename is stored in the filename buffer.
56 int /* O - New file descriptor */
57 cupsTempFd(char *filename
, /* I - Pointer to buffer */
58 int len
) /* I - Size of buffer */
60 int fd
; /* File descriptor for temp file */
61 int tries
; /* Number of tries */
62 const char *tmpdir
; /* TMPDIR environment var */
64 char tmppath
[1024]; /* Windows temporary directory */
65 DWORD curtime
; /* Current time */
67 struct timeval curtime
; /* Current time */
72 * See if TMPDIR is defined...
76 if ((tmpdir
= getenv("TEMP")) == NULL
)
78 GetTempPath(sizeof(tmppath
), tmppath
);
82 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
85 * Put root temp files in restricted temp directory...
89 tmpdir
= CUPS_REQUESTS
"/tmp";
96 * Make the temporary name using the specified directory...
105 * Get the current time of day...
108 curtime
= GetTickCount() + tries
;
111 * Format a string using the hex time values...
114 snprintf(filename
, len
- 1, "%s/%05lx%08lx", tmpdir
,
115 GetCurrentProcessId(), curtime
);
118 * Get the current time of day...
121 gettimeofday(&curtime
, NULL
);
124 * Format a string using the hex time values...
127 snprintf(filename
, len
- 1, "%s/%08lx%05lx", tmpdir
,
128 (unsigned long)curtime
.tv_sec
, (unsigned long)curtime
.tv_usec
);
132 * Open the file in "exclusive" mode, making sure that we don't
133 * stomp on an existing file or someone's symlink crack...
137 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
138 _S_IREAD
| _S_IWRITE
);
139 #elif defined(O_NOFOLLOW)
140 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
142 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
145 if (fd
< 0 && errno
!= EEXIST
)
150 while (fd
< 0 && tries
< 1000);
153 * Return the file descriptor...
161 * 'cupsTempFile()' - Generate a temporary filename.
163 * The temporary filename is stored in the filename buffer.
164 * This function is deprecated - use cupsTempFd() or cupsTempFile2()
170 char * /* O - Filename */
171 cupsTempFile(char *filename
, /* I - Pointer to buffer */
172 int len
) /* I - Size of buffer */
174 int fd
; /* File descriptor for temp file */
175 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
179 * See if a filename was specified...
182 if (filename
== NULL
)
184 filename
= cg
->tempfile
;
185 len
= sizeof(cg
->tempfile
);
189 * Create the temporary file...
192 if ((fd
= cupsTempFd(filename
, len
)) < 0)
196 * Close the temp file - it'll be reopened later as needed...
202 * Return the temp filename...
210 * 'cupsTempFile2()' - Create a temporary CUPS file.
212 * The temporary filename is stored in the filename buffer.
217 cups_file_t
* /* O - CUPS file or NULL on error */
218 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
219 int len
) /* I - Size of buffer */
221 cups_file_t
*file
; /* CUPS file */
222 int fd
; /* File descriptor */
225 if ((fd
= cupsTempFd(filename
, len
)) < 0)
227 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)
239 * End of "$Id: tempfile.c 4918 2006-01-12 05:14:40Z mike $".