Last active
June 17, 2020 17:56
-
-
Save dracconi/4bb5ba87587034133b54ef1b6f9c8543 to your computer and use it in GitHub Desktop.
repairs path that's broken by windows update duh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int needed_vars(int size) { | |
int vars_needed = size/2000; | |
if (vars_needed * 2000 < size) { | |
vars_needed++; | |
} | |
return vars_needed; | |
} | |
int consume(char **arr, int sarr, char *out, int length) { | |
int i = 0; | |
while (length > 0 && i < sarr && strlen(arr[i]) <= length) { | |
strcat(out, arr[i]); | |
length -= strlen(arr[i]); | |
i++; | |
} | |
return i; | |
} | |
int main(int argc, char* argv[]) { | |
if (argc != 2) { | |
printf("Usage: %s in", argv[0]); | |
return 1; | |
} | |
char **arr = malloc(sizeof(char*)*1); | |
int ix = 0; | |
int size = 0; | |
FILE* f = fopen(argv[1], "r"); | |
char buf[256]; | |
while (NULL != fgets(buf, 256, f)) { | |
int l = strlen(buf); | |
if (l > 0 && buf[0] != '#') { | |
size += 1+l; | |
arr[ix] = malloc(sizeof(char)*l); | |
if (buf[l-1] == '\n') { | |
buf[l-1] = ';'; | |
} else { | |
buf[l] = ';'; | |
buf[l+1] = '\0'; | |
} | |
strcpy(arr[ix], buf); | |
ix++; | |
} | |
} | |
fclose(f); | |
int vars_needed = needed_vars(size); | |
size += vars_needed * 10; // %pathXXX%; | |
int vars_needed2 = needed_vars(size); | |
while (vars_needed < vars_needed2) { | |
vars_needed = vars_needed2; | |
size += 10 * (vars_needed2-vars_needed); | |
vars_needed2 = needed_vars(size); | |
} | |
vars_needed2 = vars_needed; | |
int padding = (vars_needed-1) * 10; | |
fprintf(stderr, "Vars: %d. Takes %d characters.\n", vars_needed, padding); | |
char out[2048] = {0}; | |
int length = 2000-padding; | |
int jx = 0; | |
while (length > 0 && jx < ix && strlen(arr[jx]) <= length) { | |
strcat(out, arr[jx]); | |
length -= strlen(arr[jx]); | |
jx++; | |
} | |
while (vars_needed2 > 1) { | |
vars_needed2--; | |
char s[11]; | |
sprintf(s, "%%path%d%%;", vars_needed2); | |
strcat(out, s); | |
} | |
printf("%s\n\n", out); | |
ix -= jx; | |
while (vars_needed > 1) { | |
vars_needed--; | |
printf("%%path%d%%\n", vars_needed); | |
int count = consume(arr, ix, out, 2000); | |
fprintf(stderr, "To go: %d\n", ix-count); | |
ix -= count; | |
arr += count; | |
printf("%s\n\n", out); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment