297 |
return ret; |
return ret; |
298 |
} |
} |
299 |
|
|
300 |
|
static int sPopCount(const char *s, const char *c) |
301 |
|
{ |
302 |
|
int ret = 0; |
303 |
|
if (!s) |
304 |
|
return -1; |
305 |
|
for (int i = 0; s[i] != '\0'; i++) |
306 |
|
for (int j = 0; c[j] != '\0'; j++) |
307 |
|
if (s[i] == c[j]) |
308 |
|
ret++; |
309 |
|
return ret; |
310 |
|
} |
311 |
|
|
312 |
|
static char *shellEscape(const char *s) |
313 |
|
{ |
314 |
|
int l = strlen(s) + sPopCount(s, "'") * 2; |
315 |
|
|
316 |
|
char *ret = calloc(l+1, sizeof (*ret)); |
317 |
|
if (!ret) |
318 |
|
return NULL; |
319 |
|
for (int i = 0, j = 0; s[i] != '\0'; i++, j++) { |
320 |
|
if (s[i] == '\'') |
321 |
|
ret[j++] = '\\'; |
322 |
|
ret[j] = s[i]; |
323 |
|
} |
324 |
|
return ret; |
325 |
|
} |
326 |
|
|
327 |
|
static void unquote(char *s) |
328 |
|
{ |
329 |
|
int l = strlen(s); |
330 |
|
|
331 |
|
if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) { |
332 |
|
memmove(s, s+1, l-2); |
333 |
|
s[l-2] = '\0'; |
334 |
|
} |
335 |
|
} |
336 |
|
|
337 |
static int grub2SetEnv(struct configFileInfo *info, char *name, char *value) |
static int grub2SetEnv(struct configFileInfo *info, char *name, char *value) |
338 |
{ |
{ |
339 |
char *s = NULL; |
char *s = NULL; |
340 |
int rc = 0; |
int rc = 0; |
341 |
char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv"; |
char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv"; |
342 |
|
|
343 |
|
unquote(value); |
344 |
|
value = shellEscape(value); |
345 |
|
if (!value) |
346 |
|
return -1; |
347 |
|
|
348 |
rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value); |
rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value); |
349 |
|
free(value); |
350 |
if (rc <0) |
if (rc <0) |
351 |
return -1; |
return -1; |
352 |
|
|