From 0f6465b482bacf83180bcb45843292ac8c8ad900 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Fri, 3 Jul 2015 17:45:06 -0600 Subject: 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. --- src/main.c | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index d3baeea..fa89688 100644 --- a/src/main.c +++ b/src/main.c @@ -20,6 +20,25 @@ #include +/** + * 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" + "Example:\n" + " terminus 5 ''\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 -r or the\n" + "--reset 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" - "Example:\n" - " terminus 5 ''\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