~pac72/ubuntu/lucid/ddd/devel

« back to all changes in this revision

Viewing changes to libiberty/choose-temp.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Schepler
  • Date: 2004-07-22 03:49:37 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040722034937-cysl08t1jvba4jrx
Tags: 1:3.3.9-3
USERINFO has been renamed to USERINFO.txt; adjust debian/rules code
to match, to get correct information on the About DDD dialog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
18
Boston, MA 02111-1307, USA.  */
19
19
 
20
 
/* This file exports two functions: choose_temp_base and make_temp_file.  */
21
 
 
22
20
#ifdef HAVE_CONFIG_H
23
21
#include "config.h"
24
22
#endif
25
23
 
26
24
#include <stdio.h>      /* May get P_tmpdir.  */
27
 
#include <sys/types.h>
28
 
#ifdef HAVE_UNISTD_H
29
 
#include <unistd.h>
30
 
#endif
31
25
#ifdef HAVE_STDLIB_H
32
26
#include <stdlib.h>
33
27
#endif
34
28
#ifdef HAVE_STRING_H
35
29
#include <string.h>
36
30
#endif
37
 
#ifdef HAVE_SYS_FILE_H
38
 
#include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
39
 
#endif
40
 
 
41
 
#ifndef R_OK
42
 
#define R_OK 4
43
 
#define W_OK 2
44
 
#define X_OK 1
45
 
#endif
46
31
 
47
32
#include "libiberty.h"
48
 
extern int mkstemps ();
49
 
 
50
 
#ifndef IN_GCC
51
 
#if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN__) && ! defined (_UWIN))
52
 
#define DIR_SEPARATOR '\\'
53
 
#endif
54
 
#endif
55
 
 
56
 
#ifndef DIR_SEPARATOR
57
 
#define DIR_SEPARATOR '/'
58
 
#endif
59
 
 
60
 
/* On MSDOS, write temp files in current dir
61
 
   because there's no place else we can expect to use.  */
62
 
/* ??? Although the current directory is tried as a last resort,
63
 
   this is left in so that on MSDOS it is preferred to /tmp on the
64
 
   off chance that someone requires this, since that was the previous
65
 
   behaviour.  */
66
 
#ifdef __MSDOS__
67
 
#ifndef P_tmpdir
68
 
#define P_tmpdir "."
69
 
#endif
70
 
#endif
 
33
extern char *choose_tmpdir PARAMS ((void));
71
34
 
72
35
/* Name of temporary file.
73
36
   mktemp requires 6 trailing X's.  */
74
37
#define TEMP_FILE "ccXXXXXX"
75
 
 
76
 
/* Subroutine of choose_temp_base.
77
 
   If BASE is non-NULL, return it.
78
 
   Otherwise it checks if DIR is a usable directory.
79
 
   If success, DIR is returned.
80
 
   Otherwise NULL is returned.  */
81
 
 
82
 
static char *
83
 
try (dir, base)
84
 
     char *dir, *base;
85
 
{
86
 
  if (base != 0)
87
 
    return base;
88
 
  if (dir != 0
89
 
      && access (dir, R_OK | W_OK | X_OK) == 0)
90
 
    return dir;
91
 
  return 0;
92
 
}
93
 
 
94
 
/* Return a prefix for temporary file names or NULL if unable to find one.
95
 
   The current directory is chosen if all else fails so the program is
96
 
   exited if a temporary directory can't be found (mktemp fails).
97
 
   The buffer for the result is obtained with xmalloc. 
98
 
 
99
 
   This function is provided for backwards compatability only.  It use
100
 
   is not recommended.  */
 
38
#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
 
39
 
 
40
/*
 
41
 
 
42
@deftypefn Extension char* choose_temp_base (void)
 
43
 
 
44
Return a prefix for temporary file names or @code{NULL} if unable to
 
45
find one.  The current directory is chosen if all else fails so the
 
46
program is exited if a temporary directory can't be found (@code{mktemp}
 
47
fails).  The buffer for the result is obtained with @code{xmalloc}.
 
48
 
 
49
This function is provided for backwards compatability only.  Its use is
 
50
not recommended.
 
51
 
 
52
@end deftypefn
 
53
 
 
54
*/
101
55
 
102
56
char *
103
57
choose_temp_base ()
104
58
{
105
 
  char *base = 0;
 
59
  const char *base = choose_tmpdir ();
106
60
  char *temp_filename;
107
61
  int len;
108
 
  static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
109
 
  static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
110
 
 
111
 
  base = try (getenv ("TMPDIR"), base);
112
 
  base = try (getenv ("TMP"), base);
113
 
  base = try (getenv ("TEMP"), base);
114
 
 
115
 
#ifdef P_tmpdir
116
 
  base = try (P_tmpdir, base);
117
 
#endif
118
 
 
119
 
  /* Try /usr/tmp, then /tmp.  */
120
 
  base = try (usrtmp, base);
121
 
  base = try (tmp, base);
122
 
 
123
 
  /* If all else fails, use the current directory!  */
124
 
  if (base == 0)
125
 
    base = ".";
126
62
 
127
63
  len = strlen (base);
128
 
  temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
129
 
                           + strlen (TEMP_FILE) + 1);
 
64
  temp_filename = xmalloc (len + TEMP_FILE_LEN + 1);
130
65
  strcpy (temp_filename, base);
131
 
 
132
 
  if (len != 0
133
 
      && temp_filename[len-1] != '/'
134
 
      && temp_filename[len-1] != DIR_SEPARATOR)
135
 
    temp_filename[len++] = DIR_SEPARATOR;
136
66
  strcpy (temp_filename + len, TEMP_FILE);
137
67
 
138
68
  mktemp (temp_filename);
140
70
    abort ();
141
71
  return temp_filename;
142
72
}
143
 
/* Return a temporary file name (as a string) or NULL if unable to create
144
 
   one.  */
145
 
 
146
 
char *
147
 
make_temp_file (suffix)
148
 
     const char *suffix;
149
 
{
150
 
  char *base = 0;
151
 
  char *temp_filename;
152
 
  int base_len, suffix_len;
153
 
  int fd;
154
 
  static char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
155
 
  static char usrtmp[] = { DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
156
 
 
157
 
  base = try (getenv ("TMPDIR"), base);
158
 
  base = try (getenv ("TMP"), base);
159
 
  base = try (getenv ("TEMP"), base);
160
 
 
161
 
#ifdef P_tmpdir
162
 
  base = try (P_tmpdir, base);
163
 
#endif
164
 
 
165
 
  /* Try /usr/tmp, then /tmp.  */
166
 
  base = try (usrtmp, base);
167
 
  base = try (tmp, base);
168
 
 
169
 
  /* If all else fails, use the current directory!  */
170
 
  if (base == 0)
171
 
    base = ".";
172
 
 
173
 
  base_len = strlen (base);
174
 
 
175
 
  if (suffix)
176
 
    suffix_len = strlen (suffix);
177
 
  else
178
 
    suffix_len = 0;
179
 
 
180
 
  temp_filename = xmalloc (base_len + 1 /*DIR_SEPARATOR*/
181
 
                           + strlen (TEMP_FILE)
182
 
                           + suffix_len + 1);
183
 
  strcpy (temp_filename, base);
184
 
 
185
 
  if (base_len != 0
186
 
      && temp_filename[base_len-1] != '/'
187
 
      && temp_filename[base_len-1] != DIR_SEPARATOR)
188
 
    temp_filename[base_len++] = DIR_SEPARATOR;
189
 
  strcpy (temp_filename + base_len, TEMP_FILE);
190
 
 
191
 
  if (suffix)
192
 
    strcat (temp_filename, suffix);
193
 
 
194
 
  fd = mkstemps (temp_filename, suffix_len);
195
 
  /* If mkstemps failed, then something bad is happening.  Maybe we should
196
 
     issue a message about a possible security attack in progress?  */
197
 
  if (fd == -1)
198
 
    abort ();
199
 
  /* Similarly if we can not close the file.  */
200
 
  if (close (fd))
201
 
    abort ();
202
 
  return temp_filename;
203
 
}