OSDN Git Service

* runtime/main.c (store_exe_path): Don't crash if argv0 is NULL.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / main.c
1 /* Copyright (C) 2002-2003, 2005, 2007 Free Software Foundation, Inc.
2    Contributed by Andy Vaught and Paul Brook <paul@nowt.org>
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file.  (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
18 executable.)
19
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING.  If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA.  */
29
30 #include "libgfortran.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 /* Stupid function to be sure the constructor is always linked in, even
41    in the case of static linking.  See PR libfortran/22298 for details.  */
42 void
43 stupid_function_name_for_static_linking (void)
44 {
45   return;
46 }
47
48 /* This will be 0 for little-endian
49    machines and 1 for big-endian machines.  */
50 int big_endian = 0;
51
52
53 /* Figure out endianness for this machine.  */
54
55 static void
56 determine_endianness (void)
57 {
58   union
59   {
60     GFC_LOGICAL_8 l8;
61     GFC_LOGICAL_4 l4[2];
62   } u;
63
64   u.l8 = 1;
65   if (u.l4[0])
66     big_endian = 0;
67   else if (u.l4[1])
68     big_endian = 1;
69   else
70     runtime_error ("Unable to determine machine endianness");
71 }
72
73
74 static int argc_save;
75 static char **argv_save;
76
77 /* Set the saved values of the command line arguments.  */
78
79 void
80 set_args (int argc, char **argv)
81 {
82   argc_save = argc;
83   argv_save = argv;
84 }
85
86 /* Retrieve the saved values of the command line arguments.  */
87
88 void
89 get_args (int *argc, char ***argv)
90 {
91   *argc = argc_save;
92   *argv = argv_save;
93 }
94
95
96 static const char *exe_path;
97 static int please_free_exe_path_when_done;
98
99 /* Save the path under which the program was called, for use in the
100    backtrace routines.  */
101 void
102 store_exe_path (const char * argv0)
103 {
104 #ifndef PATH_MAX
105 #define PATH_MAX 1024
106 #endif
107
108 #ifndef DIR_SEPARATOR   
109 #define DIR_SEPARATOR '/'
110 #endif
111
112   char buf[PATH_MAX], *cwd, *path;
113
114   /* On the simulator argv is not set.  */
115   if (argv0 == NULL || argv0[0] == '/')
116     {
117       exe_path = argv0;
118       please_free_exe_path_when_done = 0;
119       return;
120     }
121
122   memset (buf, 0, sizeof (buf));
123 #ifdef HAVE_GETCWD
124   cwd = getcwd (buf, sizeof (buf));
125 #else
126   cwd = "";
127 #endif
128
129   /* exe_path will be cwd + "/" + argv[0] + "\0" */
130   path = malloc (strlen (cwd) + 1 + strlen (argv0) + 1);
131   sprintf (path, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
132   exe_path = path;
133   please_free_exe_path_when_done = 1;
134 }
135
136 /* Return the full path of the executable.  */
137 char *
138 full_exe_path (void)
139 {
140   return (char *) exe_path;
141 }
142
143 /* Initialize the runtime library.  */
144
145 static void __attribute__((constructor))
146 init (void)
147 {
148   /* Figure out the machine endianness.  */
149   determine_endianness ();
150
151   /* Must be first */
152   init_variables ();
153
154   init_units ();
155   set_fpu ();
156   init_compile_options ();
157
158 #ifdef DEBUG
159   /* Check for special command lines.  */
160
161   if (argc > 1 && strcmp (argv[1], "--help") == 0)
162     show_variables ();
163
164   /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume();  */
165 #endif
166
167   random_seed_i4 (NULL, NULL, NULL);
168 }
169
170
171 /* Cleanup the runtime library.  */
172
173 static void __attribute__((destructor))
174 cleanup (void)
175 {
176   close_units ();
177   
178   if (please_free_exe_path_when_done)
179     free ((char *) exe_path);
180 }