diff options
-rw-r--r-- | src/main.c | 56 |
1 files changed, 44 insertions, 12 deletions
@@ -21,6 +21,25 @@ /** + * Prints the helptext + */ +void get_help() { + printf("Please specify a timeout threshold (in days) as well as a command\n" + "or commands to be run if the threshold is exceeded.\n\n" + "[1mExample:[0m\n" + " terminus 5 '<command to execute>'\n\n" + "This example will execute the given command if the user has not\n" + "logged in in the last 5 days." + "\n\n" + "If terminus detects that the login threshold has been exceeded, it will\n" + "not reset the last login. If however the threshold was exceeded but the\n" + "user is fine, the lastlogin file can be reset using the [1m-r[0m or the\n" + "[1m--reset[0m flags\n\n" + ); +} + + +/** * Converts the given day count to seconds (multiplies by the number of seconds * in a day, 86400). * @@ -137,27 +156,40 @@ int main(int argc, char* argv[]) { time_t threshold; time_t lastlogin; - if(argc < 3) { - printf( - "Please specify a timeout threshold (in days) as well as a command\n" - "or commands to be run if the threshold is exceeded.\n\n" - "[1mExample:[0m\n" - " terminus 5 '<command to execute>'\n\n" - "This example will execute the given command if the user has not\n" - "logged in in the last 5 days.\n\n" - ); + if(argc == 1) { + get_help(); return 1; } - // Get program arguments - days = atoi(argv[1]); - cmd = argv[2]; // Compose path to lastlogin file get_user_home(home); strcpy(lastlogin_path, home); strcat(lastlogin_path, "/.lastlogin"); + // Parse program arguments + for(int i=1; i<argc; i++) { + if(strcmp("-r", argv[i]) == 0 || strcmp("--reset", argv[i]) == 0) { + // Reset flag set. Override default behavior and write current timestamp + // to lastlogin file + if(write_lastlogin(lastlogin_path) == 1) { + printf("Lastlogin reset failure.\n" + "Unable to write to file %s.\n", lastlogin_path); + return 1; + } + // Exit early since we're force resetting lastlogin. + return 0; + } else if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) { + // The user requested the help text + get_help(); + return 0; + } + } + + // Get program arguments + days = atoi(argv[1]); + cmd = argv[2]; + // Set time-related values now = time(NULL); threshold = days_to_seconds(days); |