3

Introduction

I have a script that I am creating for when the kids logon to their computers as part of my home domain. The script will check the current time and then if it is outside of the start and finish time it will shut the computer down automatically.

The C Script

The script I have so far follows;

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{  
    int i;
    char current_time[100];
    char *start;
    char *finish;

    if(argc>=2)
    {
        for(i=0;i<argc;i++)
        {

            if(strcmp(argv[i],"-s") == 0) 
            {
                start = argv[i+1];         
            }
            else if (strcmp(argv[i],"-f") == 0) 
            {
                finish = argv[i+1];
            }

        }
    }       

    time_t curr_time_value = time( NULL );
    strftime(current_time, 100, "%T", localtime(&curr_time_value));

    if(current_time < start && current_time > finish)
    {
        system("shutdown /s /t 0");
    }
    else
    {
        printf("%s\n", current_time);     
    }

    return 0;
}

The Problem

The part of the script I am having issues with is the comparison of the time, what I want to do is something along the lines of;

if(current_time < start && current_time > finish)
{
    system("shutdown /s /t 0");
}

I understand that the script is a string and as far as I am aware you can not compare two strings in this manner, as in lower than or greater than. But what I need to do is change these values to an int in order to use this type of comparison.

I was looking for advice on how I might proceed in making this comparison script work. I will be adding a while loop in the future to repeatedly fire the script to make sure the computer turns off.

What I've Tried

I have tried other scripts such as powershell and batch files with scheduled tasks but it isn't reliable. The kids just boot up their machines and then log back on. So given the small amount of time I have spare, I need a more fool proof method, which leads me here.

Jean-François Fabre Fix

time_t curr_time_value = time( NULL ); 
strftime(current_time, 100, "%H:%M", localtime(&curr_time_value));

if(strcmp(current_time,start) < 0 || strcmp(current_time,finish) > 0)
{
    system("shutdown /s /t 0");
}

1 Answers1

1

You could use a "pseudo-ISO" format for your string:

strftime(current_time, 100, "%H:%M", localtime(&curr_time_value));

that generates something like 20:59 or 09:00 (zero padded) (note: I couldn't make %T work on my Windows machine, it just generates an empty string, besides I assume that you don't need the seconds)

In that case, if your arguments respect that format, string comparison works fine but you have to fix your condition:

  • it has to use || because shutdown occurs on either conditions, not both at the same time
  • it has to use strcmp else you're comparing the pointers and the behaviour is undefined/not what you want

fix:

if (strcmp(current_time,start) < 0 || strcmp(current_time,finish) > 0)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • You are correct in assuming that I do not need the seconds. The problem I'm having after the fix you so kindly recommended is that now the script just fires no matter what time I put in. – user9447673 Mar 05 '18 at 20:26
  • what is your exact command line? (note: your option parsing isn't very safe) – Jean-François Fabre Mar 05 '18 at 20:32
  • When I output the variables from the command line input, the correct information is being printed out by printf(). I understand the option parsing isn't safe currently, I was hoping to upgrade once I got the general structure right. I was trying to keep it short and sweet while I'm getting it to work. – user9447673 Mar 05 '18 at 20:36
  • you're right, it doesn't work!!! how can have missed it for so long!! too much c++ `std::string` & python lately. You cannot compare strings with `<`, you have to use the result of `strcmp` instead. I have tested this and it works. – Jean-François Fabre Mar 05 '18 at 20:41
  • also: you accepted the answer even if this didn't work: you don't have to do that. Now you can keep it as accepted as it works. – Jean-François Fabre Mar 05 '18 at 20:45
  • Can you elaborate of the strcmp() and how you formatted the script please. Yeah, I accepted because it helped me out, what you said was right, and I missed it. – user9447673 Mar 05 '18 at 20:50
  • Yeah, that works brilliantly, thank you very, very much. I appreciate your help and I'm very appreciative. – user9447673 Mar 05 '18 at 20:56