OSDN Git Service

2007-07-29 Thomas Koenig <tkoenig@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / main.c
1 /* Copyright (C) 2002-2003, 2005 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 "config.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35
36 #include "libgfortran.h"
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 /* Stupid function to be sure the constructor is always linked in, even
43    in the case of static linking.  See PR libfortran/22298 for details.  */
44 void
45 stupid_function_name_for_static_linking (void)
46 {
47   return;
48 }
49
50 /* This is the offset (in bytes) required to cast from logical(8)* to
51    logical(4)*. and still get the same result.  Will be 0 for little-endian
52    machines and 4 for big-endian machines.  */
53 int l8_to_l4_offset = 0;
54
55
56 /* Figure out endianness for this machine.  */
57
58 static void
59 determine_endianness (void)
60 {
61   union
62   {
63     GFC_LOGICAL_8 l8;
64     GFC_LOGICAL_4 l4[2];
65   } u;
66
67   u.l8 = 1;
68   if (u.l4[0])
69     l8_to_l4_offset = 0;
70   else if (u.l4[1])
71     l8_to_l4_offset = 1;
72   else
73     runtime_error ("Unable to determine machine endianness");
74 }
75
76
77 static int argc_save;
78 static char **argv_save;
79
80 /* Set the saved values of the command line arguments.  */
81
82 void
83 set_args (int argc, char **argv)
84 {
85   argc_save = argc;
86   argv_save = argv;
87 }
88
89 /* Retrieve the saved values of the command line arguments.  */
90
91 void
92 get_args (int *argc, char ***argv)
93 {
94   *argc = argc_save;
95   *argv = argv_save;
96 }
97
98
99 static const char *exe_path;
100 static int please_free_exe_path_when_done;
101
102 /* Save the path under which the program was called, for use in the
103    backtrace routines.  */
104 void
105 store_exe_path (const char * argv0)
106 {
107 #ifndef PATH_MAX
108 #define PATH_MAX 1024
109 #endif
110
111 #ifndef DIR_SEPARATOR   
112 #define DIR_SEPARATOR '/'
113 #endif
114
115   char buf[PATH_MAX], *cwd, *path;
116
117   if (argv0[0] == '/')
118     {
119       exe_path = argv0;
120       please_free_exe_path_when_done = 0;
121       return;
122     }
123
124   memset (buf, 0, sizeof (buf));
125   cwd = getcwd (buf, sizeof (buf));
126
127   /* exe_path will be cwd + "/" + argv[0] + "\0" */
128   path = malloc (strlen (cwd) + 1 + strlen (argv0) + 1);
129   sprintf (path, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
130   exe_path = path;
131   please_free_exe_path_when_done = 1;
132 }
133
134 /* Return the full path of the executable.  */
135 char *
136 full_exe_path (void)
137 {
138   return (char *) exe_path;
139 }
140
141 /* Initialize the runtime library.  */
142
143 static void __attribute__((constructor))
144 init (void)
145 {
146   /* Figure out the machine endianness.  */
147   determine_endianness ();
148
149   /* Must be first */
150   init_variables ();
151
152   init_units ();
153   set_fpu ();
154   init_compile_options ();
155
156 #ifdef DEBUG
157   /* Check for special command lines.  */
158
159   if (argc > 1 && strcmp (argv[1], "--help") == 0)
160     show_variables ();
161
162   /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume();  */
163 #endif
164
165   random_seed(NULL,NULL,NULL);
166 }
167
168
169 /* Cleanup the runtime library.  */
170
171 static void __attribute__((destructor))
172 cleanup (void)
173 {
174   close_units ();
175   
176   if (please_free_exe_path_when_done)
177     free (exe_path);
178 }