Thursday, 9 August 2012

Windows shutting down (C code)

/*
 * Shutting down.c
 *
 *  Created on: 02.03.2012
 *      Author: fasenov
 */

//Copy right Filip
//Made on Tue Feb 28 16:08:00 2012
//The program is work only on Windows OS

#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<string.h>

char str[25]={};
time_t rawtime;
struct tm * timeinfo;

void HEADER(){
    printf("///////////////////////////////");
    printf("///////////////////////////////\n");
    puts("//      Shutting down\n//      Created on: 02.03.2012\n//      Author: faleksandrov */");
    puts("//    Copy right Filip\n//    Made on Tue Feb 28 16:08:00 2012\n//    The program is work only on Windows OS");
    printf("///////////////////////////////");
    printf("///////////////////////////////\n\n");
}


void input_w_day(char *w_day){
    int key;
    const char day[][5] = {"Mon ","Tue ","Wed ","Thu ","Fri ","Sat ","Sun "};
e:
    printf("Current local time:%s\n",asctime (timeinfo));
    printf("Choose the day: \n");
    printf("1-Monday\n");
    printf("2-Tuesday\n");
    printf("3-Wednesday\n");
    printf("4-Thursday\n");
    printf("5-Friday\n");
    printf("6-Saturday\n");
    printf("7-Sunday\n");
    printf("0-Exit\n");
    scanf("%d",&key);
    if(key==0) strcat(w_day,"ERROR");return;
    if((key<1)||(key>7)){
        system("cls");
        goto e;
    }
    else {
        strcat(w_day,day[key-1]);
    }
    system("cls");
}
void input_month(char *month){
    int key;
    const char mon[][5]={"Jan ","Feb ","Mar ","Apr ","May ","Jun ","Jul ","Aug ","Sep ","Oct ","Nov ","Dec "};
    printf("Current local time: %s\n",asctime (timeinfo));
    printf("Choose the month: \n");
    printf("1-January\n");
    printf("2-February\n");
    printf("3-March\n");
    printf("4-April\n");
    printf("5-May\n");
    printf("6-June\n");
    printf("7-July\n");
    printf("8-August\n");
    printf("9-September\n");
    printf("10-October\n");
    printf("11-November\n");
    printf("12-December\n");
    printf("0-Exit\n");
    scanf("%d",&key);
    if(key==0) strcat(month,"ERROR");return;
    if((key<1)||(key>12)){
        system("cls");
        input_month(month);
    }
    else {
        strcat(month,mon[key-1]);
    }
    system("cls");
}
void input_n_day(char *n_day){
        char day[3]={};
        char time[26]={};
        char check[10]={};
        strcpy(time,asctime (timeinfo));
        int i;
        for(i=8;i<10;i++){
            check[i]=time[i+8];
        }
e:
        printf("Current local time: %s\n",asctime (timeinfo));
        printf("Enter the day with number : ");
        scanf("%s",&day);
        //printf("%d",strlen(day));
        if(strlen(day)==2){
        if((day[0]<'0') || (day[0]>'3')) {goto e;}
        if((day[1]<'0') || (day[1]>'9')) {goto e;}
        if((day[0]=='3') && (day[1]>'1')) {goto e;}
        if((day[0]<check[0])||(day[1]<check[1])){goto e;}
        }
        else if((day[0]<check[1])&&(strlen(day)!=1))goto e;
        else {
            char tmp = day[0];
            day[0]='0';
            day[1]=tmp;
            }

        strcat(n_day,day);
        strcat(n_day," ");
        system("cls");

}
void input_hour(char *hour){
    char hms[10]={};
e:
    printf("Current local time: %s\n",asctime (timeinfo));
    printf("Input time HH:MM:SS ");
    scanf("%s",&hms);

    if((hms[0]<'0')||(hms[0]>'2')) {system("cls");printf("Wrong hour\n");goto e;}
    if((hms[1]<'0')||(hms[1]>'9')) {system("cls");printf("Wrong hour\n");goto e;}
    if((hms[0]=='2')&&(hms[1]>'4')){system("cls");printf("Wrong hour\n");goto e;}
    if((hms[3]<'0')||(hms[3]>'5')) {system("cls");printf("Wrong hour\n");goto e;}
    if((hms[4]<'0')||(hms[4]>'9')) {system("cls");printf("Wrong hour\n");goto e;}
    if((hms[6]<'0')||(hms[6]>'5')) {system("cls");printf("Wrong hour\n");goto e;}
    if((hms[7]<'0')||(hms[7]>'9')) {system("cls");printf("Wrong hour\n");goto e;}
    if((hms[2]!=':')||(hms[5]!=':')) {system("cls");printf("Must use ':' for delimiter\n");goto e;}

    strcat(hour,hms);
    strcat(hour," ");
    system("cls");

}
void input_year(char *year){
    char y[5]={};
    char time[26]={};
    char check[5]={};
    strcpy(time,asctime (timeinfo));
    int i;
    for(i=0;i<4;i++){
        check[i]=time[i+20];
    }

e:
    system("cls");
    printf("Current local time: %s\n",asctime (timeinfo));
    printf("Input year XXXX : ");
    scanf("%s",&y);
    printf("\n");
    if(check[0]>y[0]) {goto e;}
    else if(check[1]>y[1]) {goto e;}
          else if(check[2]>y[2]) {goto e;}
            else if(check[3]>y[3]) {goto e;}

    strcat(year,y);
}
void convert_data(char *str){

    char w_day[6]={};
    char month[6]={};
    char n_day[4]={};
    char hour[10]={};
    char year[5]={};

    input_w_day(w_day);
    if(strcmp(w_day,"ERROR")==0){strcat(str,"ERROR");return;}
    input_month(month);
    if(strcmp(month,"ERROR")==0){strcat(str,"ERROR");return;}
    input_n_day(n_day);
    input_hour(hour);
    input_year(year);
    strcat(str,w_day);
    strcat(str,month);
    strcat(str,n_day);
    strcat(str,hour);
    strcat(str,year);
    strcat(str,"\n");

}
void curent()
{

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    printf("Time to shut down: %s\n",str);
    printf("Current local time: %s\n",asctime (timeinfo));
    if (!strcmp(asctime (timeinfo),str))
    {
        system("shutdown -s -t 1");
        //printf("BOOM");
        //Sleep(5000);
    }
}
void process ()
{
    HEADER();
    curent();
    Sleep(1000);
    system("cls");
    process();

}
void smart(char *strt){
    char h[3];
    char m[3];
    char s[3];
    char time[26]={};
    char time1[26]={};
    char check[10]={};
    char yearr[6]={};
    strcpy(time,asctime (timeinfo));
    int i;
    for(i=0;i<4;i++){
        yearr[i]=time[i+20];
    }
    strncpy(time1,asctime (timeinfo),11);
e:
    system("cls");
    printf("Current local time: %s\n",asctime (timeinfo));
    printf("Please enter the hour :-->");
    scanf("%s",&h);
    if((h[0]<'0')||(h[0]>'2')) {system("cls");printf("Wrong hour\n");goto e;}
    if((h[1]<'0')||(h[1]>'9')) {system("cls");printf("Wrong hour\n");goto e;}
    if((h[0]=='2')&&(h[1]>'4')){system("cls");printf("Wrong hour\n");goto e;}
    printf("Please enter the minutes :-->");
    scanf("%s",&m);
    if((m[0]<'0')||(m[0]>'5')) {system("cls");printf("Wrong hour\n");goto e;}
    if((m[1]<'0')||(m[1]>'9')) {system("cls");printf("Wrong hour\n");goto e;}
    printf("Please enter the seconds :-->");
    scanf("%s",&s);
    if((s[0]<'0')||(s[0]>'5')) {system("cls");printf("Wrong hour\n");goto e;}
    if((s[1]<'0')||(s[1]>'9')) {system("cls");printf("Wrong hour\n");goto e;}
    strcat(check,h);
    strcat(check,":");
    strcat(check,m);
    strcat(check,":");
    strcat(check,s);
    strcat(check," ");
    strcat(strt,time1);
    strcat(strt,check);
    strcat(strt,yearr);
    strcat(strt,"\n");
}
int main ()
{
    int key;
    HEADER();
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
e:
    printf("Current local time: %s\n",asctime (timeinfo));
    printf("I want advanced search , press 1\n");
    printf("To enter the hour press 2\n");
    printf("To EXIT press 0\n");
    printf("-->");
    scanf("%d",&key);
    if(key==1){
        printf("Enter time to shut down :)\n");
        convert_data(str);
    }
    else if(key==2){
        smart(str);
    }
    else if (key != 0) goto e;
    else strcat(str,"ERROR");
    if(strcmp(str,"ERROR")!=0){
    process();
    }

    return 0;
}


No comments:

Post a Comment