source: vis_dev/glu-2.3/src/util/tmpfile.c @ 21

Last change on this file since 21 was 13, checked in by cecile, 13 years ago

library glu 2.3

File size: 3.3 KB
Line 
1/*
2 * Revision Control Information
3 *
4 * $Id: tmpfile.c,v 1.12 2009/01/05 21:04:04 fabio Exp $
5 *
6 */
7
8/*
9 *  util_tmpfile -- open an unnamed temporary file
10 *
11 *  Many compilers/systems do not have this, or have buggy versions.
12 *
13 */
14
15/* LINTLIBRARY */
16
17/* util_tempnam and check_directory are from
18   Jonathan I. Kamens          <jik@pit-manager.mit.edu> */
19
20/* modified slightly by Ellen Sentovich ellen@ic.berkeley.edu */
21
22#include <sys/types.h>
23#if HAVE_SYS_STAT_H
24#  include <sys/stat.h>
25#endif
26#if HAVE_SYS_FILE_H
27#  include <sys/file.h>
28#endif
29
30#include "util.h"
31
32static char check_directory(char const *dir)
33{
34     struct stat statbuf;
35
36     if (! dir)
37         return 0;
38     else if (stat(dir, &statbuf) < 0)
39         return 0;
40     else if (S_ISDIR(statbuf.st_mode))
41         return 0;
42     else if (access(dir, W_OK | X_OK) < 0)
43         return 0;
44     else
45         return 1;
46}
47
48/* function for creating temporary filenames */
49char *util_tempnam(char const *dir, char const *pfx)
50{
51     extern char *getenv(const char *);
52     char const *tmpdir = NULL, *env;
53     char *filename;
54     static char unique_letters[4] = "AAA";
55     char addslash = 0;
56
57     /*
58      * If a directory is passed in, verify that it exists and is a
59      * directory and is writeable by this process.  If no directory
60      * is passed in, or if the directory that is passed in does not
61      * exist, check the environment variable TMPDIR.  If it isn't
62      * set, check the predefined constant P_tmpdir.  If that isn't
63      * set, use "/tmp/".
64      */
65
66     if ((env = getenv ("TMPDIR")) && check_directory(env))
67         tmpdir = env;
68     else if (dir && check_directory(dir))
69         tmpdir = dir;
70#ifdef P_tmpdir
71     else if (check_directory(P_tmpdir))
72         tmpdir = P_tmpdir;
73#endif
74     else
75         tmpdir = "/tmp/";
76
77     /*
78      * OK, now that we've got a directory, figure out whether or not
79      * there's a slash at the end of it.
80      */
81     if (tmpdir[strlen(tmpdir) - 1] != '/')
82         addslash = 1;
83
84     /*
85      * Now figure out the set of unique letters.
86      */
87     unique_letters[0]++;
88     if (unique_letters[0] > 'Z') {
89         unique_letters[0] = 'A';
90         unique_letters[1]++;
91         if (unique_letters[1] > 'Z') {
92             unique_letters[1] = 'A';
93             unique_letters[2]++;
94             if (unique_letters[2] > 'Z') {
95                 unique_letters[2]++;
96             }
97         }
98     }
99
100     /*
101      * Allocate a string of sufficient length.
102      */
103     if (pfx) {
104         filename = (char *) malloc(strlen(tmpdir) + addslash + strlen(pfx) + 10
105);
106     } else {
107         filename = (char *) malloc(strlen(tmpdir) + addslash + 10);
108     }
109
110     /*
111      * And create the string.
112      */
113     (void) sprintf(filename, "%s%s%s%sa%05d", tmpdir, addslash ? "/" : "",
114                    pfx ? pfx : "", unique_letters, (int)getpid());
115
116     return filename;
117}
118
119
120#ifdef UNIX
121
122FILE *
123util_tmpfile(void)
124{
125    FILE *fp;
126    char *filename;
127
128    filename = util_tempnam(NIL(char), "VIS");
129    if ((fp = fopen(filename, "w+")) == NULL) {
130        FREE(filename);
131        return NULL;
132    }
133    (void) unlink(filename); 
134    FREE(filename);
135    return fp;
136}
137
138#else
139
140FILE *
141util_tmpfile(void)
142{
143    FILE *fp;
144
145    if ((fp = fopen("utiltmp", "w+")) == NULL) {
146        return NULL;
147    }
148    (void) unlink("utiltmp");
149    return fp;
150}
151
152#endif
Note: See TracBrowser for help on using the repository browser.