diff options
author | Aaron Ball <nullspoon@iohq.net> | 2015-07-03 17:45:06 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2015-07-03 17:45:06 -0600 |
commit | 0f6465b482bacf83180bcb45843292ac8c8ad900 (patch) | |
tree | 391de5a05d3a6e7bec1c0db7e08788dd3e61b3ae | |
parent | dea9798f377df0f79c366f2553cbda5a3324caf4 (diff) | |
download | terminus-0f6465b482bacf83180bcb45843292ac8c8ad900.tar.gz terminus-0f6465b482bacf83180bcb45843292ac8c8ad900.tar.xz |
Implemented reset and help switches
In the event the threshold was exceeded, terminus would (and should) not reset
the lastlogin file. However if the user did not intend for the threshold to be
exceeded (eg: vacation), there was no way to reset the timer. This implements
the -r,--reset flags for resetting the lastlogin file.
Also moved the helptext into its own function and expanded it a bit.
To implement the new switches, implemented basic argument parser as well.
Consequently, implemented -h,--help flags.
-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); |