2011-01-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgfortran / runtime / main.c
blob28247ca878ff19ded324b0a81f389fe03d2d066f
1 /* Copyright (C) 2002-2003, 2005, 2007, 2009 Free Software Foundation, Inc.
2 Contributed by Andy Vaught and Paul Brook <paul@nowt.org>
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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 3, or (at your option)
9 any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 /* Stupid function to be sure the constructor is always linked in, even
36 in the case of static linking. See PR libfortran/22298 for details. */
37 void
38 stupid_function_name_for_static_linking (void)
40 return;
43 /* This will be 0 for little-endian
44 machines and 1 for big-endian machines. */
45 int big_endian = 0;
48 /* Figure out endianness for this machine. */
50 static void
51 determine_endianness (void)
53 union
55 GFC_LOGICAL_8 l8;
56 GFC_LOGICAL_4 l4[2];
57 } u;
59 u.l8 = 1;
60 if (u.l4[0])
61 big_endian = 0;
62 else if (u.l4[1])
63 big_endian = 1;
64 else
65 runtime_error ("Unable to determine machine endianness");
69 static int argc_save;
70 static char **argv_save;
72 static const char *exe_path;
73 static int please_free_exe_path_when_done;
75 /* Save the path under which the program was called, for use in the
76 backtrace routines. */
77 void
78 store_exe_path (const char * argv0)
80 #ifndef PATH_MAX
81 #define PATH_MAX 1024
82 #endif
84 #ifndef DIR_SEPARATOR
85 #define DIR_SEPARATOR '/'
86 #endif
88 char buf[PATH_MAX], *cwd, *path;
90 /* This can only happen if store_exe_path is called multiple times. */
91 if (please_free_exe_path_when_done)
92 free ((char *) exe_path);
94 /* On the simulator argv is not set. */
95 if (argv0 == NULL || argv0[0] == '/')
97 exe_path = argv0;
98 please_free_exe_path_when_done = 0;
99 return;
102 memset (buf, 0, sizeof (buf));
103 #ifdef HAVE_GETCWD
104 cwd = getcwd (buf, sizeof (buf));
105 #else
106 cwd = "";
107 #endif
109 /* exe_path will be cwd + "/" + argv[0] + "\0" */
110 path = malloc (strlen (cwd) + 1 + strlen (argv0) + 1);
111 sprintf (path, "%s%c%s", cwd, DIR_SEPARATOR, argv0);
112 exe_path = path;
113 please_free_exe_path_when_done = 1;
117 /* Return the full path of the executable. */
118 char *
119 full_exe_path (void)
121 return (char *) exe_path;
125 /* Set the saved values of the command line arguments. */
127 void
128 set_args (int argc, char **argv)
130 argc_save = argc;
131 argv_save = argv;
132 store_exe_path (argv[0]);
134 iexport(set_args);
137 /* Retrieve the saved values of the command line arguments. */
139 void
140 get_args (int *argc, char ***argv)
142 *argc = argc_save;
143 *argv = argv_save;
147 /* Initialize the runtime library. */
149 static void __attribute__((constructor))
150 init (void)
152 /* Figure out the machine endianness. */
153 determine_endianness ();
155 /* Must be first */
156 init_variables ();
158 init_units ();
159 set_fpu ();
160 init_compile_options ();
162 #ifdef DEBUG
163 /* Check for special command lines. */
165 if (argc > 1 && strcmp (argv[1], "--help") == 0)
166 show_variables ();
168 /* if (argc > 1 && strcmp(argv[1], "--resume") == 0) resume(); */
169 #endif
171 random_seed_i4 (NULL, NULL, NULL);
175 /* Cleanup the runtime library. */
177 static void __attribute__((destructor))
178 cleanup (void)
180 close_units ();
182 if (please_free_exe_path_when_done)
183 free ((char *) exe_path);