]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/tempfile.c
4 * Temp file utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2001 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-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsTempFd() - Create a temporary file.
27 * cupsTempFile() - Generate a temporary filename.
31 * Include necessary headers...
42 #if defined(WIN32) || defined(__EMX__)
46 #endif /* WIN32 || __EMX__ */
50 * 'cupsTempFd()' - Create a temporary file.
53 int /* O - New file descriptor */
54 cupsTempFd(char *filename
, /* I - Pointer to buffer */
55 int len
) /* I - Size of buffer */
57 int fd
; /* File descriptor for temp file */
59 char tmpdir
[1024]; /* Windows temporary directory */
60 DWORD curtime
; /* Current time */
62 char *tmpdir
; /* TMPDIR environment var */
63 struct timeval curtime
; /* Current time */
65 static char buf
[1024] = ""; /* Buffer if you pass in NULL and 0 */
69 * See if a filename was specified...
79 * See if TMPDIR is defined...
83 GetTempPath(sizeof(tmpdir
), tmpdir
);
85 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
88 * Put root temp files in restricted temp directory...
92 tmpdir
= CUPS_REQUESTS
"/tmp";
99 * Make the temporary name using the specified directory...
106 * Get the current time of day...
109 curtime
= GetTickCount();
112 * Format a string using the hex time values...
115 snprintf(filename
, len
- 1, "%s/%08lx", tmpdir
, 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 curtime
.tv_sec
, 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_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, 0600);
139 fd
= open(filename
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
140 #endif /* O_NOFOLLOW */
142 if (fd
< 0 && (errno
== EPERM
|| errno
== ENOENT
))
143 break; /* Stop immediately if permission denied or the dir doesn't exist! */
148 * Return the file descriptor...
156 * 'cupsTempFile()' - Generate a temporary filename.
159 char * /* O - Filename */
160 cupsTempFile(char *filename
, /* I - Pointer to buffer */
161 int len
) /* I - Size of buffer */
163 int fd
; /* File descriptor for temp file */
164 static char buf
[1024] = ""; /* Buffer if you pass in NULL and 0 */
168 * See if a filename was specified...
171 if (filename
== NULL
)
178 * Create the temporary file...
181 if ((fd
= cupsTempFd(filename
, len
)) < 0)
185 * Close the temp file - it'll be reopened later as needed...
191 * Return the temp filename...