Subversion Repositories Kolibri OS

Rev

Rev 5217 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5217 Rev 6324
1
/* Utility to pick a temporary filename prefix.
1
/* Utility to pick a temporary filename prefix.
2
   Copyright (C) 1996, 1997, 1998, 2001, 2009, 2010
2
   Copyright (C) 1996, 1997, 1998, 2001, 2009, 2010
3
   Free Software Foundation, Inc.
3
   Free Software Foundation, Inc.
4
 
4
 
5
This file is part of the libiberty library.
5
This file is part of the libiberty library.
6
Libiberty is free software; you can redistribute it and/or
6
Libiberty is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
9
version 2 of the License, or (at your option) any later version.
10
 
10
 
11
Libiberty is distributed in the hope that it will be useful,
11
Libiberty is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
Library General Public License for more details.
14
Library General Public License for more details.
15
 
15
 
16
You should have received a copy of the GNU Library General Public
16
You should have received a copy of the GNU Library General Public
17
License along with libiberty; see the file COPYING.LIB.  If not,
17
License along with libiberty; see the file COPYING.LIB.  If not,
18
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19
Boston, MA 02110-1301, USA.  */
19
Boston, MA 02110-1301, USA.  */
20
 
20
 
21
#ifdef HAVE_CONFIG_H
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
22
#include "config.h"
23
#endif
23
#endif
24
 
24
 
25
#include 	/* May get P_tmpdir.  */
25
#include 	/* May get P_tmpdir.  */
26
#include 
26
#include 
27
#include 
27
#include 
28
#ifdef HAVE_UNISTD_H
28
#ifdef HAVE_UNISTD_H
29
#include 
29
#include 
30
#endif
30
#endif
31
#ifdef HAVE_STDLIB_H
31
#ifdef HAVE_STDLIB_H
32
#include 
32
#include 
33
#endif
33
#endif
34
#ifdef HAVE_STRING_H
34
#ifdef HAVE_STRING_H
35
#include 
35
#include 
36
#endif
36
#endif
37
#ifdef HAVE_SYS_FILE_H
37
#ifdef HAVE_SYS_FILE_H
38
#include    /* May get R_OK, etc. on some systems.  */
38
#include    /* May get R_OK, etc. on some systems.  */
39
#endif
39
#endif
40
#if defined(_WIN32) && !defined(__CYGWIN__)
40
#if defined(_WIN32) && !defined(__CYGWIN__)
41
#include 
41
#include 
42
#endif
42
#endif
43
 
43
 
44
#ifndef R_OK
44
#ifndef R_OK
45
#define R_OK 4
45
#define R_OK 4
46
#define W_OK 2
46
#define W_OK 2
47
#define X_OK 1
47
#define X_OK 1
48
#endif
48
#endif
49
 
49
 
50
#include "libiberty.h"
50
#include "libiberty.h"
51
extern int mkstemps (char *, int);
51
extern int mkstemps (char *, int);
52
 
52
 
53
/* '/' works just fine on MS-DOS based systems.  */
53
/* '/' works just fine on MS-DOS based systems.  */
54
#ifndef DIR_SEPARATOR
54
#ifndef DIR_SEPARATOR
55
#define DIR_SEPARATOR '/'
55
#define DIR_SEPARATOR '/'
56
#endif
56
#endif
57
 
57
 
58
/* Name of temporary file.
58
/* Name of temporary file.
59
   mktemp requires 6 trailing X's.  */
59
   mktemp requires 6 trailing X's.  */
60
#define TEMP_FILE "ccXXXXXX"
60
#define TEMP_FILE "ccXXXXXX"
61
#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
61
#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
62
 
62
 
63
#if !defined(_WIN32) || defined(__CYGWIN__)
63
#if !defined(_WIN32) || defined(__CYGWIN__)
64
 
64
 
65
/* Subroutine of choose_tmpdir.
65
/* Subroutine of choose_tmpdir.
66
   If BASE is non-NULL, return it.
66
   If BASE is non-NULL, return it.
67
   Otherwise it checks if DIR is a usable directory.
67
   Otherwise it checks if DIR is a usable directory.
68
   If success, DIR is returned.
68
   If success, DIR is returned.
69
   Otherwise NULL is returned.  */
69
   Otherwise NULL is returned.  */
70
 
70
 
71
static inline const char *try_dir (const char *, const char *);
71
static inline const char *try_dir (const char *, const char *);
72
 
72
 
73
static inline const char *
73
static inline const char *
74
try_dir (const char *dir, const char *base)
74
try_dir (const char *dir, const char *base)
75
{
75
{
76
  if (base != 0)
76
  if (base != 0)
77
    return base;
77
    return base;
78
  if (dir != 0
78
  if (dir != 0
79
      && access (dir, R_OK | W_OK | X_OK) == 0)
79
      && access (dir, R_OK | W_OK | X_OK) == 0)
80
    return dir;
80
    return dir;
81
  return 0;
81
  return 0;
82
}
82
}
83
 
83
 
84
static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
84
static const char tmp[] = { DIR_SEPARATOR, 't', 'm', 'p', 0 };
85
static const char usrtmp[] =
85
static const char usrtmp[] =
86
{ DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
86
{ DIR_SEPARATOR, 'u', 's', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
87
static const char vartmp[] =
87
static const char vartmp[] =
88
{ DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
88
{ DIR_SEPARATOR, 'v', 'a', 'r', DIR_SEPARATOR, 't', 'm', 'p', 0 };
89
 
89
 
90
#endif
90
#endif
91
 
91
 
92
//static char *memoized_tmpdir;
92
//static char *memoized_tmpdir;
93
 
93
 
94
/*
94
/*
95
 
95
 
96
@deftypefn Replacement char* choose_tmpdir ()
96
@deftypefn Replacement const char* choose_tmpdir ()
97
 
97
 
98
Returns a pointer to a directory path suitable for creating temporary
98
Returns a pointer to a directory path suitable for creating temporary
99
files in.
99
files in.
100
 
100
 
101
@end deftypefn
101
@end deftypefn
102
 
102
 
103
*/
103
*/
104
 
104
 
105
char *
105
const char *
106
choose_tmpdir (void)
106
choose_tmpdir (void)
107
{
107
{
108
  return "/tmp0/1/";
108
  return "/tmp0/1/";
109
}
109
}
110
 
110
 
111
/*
111
/*
112
 
112
 
113
@deftypefn Replacement char* make_temp_file (const char *@var{suffix})
113
@deftypefn Replacement char* make_temp_file (const char *@var{suffix})
114
 
114
 
115
Return a temporary file name (as a string) or @code{NULL} if unable to
115
Return a temporary file name (as a string) or @code{NULL} if unable to
116
create one.  @var{suffix} is a suffix to append to the file name.  The
116
create one.  @var{suffix} is a suffix to append to the file name.  The
117
string is @code{malloc}ed, and the temporary file has been created.
117
string is @code{malloc}ed, and the temporary file has been created.
118
 
118
 
119
@end deftypefn
119
@end deftypefn
120
 
120
 
121
*/
121
*/
122
 
122
 
123
char *
123
char *
124
make_temp_file (const char *suffix)
124
make_temp_file (const char *suffix)
125
{
125
{
126
  const char *base = choose_tmpdir ();
126
  const char *base = choose_tmpdir ();
127
  char *temp_filename;
127
  char *temp_filename;
128
  int base_len, suffix_len;
128
  int base_len, suffix_len;
129
  int fd;
129
  int fd;
130
 
130
 
131
  if (suffix == 0)
131
  if (suffix == 0)
132
    suffix = "";
132
    suffix = "";
133
 
133
 
134
  base_len = strlen (base);
134
  base_len = strlen (base);
135
  suffix_len = strlen (suffix);
135
  suffix_len = strlen (suffix);
136
 
136
 
137
  temp_filename = XNEWVEC (char, base_len
137
  temp_filename = XNEWVEC (char, base_len
138
			   + TEMP_FILE_LEN
138
			   + TEMP_FILE_LEN
139
			   + suffix_len + 1);
139
			   + suffix_len + 1);
140
  strcpy (temp_filename, base);
140
  strcpy (temp_filename, base);
141
  strcpy (temp_filename + base_len, TEMP_FILE);
141
  strcpy (temp_filename + base_len, TEMP_FILE);
142
  strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
142
  strcpy (temp_filename + base_len + TEMP_FILE_LEN, suffix);
143
 
143
 
144
  fd = mkstemps (temp_filename, suffix_len);
144
  fd = mkstemps (temp_filename, suffix_len);
145
  /* Mkstemps failed.  It may be EPERM, ENOSPC etc.  */
145
  /* Mkstemps failed.  It may be EPERM, ENOSPC etc.  */
146
  if (fd == -1)
146
  if (fd == -1)
147
    {
147
    {
148
      fprintf (stderr, "Cannot create temporary file in %s: %s\n",
148
      fprintf (stderr, "Cannot create temporary file in %s: %s\n",
149
	       base, strerror (errno));
149
	       base, strerror (errno));
150
      abort ();
150
      abort ();
151
    }
151
    }
152
  /* We abort on failed close out of sheer paranoia.  */
152
  /* We abort on failed close out of sheer paranoia.  */
153
  if (close (fd))
153
  if (close (fd))
154
    abort ();
154
    abort ();
155
  return temp_filename;
155
  return temp_filename;
156
}
156
}