]>
Commit | Line | Data |
---|---|---|
b56e1a4c | 1 | /* |
efb2f309 | 2 | * "$Id: tempfile.c,v 1.3 2002/01/02 17:58:41 mike Exp $" |
b56e1a4c | 3 | * |
4 | * Temp file utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
efb2f309 | 6 | * Copyright 1997-2002 by Easy Software Products. |
b56e1a4c | 7 | * |
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 | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsTempFd() - Create a temporary file. | |
27 | * cupsTempFile() - Generate a temporary filename. | |
28 | */ | |
29 | ||
30 | /* | |
31 | * Include necessary headers... | |
32 | */ | |
33 | ||
34 | #include "cups.h" | |
35 | #include "string.h" | |
36 | #include "debug.h" | |
37 | #include <stdlib.h> | |
38 | #include <ctype.h> | |
39 | #include <errno.h> | |
40 | #include <fcntl.h> | |
41 | #include <sys/stat.h> | |
42 | #if defined(WIN32) || defined(__EMX__) | |
43 | # include <io.h> | |
44 | #else | |
45 | # include <unistd.h> | |
46 | #endif /* WIN32 || __EMX__ */ | |
47 | ||
48 | ||
49 | /* | |
50 | * 'cupsTempFd()' - Create a temporary file. | |
51 | */ | |
52 | ||
53 | int /* O - New file descriptor */ | |
54 | cupsTempFd(char *filename, /* I - Pointer to buffer */ | |
55 | int len) /* I - Size of buffer */ | |
56 | { | |
57 | int fd; /* File descriptor for temp file */ | |
58 | #ifdef WIN32 | |
59 | char tmpdir[1024]; /* Windows temporary directory */ | |
60 | DWORD curtime; /* Current time */ | |
61 | #else | |
62 | char *tmpdir; /* TMPDIR environment var */ | |
63 | struct timeval curtime; /* Current time */ | |
64 | #endif /* WIN32 */ | |
65 | static char buf[1024] = ""; /* Buffer if you pass in NULL and 0 */ | |
66 | ||
67 | ||
68 | /* | |
69 | * See if a filename was specified... | |
70 | */ | |
71 | ||
72 | if (filename == NULL) | |
73 | { | |
74 | filename = buf; | |
75 | len = sizeof(buf); | |
76 | } | |
77 | ||
78 | /* | |
79 | * See if TMPDIR is defined... | |
80 | */ | |
81 | ||
82 | #ifdef WIN32 | |
83 | GetTempPath(sizeof(tmpdir), tmpdir); | |
84 | #else | |
85 | if ((tmpdir = getenv("TMPDIR")) == NULL) | |
86 | { | |
87 | /* | |
88 | * Put root temp files in restricted temp directory... | |
89 | */ | |
90 | ||
91 | if (getuid() == 0) | |
92 | tmpdir = CUPS_REQUESTS "/tmp"; | |
93 | else | |
94 | tmpdir = "/var/tmp"; | |
95 | } | |
96 | #endif /* WIN32 */ | |
97 | ||
98 | /* | |
99 | * Make the temporary name using the specified directory... | |
100 | */ | |
101 | ||
102 | do | |
103 | { | |
104 | #ifdef WIN32 | |
105 | /* | |
106 | * Get the current time of day... | |
107 | */ | |
108 | ||
109 | curtime = GetTickCount(); | |
110 | ||
111 | /* | |
112 | * Format a string using the hex time values... | |
113 | */ | |
114 | ||
115 | snprintf(filename, len - 1, "%s/%08lx", tmpdir, curtime); | |
116 | #else | |
117 | /* | |
118 | * Get the current time of day... | |
119 | */ | |
120 | ||
121 | gettimeofday(&curtime, NULL); | |
122 | ||
123 | /* | |
124 | * Format a string using the hex time values... | |
125 | */ | |
126 | ||
127 | snprintf(filename, len - 1, "%s/%08lx%05lx", tmpdir, | |
128 | curtime.tv_sec, curtime.tv_usec); | |
129 | #endif /* WIN32 */ | |
130 | ||
131 | /* | |
132 | * Open the file in "exclusive" mode, making sure that we don't | |
133 | * stomp on an existing file or someone's symlink crack... | |
134 | */ | |
135 | ||
136 | #ifdef O_NOFOLLOW | |
137 | fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600); | |
138 | #else | |
139 | fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); | |
140 | #endif /* O_NOFOLLOW */ | |
141 | ||
5c5e0e3e | 142 | if (fd < 0 && (errno == EPERM || errno == ENOENT)) |
143 | break; /* Stop immediately if permission denied or the dir doesn't exist! */ | |
b56e1a4c | 144 | } |
145 | while (fd < 0); | |
146 | ||
147 | /* | |
148 | * Return the file descriptor... | |
149 | */ | |
150 | ||
151 | return (fd); | |
152 | } | |
153 | ||
154 | ||
155 | /* | |
156 | * 'cupsTempFile()' - Generate a temporary filename. | |
157 | */ | |
158 | ||
159 | char * /* O - Filename */ | |
160 | cupsTempFile(char *filename, /* I - Pointer to buffer */ | |
161 | int len) /* I - Size of buffer */ | |
162 | { | |
163 | int fd; /* File descriptor for temp file */ | |
164 | static char buf[1024] = ""; /* Buffer if you pass in NULL and 0 */ | |
165 | ||
166 | ||
167 | /* | |
168 | * See if a filename was specified... | |
169 | */ | |
170 | ||
171 | if (filename == NULL) | |
172 | { | |
173 | filename = buf; | |
174 | len = sizeof(buf); | |
175 | } | |
176 | ||
177 | /* | |
178 | * Create the temporary file... | |
179 | */ | |
180 | ||
181 | if ((fd = cupsTempFd(filename, len)) < 0) | |
182 | return (NULL); | |
183 | ||
184 | /* | |
185 | * Close the temp file - it'll be reopened later as needed... | |
186 | */ | |
187 | ||
188 | close(fd); | |
189 | ||
190 | /* | |
191 | * Return the temp filename... | |
192 | */ | |
193 | ||
194 | return (filename); | |
195 | } | |
196 | ||
197 | ||
198 | /* | |
efb2f309 | 199 | * End of "$Id: tempfile.c,v 1.3 2002/01/02 17:58:41 mike Exp $". |
b56e1a4c | 200 | */ |