OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / tempdir.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              T E M P D I R                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2003-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
27
28 with Hostparm; use Hostparm;
29 with Opt;      use Opt;
30 with Output;   use Output;
31
32 package body Tempdir is
33
34    Tmpdir_Needs_To_Be_Displayed : Boolean := True;
35
36    Tmpdir    : constant String := "TMPDIR";
37    Gnutmpdir : constant String := "GNUTMPDIR";
38    No_Dir    : aliased String  := "";
39    Temp_Dir  : String_Access   := No_Dir'Access;
40
41    ----------------------
42    -- Create_Temp_File --
43    ----------------------
44
45    procedure Create_Temp_File
46      (FD   : out File_Descriptor;
47       Name : out Path_Name_Type)
48    is
49       File_Name : String_Access;
50       Current_Dir : constant String := Get_Current_Dir;
51
52       function Directory return String;
53       --  Returns Temp_Dir.all if not empty, else return current directory
54
55       ---------------
56       -- Directory --
57       ---------------
58
59       function Directory return String is
60       begin
61          if Temp_Dir'Length /= 0 then
62             return Temp_Dir.all;
63
64          else
65             return Current_Dir;
66          end if;
67       end Directory;
68
69    --  Start of processing Tempdir
70
71    begin
72       if Temp_Dir'Length /= 0 then
73
74          --  In verbose mode, display once the value of TMPDIR, so that
75          --  if temp files cannot be created, it is easier to understand
76          --  where temp files are supposed to be created.
77
78          if Verbose_Mode and then Tmpdir_Needs_To_Be_Displayed then
79             Write_Str ("TMPDIR = """);
80             Write_Str (Temp_Dir.all);
81             Write_Line ("""");
82             Tmpdir_Needs_To_Be_Displayed := False;
83          end if;
84
85          --  Change directory to TMPDIR before creating the temp file,
86          --  then change back immediately to the previous directory.
87
88          Change_Dir (Temp_Dir.all);
89          Create_Temp_File (FD, File_Name);
90          Change_Dir (Current_Dir);
91
92       else
93          Create_Temp_File (FD, File_Name);
94       end if;
95
96       if FD = Invalid_FD then
97          Name := No_Path;
98
99       else
100          declare
101             Path_Name : constant String :=
102                           Normalize_Pathname
103                             (Directory & Directory_Separator & File_Name.all);
104
105          begin
106             Name_Len := Path_Name'Length;
107             Name_Buffer (1 .. Name_Len) := Path_Name;
108             Name := Name_Find;
109             Free (File_Name);
110          end;
111       end if;
112    end Create_Temp_File;
113
114 --  Start of elaboration for package Tempdir
115
116 begin
117    declare
118       Dir : String_Access;
119
120    begin
121       --  On VMS, if GNUTMPDIR is defined, use it
122
123       if OpenVMS then
124          Dir := Getenv (Gnutmpdir);
125
126          --  Otherwise, if GNUTMPDIR is not defined, try TMPDIR
127
128          if Dir'Length = 0 then
129             Dir := Getenv (Tmpdir);
130          end if;
131
132       else
133          Dir := Getenv (Tmpdir);
134       end if;
135
136       if Dir'Length > 0 and then
137         Is_Absolute_Path (Dir.all) and then
138         Is_Directory (Dir.all)
139       then
140          Temp_Dir := new String'(Normalize_Pathname (Dir.all));
141       end if;
142
143       Free (Dir);
144    end;
145 end Tempdir;