winprefs v0.3.2
A registry exporter for programmers.
Loading...
Searching...
No Matches
shell.c
1#include "shell.h"
2#include "constants.h"
3
4wchar_t *escape_for_batch(const wchar_t *input, size_t n_chars) {
5 if (input == nullptr || n_chars == 0) {
6 errno = EINVAL;
7 return nullptr;
8 }
9 unsigned i, j;
10 size_t new_n_chars = 0;
11 for (i = 0; i < n_chars; i++) {
12 new_n_chars++;
13 // Last condition is to handle REG_MULTI_SZ string sets
14 if (input[i] == L'\\' || input[i] == L'%' || input[i] == L'"' ||
15 (input[i] == L'\0' && i < (n_chars - 1))) {
16 new_n_chars++;
17 }
18 }
19 wchar_t *out = calloc(new_n_chars + 1, WL);
20 if (!out) { // LCOV_EXCL_START
21 return nullptr;
22 } // LCOV_EXCL_STOP
23 wmemset(out, L'\0', new_n_chars + 1);
24 if (n_chars == new_n_chars) {
25 wmemcpy(out, input, new_n_chars + 1);
26 return out;
27 }
28 for (i = 0, j = 0; i < n_chars && j < new_n_chars; i++, j++) {
29 // This condition is to handle REG_MULTI_SZ string sets
30 if (input[i] == '\0' && i < (n_chars - 1) && j < (new_n_chars - 1)) {
31 out[j] = '\\';
32 out[++j] = '0';
33 } else {
34 out[j] = input[i];
35 if (input[i] == L'"' || input[i] == L'%') {
36 out[++j] = input[i];
37 }
38 }
39 }
40 return out;
41}