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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/* stringutils.c - String helper functions.
* Copyright (C) 1997, 2014 Werner Koch
* Copyright (C) 2020 g10 Code GmbH
*
* This file is part of libgpg-error.
*
* libgpg-error is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* libgpg-error 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <config.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#ifdef HAVE_STAT
# include <sys/stat.h>
#endif
#include <sys/types.h>
#include "gpgrt-int.h"
char *
_gpgrt_trim_spaces (char *str)
{
char *string, *p, *mark;
string = str;
/* Find first non space character. */
for (p=string; *p && isspace (*(unsigned char*)p) ; p++)
;
/* Move characters. */
for ((mark = NULL); (*string = *p); string++, p++)
if (isspace (*(unsigned char*)p))
{
if (!mark)
mark = string;
}
else
mark = NULL;
if (mark)
*mark = '\0' ; /* Remove trailing spaces. */
return str ;
}
/* Helper for _gpgrt_fnameconcat. FLAGS controls special features of
* the function. See GPGRT_FCONCAT_ constants for details. */
char *
_gpgrt_vfnameconcat (unsigned int flags,
const char *first_part, va_list arg_ptr)
{
const char *argv[32];
int argc;
size_t n;
int skip = 1; /* Characters to skip from FIRST_PART. Only used if
* HOME is NULL */
char *home_buffer = NULL;
char *name, *p;
const char *home;
const char *extradelim = "";
/* Put all args into an array because we need to scan them twice. */
n = strlen (first_part) + 1;
argc = 0;
while ((argv[argc] = va_arg (arg_ptr, const char *)))
{
n += strlen (argv[argc]) + 1;
if (argc >= DIM (argv)-1)
{
_gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
}
n++;
home = NULL;
if ((flags & GPGRT_FCONCAT_SYSCONF))
{
#ifdef HAVE_W32_SYSTEM
home = _gpgrt_w32_get_sysconfdir ();
if (!home)
return NULL;
#else
home = SYSCONFDIR;
#endif
skip = 0;
n += strlen (home);
if (n && home[n-1] != '/' && *first_part != '/')
{
extradelim = "/";
n++;
}
}
else if (*first_part == '~' && (flags & GPGRT_FCONCAT_TILDE))
{
if (first_part[1] == '/' || !first_part[1])
{
/* This is the "~/" or "~" case. */
#ifdef HAVE_W32_SYSTEM
home = _gpgrt_w32_get_profile ();
/* Note that we fallback to $HOME if the above failed. */
#endif
if (!home)
{
home_buffer = _gpgrt_getenv ("HOME");
if (!home_buffer)
home_buffer = _gpgrt_getpwdir (NULL);
home = home_buffer;
}
if (home && *home)
n += strlen (home);
}
else
{
/* This is the "~username/" or "~username" case. */
char *user;
user = _gpgrt_strdup (first_part+1);
if (!user)
return NULL;
p = strchr (user, '/');
if (p)
*p = 0;
skip = 1 + strlen (user);
home = home_buffer = _gpgrt_getpwdir (user);
xfree (user);
if (home)
n += strlen (home);
else
skip = 1;
}
}
name = xtrymalloc (n);
if (!name)
{
_gpgrt_free (home_buffer);
return NULL;
}
if (home)
p = stpcpy (stpcpy (stpcpy (name, home), extradelim), first_part + skip);
else
p = stpcpy (name, first_part);
xfree (home_buffer);
home_buffer = NULL;
for (argc=0; argv[argc]; argc++)
{
/* Avoid a leading double slash if the first part was "/". */
if (!argc && name[0] == '/' && !name[1])
p = stpcpy (p, argv[argc]);
else
p = stpcpy (stpcpy (p, "/"), argv[argc]);
}
if ((flags & GPGRT_FCONCAT_ABS))
{
#ifdef HAVE_W32_SYSTEM
p = strchr (name, ':');
if (p)
p++;
else
p = name;
#else
p = name;
#endif
if (*p != '/'
#ifdef HAVE_W32_SYSTEM
&& *p != '\\'
#endif
)
{
char *mycwd = _gpgrt_getcwd ();
if (!mycwd)
{
xfree (name);
return NULL;
}
n = strlen (mycwd) + 1 + strlen (name) + 1;
home_buffer = xtrymalloc (n);
if (!home_buffer)
{
xfree (mycwd);
xfree (name);
return NULL;
}
if (p == name)
p = home_buffer;
else /* Windows case. */
{
memcpy (home_buffer, p, p - name + 1);
p = home_buffer + (p - name + 1);
}
/* Avoid a leading double slash if the cwd is "/". */
if (mycwd[0] == '/' && !mycwd[1])
strcpy (stpcpy (p, "/"), name);
else
strcpy (stpcpy (stpcpy (p, mycwd), "/"), name);
xfree (mycwd);
xfree (name);
name = home_buffer;
/* Let's do a simple compression to catch the common case of
* a trailing "/.". */
n = strlen (name);
if (n > 2 && name[n-2] == '/' && name[n-1] == '.')
name[n-2] = 0;
}
#ifdef HAVE_W32_SYSTEM
else if (name[0] && name[1] != ':'
&& !(name[0] == '/' && name[1] == '/')
&& !(name[0] == '\\' && name[1] == '\\'))
{
/* Absolute name but no drive letter but also no UNC. */
char *mycwd = _gpgrt_getcwd ();
if (!mycwd)
{
xfree (name);
return NULL;
}
if (mycwd[0] && mycwd[1] == ':')
{
home_buffer = xtrymalloc (2 + strlen (name) + 1);
if (!home_buffer)
{
xfree (mycwd);
xfree (name);
return NULL;
}
home_buffer[0] = mycwd[0];
home_buffer[1] = mycwd[1];
strcpy (home_buffer + 2, name);
xfree (name);
name = home_buffer;
}
xfree (mycwd);
}
#endif /* W32 */
#ifdef HAVE_W32_SYSTEM
/* Fix "c://foo" to c:/foo". */
n = strlen (name);
if (name[0] && name[1] == ':' && name[2] == '/' && name[3] == '/')
memmove (name+3, name+4, strlen (name+4)+1);
#endif
} /* end GPGRT_FCONCAT_ABS */
#ifdef HAVE_W32_SYSTEM
for (p=name; *p; p++)
if (*p == '\\')
*p = '/';
#endif
return name;
}
char *
_gpgrt_fconcat (unsigned int flags, const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = _gpgrt_vfnameconcat (flags, first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
/* Construct a filename from the NULL terminated list of parts. Tilde
* expansion is done for the first argument. The caller must release
* the result using gpgrt_free; on error ERRNO is set and NULL
* returned. */
char *
_gpgrt_fnameconcat (const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = _gpgrt_vfnameconcat (GPGRT_FCONCAT_TILDE, first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
/* Construct a filename from the NULL terminated list of parts. Tilde
* expansion is done for the first argument. The caller must release
* the result using gpgrt_free; on error ERRNO is set and NULL
* returned. This version returns an absolute filename. */
char *
_gpgrt_absfnameconcat (const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = _gpgrt_vfnameconcat (GPGRT_FCONCAT_TILDE| GPGRT_FCONCAT_ABS,
first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
|