+void save_keybindings(struct World * world) {
+// Write keybidings to keybindings file.
+ struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
+ struct KeyBinding * keybindings = world->keybindings;
+ FILE * file = fopen("keybindings", "w");
+ int linemax = 0;
+ int i;
+ for (i = 0; i <= keyswindata->max; i++)
+ if (strlen(keybindings[i].name) > linemax)
+ linemax = strlen(keybindings[i].name);
+ linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
+ char * line = malloc(linemax);
+ for (i = 0; i <= keyswindata->max; i++) {
+ snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
+ fwrite(line, sizeof(char), strlen(line), file); }
+ free(line);
+ fclose(file); }
+