Skip to content

Commit

Permalink
Various fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanem committed Jul 8, 2023
1 parent e276a69 commit f6c7f67
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.MinGW
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
FORTIFY_SOURCE ?= 0
USE_DEBUG ?= 0
USE_64BIT ?= 0
USE_64BIT ?= 1

CC = gcc
CFLAGS = -Wall \
Expand Down
16 changes: 10 additions & 6 deletions src/cmake.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ int cmake_get_info_registry (smartlist_t *sl, int *index, HKEY top_key)

for (num = 0; rc == ERROR_SUCCESS; num++)
{
char package_key [512];
char package [100];
char *uuid = "?";
char *path = "?";
BOOL exist;
DWORD size = sizeof(package);
char package_key [512];
char package [100];
char *uuid = "?";
char *path = "?";
BOOL exist;
DWORD size = sizeof(package);
struct stat st;

rc = RegEnumKeyEx (key, num, package, &size, NULL, NULL, NULL, NULL);
Expand All @@ -100,7 +100,11 @@ int cmake_get_info_registry (smartlist_t *sl, int *index, HKEY top_key)
if (sl && exist)
smartlist_add_strdup (sl, path);
if (opt.do_check)
{
if (opt.show_unix_paths)
slashify2 (path, path, '/');
C_printf (" [%2d]: ~6%-15s~0 -> ~6%s%s~0\n", num, package, path, exist ? "": " ~5(Missing)");
}

FREE (uuid);
FREE (path);
Expand Down
127 changes: 81 additions & 46 deletions src/envtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,19 @@ static BOOL asan_enabled (void)
#endif
}

/**
* Returns TRUE if program was compiled with
* "Undefined Behaviour Sanitizer" support.
*/
static BOOL ubsan_enabled (void)
{
#ifdef USE_UBSAN
return (TRUE);
#else
return (FALSE);
#endif
}

/**
* Handler for `envtool -V..`:
* + Show some basic version information: option `-V`.
Expand All @@ -369,13 +382,14 @@ static BOOL asan_enabled (void)
static int show_version (void)
{
HWND wnd;
BOOL wow64 = is_wow64_active();

C_printf ("%s.\n"
" Version ~3%s ~1(%s, %s%s%s)~0 by %s.\n"
" Version ~3%s ~1(%s, %s%s%s%s)~0 by %s.\n"
" Hosted at: ~6%s~0\n",
who_am_I, VER_STRING, compiler_version(), WIN_VERSTR,
wow64 ? ", ~1WOW64" : "", asan_enabled() ? ", ASAN" : "",
is_wow64_active() ? ", ~1WOW64" : "",
asan_enabled() ? ", ASAN" : "",
ubsan_enabled() ? ", UBSAN" : "",
AUTHOR_STR, GITHUB_STR);

wnd = FindWindow (EVERYTHING_IPC_WNDCLASS, 0);
Expand Down Expand Up @@ -3897,7 +3911,7 @@ static void check_env_val (const char *env, const char *file_spec, int *num, cha
else slashify2 (fbuf, arr->dir, opt.show_unix_paths ? '/' : '\\');

if (!stricmp("PATH", env))
ignored = cfg_ignore_lookup("[Path]", fbuf);
ignored = cfg_ignore_lookup("[Path]", arr->dir);

if (start > arr->dir)
{
Expand Down Expand Up @@ -3997,7 +4011,7 @@ static void check_env_val_reg (const smartlist_t *list, const char *env_name)
get_reparse_point (arr->dir, link, sizeof(link)))
{
C_puts ("\n -> ");
print_raw (_fix_drive(link), "~4", NULL);
print_raw (slashify2(link, link, opt.show_unix_paths ? '/' : '\\'), "~4", NULL);
}
C_puts ("~0\n");
}
Expand Down Expand Up @@ -4029,7 +4043,7 @@ static void check_env_val_reg (const smartlist_t *list, const char *env_name)
else if (errors == 0)
C_puts ("~2OK~0, ");

C_printf ("~6%2d~0 elements\n", max);
C_printf ("~6%2d~0 elements\n\n", max);
dir_array_free();
}

Expand Down Expand Up @@ -4078,11 +4092,10 @@ static void check_app_paths (HKEY key)
fname = opt.show_unix_paths ? slashify (fname,'/') : fname;
print_raw (fname, NULL, NULL);
}

C_puts ("~0\n");
}

if (!is_directory(fbuf) && !cfg_ignore_lookup("[Registry]",fbuf))
if (!is_directory(fbuf) && !cfg_ignore_lookup("[Registry]", fbuf))
{
C_printf ("%*c~5Missing dir~0: ~3%s\"~0\n", indent, ' ', fbuf);
errors++;
Expand All @@ -4103,9 +4116,9 @@ static void check_app_paths (HKEY key)
if (max == 0)
C_puts ("~5Does not exists~0,"); /* Impossible */
else if (errors == 0)
C_puts ("~2OK~0, ");
C_puts ("~2OK~0, ");
else
C_puts ("~5Error~0, ");
C_puts ("~5Error~0, ");

C_printf ("~6%2d~0 elements\n", max);
reg_array_free();
Expand All @@ -4130,29 +4143,34 @@ static void print_env_val (const char *val, size_t indent)
/**
* Compare an environment value from SYSTEM and USER.
* Ignore differences in case and trailing slashes ('\\' or '/') or ';'.
* Also ignore difference in values like:
* \li `0` vs `false` or
* \li `1` vs `true`.
*
* \todo if an env-var looks like 1 or more directories and one of it's values
* is a junction, check it's target before comparing each directory.
*/
static void compare_user_sys_env (const char *env_var, const char *sys_value, int indent1)
{
const char *end;
const char *sys_end;
const char *user_value = getenv (env_var);
char *sys_val = getenv_expand_sys (sys_value);
size_t len, indent2;
int i;
BOOL equal = FALSE;
size_t sys_len, indent2;

if (sys_val)
sys_value = sys_val;

len = strlen (sys_value);
end = sys_value + len - 1;
if (IS_SLASH(*end))
sys_len = strlen (sys_value);
sys_end = sys_value + sys_len - 1;
if (IS_SLASH(*sys_end))
{
len--;
end--;
sys_len--;
sys_end--;
}
if (*end == ';')
len--;
if (*sys_end == ';')
sys_len--;

#if 0
if (sys_value && user_value && strchr(sys_value, ';') && strchr(user_value, ';'))
Expand All @@ -4162,6 +4180,19 @@ static void compare_user_sys_env (const char *env_var, const char *sys_value, in
}
#endif

if (sys_value && user_value)
{
const char *equals[] = { "false", "0", "true", "1" };

for (i = 0; i < DIM(equals) && !equal; i++)
{
if (!stricmp(sys_value, equals[i]) || !stricmp(user_value, equals[i]))
equal = TRUE;
}
if (!equal)
equal = (strnicmp(user_value, sys_value, sys_len) == 0);
}

C_printf (" ~3%-*s~0", indent1, env_var);
indent2 = indent1 + sizeof("SYSTEM = ") + 2;

Expand All @@ -4171,7 +4202,7 @@ static void compare_user_sys_env (const char *env_var, const char *sys_value, in
print_env_val (sys_value, indent2);
C_printf (" %*s~2USER = <None>~0\n", indent1, "");
}
else if (strnicmp(user_value, sys_value, len))
else if (!equal)
{
C_printf ("Mismatch:\n %*s~6SYSTEM = ", indent1, "");
print_env_val (sys_value, indent2);
Expand All @@ -4187,43 +4218,43 @@ static void compare_user_sys_env (const char *env_var, const char *sys_value, in
}

/**
* Compare all environment values for SYSTEM and check if there is a difference
* in the corresponding USER variable.
* Compare all environment values for SYSTEM and check if there
* is a difference in the corresponding USER variable.
*/
static void do_check_user_sys_env (void)
{
smartlist_t *list;
smartlist_t *sys_list;
int i, max;
size_t len, longest_env = 0;
char *env, *val;
char *sys_env, *sys_val;

C_puts ("\nComparing ~6SYSTEM~0 and ~2USER~0 environments:\n");

if (!getenv_system(&list))
if (!getenv_system(&sys_list))
{
C_printf ("CreateEnvironmentBlock() failed: %s.\n", win_strerror(GetLastError()));
return;
}

TRACE (1, "C_screen_width(): %d\n", (int)C_screen_width());
max = smartlist_len (list);
max = smartlist_len (sys_list);
for (i = 0; i < max; i++)
{
env = smartlist_get (list, i);
val = strchr (env, '=');
*val++ = '\0';
len = strlen (env);
sys_env = smartlist_get (sys_list, i);
sys_val = strchr (sys_env, '=');
*sys_val++ = '\0';
len = strlen (sys_env);
if (len > longest_env)
longest_env = len;
}

for (i = 0; i < max; i++)
{
env = smartlist_get (list, i);
val = strchr (env, '\0') + 1;
compare_user_sys_env (env, val, (int)(2 + longest_env));
sys_env = smartlist_get (sys_list, i);
sys_val = strchr (sys_env, '\0') + 1;
compare_user_sys_env (sys_env, sys_val, (int)(2 + longest_env));
}
smartlist_free_all (list);
smartlist_free_all (sys_list);
}

/**
Expand Down Expand Up @@ -4252,21 +4283,21 @@ static struct environ_fspec envs[] = {
{ "*.dll", "LUA_CPATH" },
{ "*.cmake", "CMAKE_MODULE_PATH" },
{ "*.pm", "PERLLIBDIR" },
{ NULL, "CLASSPATH" }, /* No support for these. But do it anyway. */
{ NULL, "CLASSPATH" }, /* No support for these. But do it anyway */
{ "*.go", "GOPATH" },
{ NULL, "FOO" } /* Check that non-existing env-vars are also checked */
};

static int do_check (void)
{
struct ver_info cmake_ver;
char *cmake_exe;
char *sys_env_path = NULL;
char *sys_env_inc = NULL;
char *sys_env_lib = NULL;
char status [200+_MAX_PATH];
int i, save, num;
int index = 0;
char *cmake_exe;
char *sys_env_path = NULL;
char *sys_env_inc = NULL;
char *sys_env_lib = NULL;
char status [200+_MAX_PATH];
int i, save, num;
int index = 0;

/* Do not implicitly add current directory in these searches.
*/
Expand Down Expand Up @@ -4304,12 +4335,16 @@ static int do_check (void)
{
FREE (cmake_exe);
C_printf ("Checking ~3HKEY_CURRENT_USER\\%s~0 keys:\n", KITWARE_REG_NAME);
cmake_get_info_registry (NULL, &index, HKEY_CURRENT_USER);
C_putc ('\n');
num = cmake_get_info_registry (NULL, &index, HKEY_CURRENT_USER);
if (num == 0)
C_printf (" ~5Does not exists~0\n\n");
else C_printf (" ~2OK~0, %d elements\n\n", num);

C_printf ("Checking ~3HKEY_LOCAL_MACHINE\\%s~0 keys:\n", KITWARE_REG_NAME);
cmake_get_info_registry (NULL, &index, HKEY_LOCAL_MACHINE);
C_putc ('\n');
num = cmake_get_info_registry (NULL, &index, HKEY_LOCAL_MACHINE);
if (num == 0)
C_printf (" ~5Does not exists~0\n\n");
else C_printf (" ~2OK~0, %d elements\n\n", num);
}

opt.no_cwd = save;
Expand Down
27 changes: 24 additions & 3 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ typedef BOOL (WINAPI *func_IsWow64Process) (HANDLE proc, BOOL *wow64);
*/
typedef BOOL (WINAPI *func_NeedCurrentDirectoryForExePathA) (const char *exe_name);

/** \typedef func_ExpandEnvironmentStringsForUserA
/**
* \typedef func_ExpandEnvironmentStringsForUserA
*
* Since these functions are not available on Win-XP, dynamically load "userenv.dll"
* and get the function-pointer to `ExpandEnvironmentStringsForUserA()`.
*
* \note The MSDN documentation for `ExpandEnvironmentStringsForUser`()` is
* wrong. The return-value is *not* a `BOOL`, but it returns the length
* of the expanded buffer (similar to `ExpandEnvironmentStrings()`).
* \see https://msdn.microsoft.com/en-us/library/windows/desktop/bb762275(v=vs.85).aspx
* \see https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsa
* \see https://learn.microsoft.com/en-gb/windows/win32/api/userenv/nf-userenv-expandenvironmentstringsforusera
*/
typedef DWORD (WINAPI *func_ExpandEnvironmentStringsForUserA) (
HANDLE token,
Expand Down Expand Up @@ -1574,6 +1576,25 @@ char *str_plural (DWORD val, const char *singular, const char *plural)
return (char*) singular;
}

/**
* Checks a file-name for illegal characters.
*
* \ref https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file
*/
BOOL legal_file_name (const char *fname)
{
const char *p;

for (p = fname; *p; p++)
{
if (*p >= 1 && *p <= 31)
return (FALSE);
if (strchr ("|<>\"?*", *p))
return (FALSE);
}
return (TRUE);
}

/**
* Find the first slash in a file-name.
* \param[in] s the file-name to search in.
Expand Down Expand Up @@ -4502,7 +4523,7 @@ char *getenv_expand_sys (const char *variable)
TRACE (1, "ExpandEnvironmentStringsForUser() failed: %s.\n",
win_strerror(GetLastError()));
else rc = STRDUP (buf);
TRACE (3, "variable: '%s', expanded: '%s'\n", variable, rc);
TRACE (3, "size: %lu, variable: '%s', expanded: '%s'\n", size, variable, rc);
}
return (rc);
}
Expand Down

0 comments on commit f6c7f67

Please sign in to comment.