Back to index

plt-scheme  4.2.1
TempFile.cc
Go to the documentation of this file.
00001 /*                                                      -*- C++ -*-
00002  *
00003  * Purpose: filename for temporary files
00004  *
00005  * Authors: Markus Holzem and Julian Smart
00006  *
00007  * Copyright: (C) 2004-2009 PLT Scheme Inc.
00008  * Copyright: (C) 1995, AIAI, University of Edinburgh (Julian)
00009  * Copyright: (C) 1995, GNU (Markus)
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00027 #include "wx.h"
00028 
00029 #include <unistd.h>
00030 #include <string.h>
00031 
00032 char *wxGetTempFileName(char *prefix, char *dest)
00033 {
00034     static short last_temp = 0;    // cache last to speed things a bit
00035     // At most 1000 temp files to a process! We use a ring count.
00036     char buf[64];
00037     short suffix;
00038 
00039     for (suffix = last_temp + 1; suffix != last_temp; ++suffix %= 1000) {
00040       int pid;
00041       pid = (int)getpid();
00042       sprintf (buf, "/tmp/%s%d.%03x", prefix, pid, (int)suffix);
00043       if (!wxFileExists(buf)) {
00044        // Touch the file to create it (reserve name)
00045        FILE *fd;
00046        fd = fopen (buf, "w");
00047        if (fd)
00048          fclose (fd);
00049        last_temp = suffix;
00050        if (dest)
00051          strcpy(dest, buf);
00052        else {
00053          dest = copystring(buf);
00054        }
00055        return dest;
00056        }
00057     }
00058     
00059     wxError("wxWindows: error finding temporary file name.");
00060     if (dest) dest[0] = 0;
00061 
00062     return NULL;
00063 }
00064