diff --git a/include/launch_state.h b/include/launch_state.h index eb15b23..e63ebe3 100644 --- a/include/launch_state.h +++ b/include/launch_state.h @@ -3,6 +3,7 @@ #include #include +#include struct LaunchState { int user_opt; diff --git a/src/launch_state.c b/src/launch_state.c index 381209d..5a26fdb 100644 --- a/src/launch_state.c +++ b/src/launch_state.c @@ -1,6 +1,7 @@ // Small file for saving last selection -#define STATE_PATH "/var/lib/lidm/state" +#define STATE_DIR "/var/lib/lidm" +#define STATE_FILE "/var/lib/lidm/state" #include "launch_state.h" @@ -8,8 +9,8 @@ struct LaunchState read_launch_state() { struct LaunchState state; state.user_opt = 1; state.session_opt = 1; - FILE* state_fd = fopen(STATE_PATH, "r"); - if (!state_fd) return state; + FILE* state_fd = fopen(STATE_FILE, "r"); + if (state_fd == NULL) return state; if (fscanf(state_fd, "%i;%i", &state.user_opt, &state.session_opt) != 2) { state.user_opt = 1; state.session_opt = 1; @@ -19,8 +20,12 @@ struct LaunchState read_launch_state() { } bool write_launch_state(struct LaunchState state) { - FILE* state_fd = fopen(STATE_PATH, "w"); - if (!state_fd) return false; + FILE* state_fd = fopen(STATE_FILE, "w"); + if (state_fd == NULL) { + if (mkdir(STATE_DIR, 0755) == -1) return false; + state_fd = fopen(STATE_FILE, "w"); + if (state_fd == NULL) return false; + } (void)fprintf(state_fd, "%i;%i", state.user_opt, state.session_opt); (void)fclose(state_fd); return true;