#pragma check_stack(off)
#pragma comment(linker,"/OPT:NOWIN98")

#include <windows.h>

#define PARAMS(x) int __stdcall x(HWND mWnd, HWND aWnd, char *data, char *parms, BOOL show, BOOL nopause)
#define re(x) lstrcpy(data, x); return 3;

extern "C" int WINAPI _DllMainCRTStartup(HANDLE,DWORD,void*) { return 1; }

/*
Credit to Necroman for inspiring the atoi code
and for his extremely useful tutorial. Found here:
http://necroman.wot.net/dllhelp.htm
*/
int atoi(const char *str)
{
    int r=0,s=1;

    if (*str=='-') { s=-1; str++; }

    while (*str>='0' && *str<='9') { r*=10; r+=(*str++-'0'); }

    return r*s;
}
void strtok(char *s, char *to, int num, char d)
{
    int i,n,c,l=lstrlen(s);

    if (num==0) { lstrcpy(s,to); return; }

    for (i=n=c=0;i<l;i++)
    {
        if (s[i]==d) { n++; i++; }
        if (n==num-1) { to[c] = s[i]; c++; }
        else if (n>num) break;
    }

    to[c]=0;
}

PARAMS(set)
{
    if (!*data) { re("0 Insufficient parameters") }

    char buf[15]; 
    SYSTEMTIME st;

    GetLocalTime(&st);

    strtok(data, buf, 1, ':'); st.wHour = atoi(buf);
    strtok(data, buf, 2, ':'); st.wMinute = atoi(buf);
    strtok(data, buf, 3, ':'); st.wSecond = atoi(buf);

    if (!SetLocalTime(&st)) { re("0 Error setting time") }

    re("1 Time configuration successful")    
}
