1 /******************************** -*- C -*- ****************************
2 *
3 * System specific implementation module.
4 *
5 * This module contains implementations of various operating system
6 * specific routines. This module should encapsulate most (or all)
7 * of these calls so that the rest of the code is portable.
8 *
9 *
10 ***********************************************************************/
11
12 /***********************************************************************
13 *
14 * Copyright 1988,89,90,91,92,94,95,99,2000,2001,2002,2003,2006,2007,2008,2009
15 * Free Software Foundation, Inc.
16 * Written by Steve Byrne.
17 *
18 * This file is part of GNU Smalltalk.
19 *
20 * GNU Smalltalk is free software; you can redistribute it and/or modify it
21 * under the terms of the GNU General Public License as published by the Free
22 * Software Foundation; either version 2, or (at your option) any later
23 * version.
24 *
25 * Linking GNU Smalltalk statically or dynamically with other modules is
26 * making a combined work based on GNU Smalltalk. Thus, the terms and
27 * conditions of the GNU General Public License cover the whole
28 * combination.
29 *
30 * In addition, as a special exception, the Free Software Foundation
31 * give you permission to combine GNU Smalltalk with free software
32 * programs or libraries that are released under the GNU LGPL and with
33 * independent programs running under the GNU Smalltalk virtual machine.
34 *
35 * You may copy and distribute such a system following the terms of the
36 * GNU GPL for GNU Smalltalk and the licenses of the other code
37 * concerned, provided that you include the source code of that other
38 * code when and as the GNU GPL requires distribution of source code.
39 *
40 * Note that people who make modified versions of GNU Smalltalk are not
41 * obligated to grant this special exception for their modified
42 * versions; it is their choice whether to do so. The GNU General
43 * Public License gives permission to release a modified version without
44 * this exception; this exception also makes it possible to release a
45 * modified version which carries forward this exception.
46 *
47 * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
48 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
50 * more details.
51 *
52 * You should have received a copy of the GNU General Public License along with
53 * GNU Smalltalk; see the file COPYING. If not, write to the Free Software
54 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
55 *
56 ***********************************************************************/
57
58
59 #include "gstpriv.h"
60
61 #ifdef WIN32
62 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
63 # include <windows.h>
64 #endif
65
66 /* The path to the executable, derived from argv[0]. */
67 const char *_gst_executable_path = NULL;
68
69 /* Store the full pathname of the current executable. */
70 void
_gst_set_executable_path(const char * argv0)71 _gst_set_executable_path (const char *argv0)
72 {
73 char location[MAX_PATH];
74 static char location_as_posix_path[2 * MAX_PATH];
75 int length = GetModuleFileName (NULL, location, sizeof (location));
76 if (length <= 0)
77 return NULL;
78
79 /* On Cygwin, we need to convert paths coming from Win32 system calls
80 to the Unix-like slashified notation.
81
82 There's no error return defined for cygwin_conv_to_posix_path.
83 See cygwin-api/func-cygwin-conv-to-posix-path.html.
84 Does it overflow the buffer of expected size MAX_PATH or does it
85 truncate the path? I don't know. Let's catch both. */
86 cygwin_conv_to_posix_path (location, location_as_posix_path);
87 location_as_posix_path[MAX_PATH - 1] = '\0';
88 if (strlen (location_as_posix_path) >= MAX_PATH - 1)
89 /* A sign of buffer overflow or path truncation. */
90 _gst_executable_path = NULL;
91 else
92 _gst_executable_path = _gst_get_full_file_name (location_as_posix_path);
93 }
94