1 /* Utility to pick a temporary directory.
2 Copyright (C) 1996 Free Software Foundation, Inc.
4 This file is part of the libiberty library.
5 Libiberty is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 Libiberty is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with libiberty; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* This file exports one function: choose_temp_base. */
23 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
34 #include "gansidecl.h"
35 extern char *xmalloc ();
38 #include "libiberty.h"
39 #if defined (__MSDOS__) || defined (_WIN32)
40 #define DIR_SEPARATOR '\\'
45 #define DIR_SEPARATOR '/'
48 /* On MSDOS, write temp files in current dir
49 because there's no place else we can expect to use. */
50 /* ??? Although the current directory is tried as a last resort,
51 this is left in so that on MSDOS it is prefered to /tmp on the
52 off chance that someone requires this, since that was the previous
60 /* Name of temporary file.
61 mktemp requires 6 trailing X's. */
62 #define TEMP_FILE "ccXXXXXX"
64 /* Subroutine of choose_temp_base.
65 If BASE is non-NULL, returh it.
66 Otherwise it checks if DIR is a usable directory.
67 If success, DIR is returned.
68 Otherwise NULL is returned. */
77 && access (dir, R_OK | W_OK) == 0)
82 /* Return a prefix for temporary file names or NULL if unable to find one.
83 The current directory is chosen if all else fails so the program is
84 exited if a temporary directory can't be found (mktemp fails).
85 The buffer for the result is obtained with xmalloc. */
93 char usrtmp[sizeof ("/usr/tmp")],tmp[sizeof ("/tmp")];
96 base = try (getenv ("TMPDIR"), base);
97 base = try (getenv ("TMP"), base);
98 base = try (getenv ("TEMP"), base);
101 base = try (P_tmpdir, base);
104 /* Try /usr/tmp, then /tmp. */
105 sprintf (usrtmp, "%cusr%ctmp", DIR_SEPARATOR, DIR_SEPARATOR);
106 base = try (usrtmp, base);
107 sprintf (tmp, "%ctmp", DIR_SEPARATOR);
108 base = try (tmp, base);
110 /* If all else fails, use the current directory! */
121 temp_filename = xmalloc (len + 1 /*DIR_SEPARATOR*/
122 + strlen (TEMP_FILE) + 1);
123 strcpy (temp_filename, base);
126 if (temp_filename[len-1] != '/'
127 && temp_filename[len-1] != DIR_SEPARATOR)
128 temp_filename[len++] = DIR_SEPARATOR;
130 if (temp_filename[len-1] != ':')
131 temp_filename[len++] = ':';
133 strcpy (temp_filename + len, TEMP_FILE);
135 mktemp (temp_filename);
136 if (strlen (temp_filename) == 0)
138 return temp_filename;