File: tempfile.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (89 lines) | stat: -rw-r--r-- 2,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/***********************************************************
 * char *
 * G_tempfile()
 *
 *   returns a unique temporary (full path) file name
 *   these files are in a temp directory under the
 *   current mapset.
 *
 *   successive calls will generate different names
 *   the names are of the form pid.n where pid is the
 *   programs process id number and n is a unique identifier
 *
 * returns:
 *   pointer to a character string containing the name.
 *   the name is copied to allocated memory and may be
 *   released by the unix free() routine.
 *
 * note:
 *   It is recommended to unlink() the tempfile on exit/error.
 *   Only if GRASS is left with 'exit', the GIS mapset manangement 
 *   will clean up the temp directory (ETC/clean_temp)
 ***********************************************************/

#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "gis.h"

/*!
 * \brief returns a temporary file name
 *
 * This routine
 * returns a pointer to a string containing a unique file name that can be used
 * as a temporary file within the module. Successive calls to G_tempfile() will
 * generate new names.
 * Only the file name is generated. The file itself is not created. To create the
 * file, the module must use standard UNIX functions which create and open files,
 * e.g., creat() or fopen().
 * The programmer should take reasonable care to remove (unlink) the file before
 * the module exits. However, GRASS database management will eventually remove
 * all temporary files created by G_tempfile() that have been left behind by
 * the modules which created them.
 *
 *  \return char:  pointer to a character string containing the name.
 *   the name is copied to allocated memory and may be
 *   released by the unix free() routine.
 */

char *G_tempfile(void)
{
    return G__tempfile(getpid());
}

char *G__tempfile (int pid)
{
    char path[1024];
    char name[20];
    char element[100];
    static int uniq = 0;
    struct stat st;

    if (pid <= 0)
	pid = getpid();
    G__temp_element(element);
    do
    {
	sprintf (name, "%d.%d", pid, uniq++) ;
	G__file_name (path, element, name, G_mapset()) ;
    }
    while (stat(path, &st) == 0) ;

    return G_store (path);
}

int G__temp_element(char *element)
{
    char *machine;

    strcpy (element, ".tmp");
    machine = G__machine_name();
    if (machine != NULL && *machine != 0)
    {
	strcat (element, "/");
	strcat (element, machine);
    }
    G__make_mapset_element (element);

    return 0;
}