tempfile.c

Go to the documentation of this file.
00001 /***********************************************************
00002  * char *
00003  * G_tempfile()
00004  *
00005  *   returns a unique temporary (full path) file name
00006  *   these files are in a temp directory under the
00007  *   current mapset.
00008  *
00009  *   successive calls will generate different names
00010  *   the names are of the form pid.n where pid is the
00011  *   programs process id number and n is a unique identifier
00012  *
00013  * returns:
00014  *   pointer to a character string containing the name.
00015  *   the name is copied to allocated memory and may be
00016  *   released by the unix free() routine.
00017  *
00018  * note:
00019  *   It is recommended to unlink() the tempfile on exit/error.
00020  *   Only if GRASS is left with 'exit', the GIS mapset manangement 
00021  *   will clean up the temp directory (ETC/clean_temp)
00022  ***********************************************************/
00023 
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <sys/stat.h>
00027 #include "gis.h"
00028 
00049 char *G_tempfile(void)
00050 {
00051     return G__tempfile(getpid());
00052 }
00053 
00054 char *G__tempfile (int pid)
00055 {
00056     char path[1024];
00057     char name[20];
00058     char element[100];
00059     static int uniq = 0;
00060     struct stat st;
00061 
00062     if (pid <= 0)
00063         pid = getpid();
00064     G__temp_element(element);
00065     do
00066     {
00067         sprintf (name, "%d.%d", pid, uniq++) ;
00068         G__file_name (path, element, name, G_mapset()) ;
00069     }
00070     while (stat(path, &st) == 0) ;
00071 
00072     return G_store (path);
00073 }
00074 
00075 int G__temp_element(char *element)
00076 {
00077     char *machine;
00078 
00079     strcpy (element, ".tmp");
00080     machine = G__machine_name();
00081     if (machine != NULL && *machine != 0)
00082     {
00083         strcat (element, "/");
00084         strcat (element, machine);
00085     }
00086     G__make_mapset_element (element);
00087 
00088     return 0;
00089 }

Generated on Wed Aug 23 17:49:23 2006 for GRASS by  doxygen 1.4.7