1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* gui_string_util.c - String utilities used by the GUI system. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008,2009 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/gui_string_util.h>
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
/* Create a new NUL-terminated string on the heap as a substring of BUF.
The range of buf included is the half-open interval [START,END).
The index START is inclusive, END is exclusive. */
char *
grub_new_substring (const char *buf,
grub_size_t start, grub_size_t end)
{
if (end < start)
return 0;
grub_size_t len = end - start;
char *s = grub_malloc (len + 1);
if (! s)
return 0;
grub_memcpy (s, buf + start, len);
s[len] = '\0';
return s;
}
/* Eliminate "." and ".." path elements from PATH. A new heap-allocated
string is returned. */
static char *
canonicalize_path (const char *path)
{
int i;
const char *p;
char *newpath = 0;
/* Count the path components in path. */
int components = 1;
for (p = path; *p; p++)
if (*p == '/')
components++;
char **path_array = grub_calloc (components, sizeof (*path_array));
if (! path_array)
return 0;
/* Initialize array elements to NULL pointers; in case once of the
allocations fails, the cleanup code can just call grub_free() for all
pointers in the array. */
for (i = 0; i < components; i++)
path_array[i] = 0;
/* Parse the path into path_array. */
p = path;
for (i = 0; i < components && p; i++)
{
/* Find the end of the path element. */
const char *end = grub_strchr (p, '/');
if (!end)
end = p + grub_strlen (p);
/* Copy the element. */
path_array[i] = grub_new_substring (p, 0, end - p);
if (! path_array[i])
goto cleanup;
/* Advance p to point to the start of the next element, or NULL. */
if (*end)
p = end + 1;
else
p = 0;
}
/* Eliminate '.' and '..' elements from the path array. */
int newpath_length = 0;
for (i = components - 1; i >= 0; --i)
{
if (! grub_strcmp (path_array[i], "."))
{
grub_free (path_array[i]);
path_array[i] = 0;
}
else if (! grub_strcmp (path_array[i], "..")
&& i > 0)
{
/* Delete the '..' and the prior path element. */
grub_free (path_array[i]);
path_array[i] = 0;
--i;
grub_free (path_array[i]);
path_array[i] = 0;
}
else
{
newpath_length += grub_strlen (path_array[i]) + 1;
}
}
/* Construct a new path string. */
newpath = grub_malloc (newpath_length + 1);
if (! newpath)
goto cleanup;
newpath[0] = '\0';
char *newpath_end = newpath;
int first = 1;
for (i = 0; i < components; i++)
{
char *element = path_array[i];
if (element)
{
/* For all components but the first, prefix with a slash. */
if (! first)
newpath_end = grub_stpcpy (newpath_end, "/");
newpath_end = grub_stpcpy (newpath_end, element);
first = 0;
}
}
cleanup:
for (i = 0; i < components; i++)
grub_free (path_array[i]);
grub_free (path_array);
return newpath;
}
/* Return a new heap-allocated string representing to absolute path
to the file referred to by PATH. If PATH is an absolute path, then
the returned path is a copy of PATH. If PATH is a relative path, then
BASE is with PATH used to construct the absolute path. */
char *
grub_resolve_relative_path (const char *base, const char *path)
{
char *abspath;
char *canonpath;
char *p;
grub_size_t l;
/* If PATH is an absolute path, then just use it as is. */
if (path[0] == '/' || path[0] == '(')
return canonicalize_path (path);
abspath = grub_malloc (grub_strlen (base) + grub_strlen (path) + 3);
if (! abspath)
return 0;
/* Concatenate BASE and PATH. */
p = grub_stpcpy (abspath, base);
l = grub_strlen (abspath);
if (l == 0 || abspath[l-1] != '/')
{
*p = '/';
p++;
*p = 0;
}
grub_stpcpy (p, path);
canonpath = canonicalize_path (abspath);
if (! canonpath)
return abspath;
grub_free (abspath);
return canonpath;
}
/* Get the path of the directory where the file at FILE_PATH is located.
FILE_PATH should refer to a file, not a directory. The returned path
includes a trailing slash.
This does not handle GRUB "(hd0,0)" paths properly yet since it only
looks at slashes. */
char *
grub_get_dirname (const char *file_path)
{
int i;
int last_slash;
last_slash = -1;
for (i = grub_strlen (file_path) - 1; i >= 0; --i)
{
if (file_path[i] == '/')
{
last_slash = i;
break;
}
}
if (last_slash == -1)
return grub_strdup ("/");
return grub_new_substring (file_path, 0, last_slash + 1);
}
|