cscriptingstartupshutdownparental-control

Run script outside of start and finish times in the C programming language on Windows platform


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");
}

Solution

  • 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:

    fix:

    if (strcmp(current_time,start) < 0 || strcmp(current_time,finish) > 0)