]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
9375edc467601cc4fd75188651d876b8857154d5
2 * "$Id: tempfile.c 6599 2007-06-22 18:11:12Z 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
);
84 * Previously we put root temporary files in the default CUPS temporary
85 * directory under /var/spool/cups. However, since the scheduler cleans
86 * out temporary files there and runs independently of the user apps, we
87 * don't want to use it unless specifically told to by cupsd.
90 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
92 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
95 # endif /* __APPLE__ */
99 * Make the temporary name using the specified directory...
108 * Get the current time of day...
111 curtime
= GetTickCount() + tries
;
114 * Format a string using the hex time values...
117 snprintf(filename
, len
- 1, "%s/%05lx%08lx", tmpdir
,
118 GetCurrentProcessId(), curtime
);
121 * Get the current time of day...
124 gettimeofday(&curtime
, NULL
);
127 * Format a string using the hex time values...
130 snprintf(filename
, len
- 1, "%s/%08lx%05lx", tmpdir
,
131 (unsigned long)curtime
.tv_sec
, (unsigned long)curtime
.tv_usec
);
135 * Open the file in "exclusive" mode, making sure that we don't
136 * stomp on an existing file or someone's symlink crack...
140 fd
= open(filename
, _O_CREAT
| _O_RDWR
| _O_TRUNC
| _O_BINARY
,
141 _S_IREAD
| _S_IWRITE
);
142 #elif defined(O_NOFOLLOW)
143 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
145 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
148 if (fd
< 0 && errno
!= EEXIST
)
153 while (fd
< 0 && tries
< 1000);
156 * Return the file descriptor...
164 * 'cupsTempFile()' - Generates a temporary filename.
166 * The temporary filename is returned in the filename buffer.
167 * This function is deprecated - use cupsTempFd() or cupsTempFile2()
173 char * /* O - Filename or NULL on error */
174 cupsTempFile(char *filename
, /* I - Pointer to buffer */
175 int len
) /* I - Size of buffer */
177 int fd
; /* File descriptor for temp file */
178 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
182 * See if a filename was specified...
185 if (filename
== NULL
)
187 filename
= cg
->tempfile
;
188 len
= sizeof(cg
->tempfile
);
192 * Create the temporary file...
195 if ((fd
= cupsTempFd(filename
, len
)) < 0)
199 * Close the temp file - it'll be reopened later as needed...
205 * Return the temp filename...
213 * 'cupsTempFile2()' - Creates a temporary CUPS file.
215 * The temporary filename is returned in the filename buffer.
216 * The temporary file is opened for writing.
221 cups_file_t
* /* O - CUPS file or NULL on error */
222 cupsTempFile2(char *filename
, /* I - Pointer to buffer */
223 int len
) /* I - Size of buffer */
225 cups_file_t
*file
; /* CUPS file */
226 int fd
; /* File descriptor */
229 if ((fd
= cupsTempFd(filename
, len
)) < 0)
231 else if ((file
= cupsFileOpenFd(fd
, "w")) == NULL
)
243 * End of "$Id: tempfile.c 6599 2007-06-22 18:11:12Z mike $".