Create new common/pathstuff.[ch]
[deliverable/binutils-gdb.git] / gdb / common / pathstuff.c
1 /* Path manipulation routines for GDB and gdbserver.
2
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "common-defs.h"
21 #include "pathstuff.h"
22 #include "host-defs.h"
23 #include "filenames.h"
24 #include "gdb_tilde_expand.h"
25
26 /* See common/pathstuff.h. */
27
28 gdb::unique_xmalloc_ptr<char>
29 gdb_realpath (const char *filename)
30 {
31 /* On most hosts, we rely on canonicalize_file_name to compute
32 the FILENAME's realpath.
33
34 But the situation is slightly more complex on Windows, due to some
35 versions of GCC which were reported to generate paths where
36 backlashes (the directory separator) were doubled. For instance:
37 c:\\some\\double\\slashes\\dir
38 ... instead of ...
39 c:\some\double\slashes\dir
40 Those double-slashes were getting in the way when comparing paths,
41 for instance when trying to insert a breakpoint as follow:
42 (gdb) b c:/some/double/slashes/dir/foo.c:4
43 No source file named c:/some/double/slashes/dir/foo.c:4.
44 (gdb) b c:\some\double\slashes\dir\foo.c:4
45 No source file named c:\some\double\slashes\dir\foo.c:4.
46 To prevent this from happening, we need this function to always
47 strip those extra backslashes. While canonicalize_file_name does
48 perform this simplification, it only works when the path is valid.
49 Since the simplification would be useful even if the path is not
50 valid (one can always set a breakpoint on a file, even if the file
51 does not exist locally), we rely instead on GetFullPathName to
52 perform the canonicalization. */
53
54 #if defined (_WIN32)
55 {
56 char buf[MAX_PATH];
57 DWORD len = GetFullPathName (filename, MAX_PATH, buf, NULL);
58
59 /* The file system is case-insensitive but case-preserving.
60 So it is important we do not lowercase the path. Otherwise,
61 we might not be able to display the original casing in a given
62 path. */
63 if (len > 0 && len < MAX_PATH)
64 return gdb::unique_xmalloc_ptr<char> (xstrdup (buf));
65 }
66 #else
67 {
68 char *rp = canonicalize_file_name (filename);
69
70 if (rp != NULL)
71 return gdb::unique_xmalloc_ptr<char> (rp);
72 }
73 #endif
74
75 /* This system is a lost cause, just dup the buffer. */
76 return gdb::unique_xmalloc_ptr<char> (xstrdup (filename));
77 }
78
79 /* See common/pathstuff.h. */
80
81 gdb::unique_xmalloc_ptr<char>
82 gdb_realpath_keepfile (const char *filename)
83 {
84 const char *base_name = lbasename (filename);
85 char *dir_name;
86 char *result;
87
88 /* Extract the basename of filename, and return immediately
89 a copy of filename if it does not contain any directory prefix. */
90 if (base_name == filename)
91 return gdb::unique_xmalloc_ptr<char> (xstrdup (filename));
92
93 dir_name = (char *) alloca ((size_t) (base_name - filename + 2));
94 /* Allocate enough space to store the dir_name + plus one extra
95 character sometimes needed under Windows (see below), and
96 then the closing \000 character. */
97 strncpy (dir_name, filename, base_name - filename);
98 dir_name[base_name - filename] = '\000';
99
100 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
101 /* We need to be careful when filename is of the form 'd:foo', which
102 is equivalent of d:./foo, which is totally different from d:/foo. */
103 if (strlen (dir_name) == 2 && isalpha (dir_name[0]) && dir_name[1] == ':')
104 {
105 dir_name[2] = '.';
106 dir_name[3] = '\000';
107 }
108 #endif
109
110 /* Canonicalize the directory prefix, and build the resulting
111 filename. If the dirname realpath already contains an ending
112 directory separator, avoid doubling it. */
113 gdb::unique_xmalloc_ptr<char> path_storage = gdb_realpath (dir_name);
114 const char *real_path = path_storage.get ();
115 if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1]))
116 result = concat (real_path, base_name, (char *) NULL);
117 else
118 result = concat (real_path, SLASH_STRING, base_name, (char *) NULL);
119
120 return gdb::unique_xmalloc_ptr<char> (result);
121 }
122
123 /* See common/pathstuff.h. */
124
125 gdb::unique_xmalloc_ptr<char>
126 gdb_abspath (const char *path)
127 {
128 gdb_assert (path != NULL && path[0] != '\0');
129
130 if (path[0] == '~')
131 return gdb_tilde_expand_up (path);
132
133 if (IS_ABSOLUTE_PATH (path))
134 return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
135
136 /* Beware the // my son, the Emacs barfs, the botch that catch... */
137 return gdb::unique_xmalloc_ptr<char>
138 (concat (current_directory,
139 IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
140 ? "" : SLASH_STRING,
141 path, (char *) NULL));
142 }
This page took 0.033825 seconds and 4 git commands to generate.