warnings on compile
Michael Sweet
mike at easysw.com
Fri Dec 15 18:00:39 GMT 2000
Jeremy Allison wrote:
> ...
> That's a good idea, we should look into grabbing the
> code from glibc or something and modifying it for our
> own use. Any volenteers ? :-).
Here's the code from CUPS that replaces the mkstemp() functionality
with something that is fairly portable; feel free to rip out the
Windows code... :)
/*
* 'cupsTempFile()' - Generate a temporary filename.
*/
char * /* O - Filename */
cupsTempFile(char *filename, /* I - Pointer to buffer */
int len) /* I - Size of buffer */
{
int fd; /* File descriptor for temp file */
#ifdef WIN32
char tmpdir[1024]; /* Windows temporary directory */
#else
char *tmpdir; /* TMPDIR environment var */
#endif /* WIN32 */
struct timeval curtime; /* Current time */
static char buf[1024] = ""; /* Buffer if you pass in NULL and 0 */
/*
* See if a filename was specified...
*/
if (filename == NULL)
{
filename = buf;
len = sizeof(buf);
}
/*
* See if TMPDIR is defined...
*/
#ifdef WIN32
GetTempPath(sizeof(tmpdir), tmpdir);
#else
if ((tmpdir = getenv("TMPDIR")) == NULL)
{
/*
* Put root temp files in restricted temp directory...
*/
if (getuid() == 0)
tmpdir = CUPS_REQUESTS "/tmp";
else
tmpdir = "/var/tmp";
}
#endif /* WIN32 */
/*
* Make the temporary name using the specified directory...
*/
do
{
/*
* Get the current time of day...
*/
gettimeofday(&curtime, NULL);
/*
* Format a string using the hex time values...
*/
snprintf(filename, len - 1, "%s/%08x%05x", tmpdir,
curtime.tv_sec, curtime.tv_usec);
/*
* Open the file in "exclusive" mode, making sure that we don't
* stomp on an existing file or someone's symlink crack...
*/
#ifdef O_NOFOLLOW
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
#else
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
#endif /* O_NOFOLLOW */
}
while (fd < 0);
/*
* Close the temp file - it'll be reopened later as needed...
*/
close(fd);
/*
* Return the temp filename...
*/
return (filename);
}
--
______________________________________________________________________
Michael Sweet, Easy Software Products mike at easysw.com
Printing Software for UNIX http://www.easysw.com
More information about the samba-technical
mailing list