source: vis_dev/cusp-1.1/src/util/tmpfile.c

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

cusp added

File size: 3.4 KB
Line 
1/*
2 * Revision Control Information
3 *
4 * $Id: tmpfile.c,v 1.2 2009-04-13 17:03:40 hhkim 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 *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 *dir, char *pfx)
50{
51     /*extern char *getenv(const char *);*/
52     char *tmpdir = NULL, *env, *filename;
53     int l1, l2, l3;
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         l1 = strlen(tmpdir);
105         l2 = strlen(pfx);
106         l3 = l1 + l2 + addslash + 10;
107         /*filename = (char *) malloc(strlen(tmpdir) + addslash + strlen(pfx) + 10
108);*/
109         filename = (char *) malloc(l3);
110     } else {
111         filename = (char *) malloc(strlen(tmpdir) + addslash + 10);
112     }
113
114     /*
115      * And create the string.
116      */
117     (void) sprintf(filename, "%s%s%s%sa%05d", tmpdir, addslash ? "/" : "",
118                    pfx ? pfx : "", unique_letters, (int)getpid());
119
120     return filename;
121}
122
123
124#ifdef UNIX
125
126FILE *
127util_tmpfile(void)
128{
129    FILE *fp;
130    char *filename;
131
132    filename = util_tempnam(NIL(char), "VIS");
133    if ((fp = fopen(filename, "w+")) == NULL) {
134        FREE(filename);
135        return NULL;
136    }
137    (void) unlink(filename); 
138    FREE(filename);
139    return fp;
140}
141
142#else
143
144FILE *
145util_tmpfile(void)
146{
147    FILE *fp;
148
149    if ((fp = fopen("utiltmp", "w+")) == NULL) {
150        return NULL;
151    }
152    (void) unlink("utiltmp");
153    return fp;
154}
155
156#endif
Note: See TracBrowser for help on using the repository browser.