Make a prank shutdown program in C language

Reference:C language to make a prank shutdown program-CSDN Blog

1. Project introduction

C language implements a simple "rogue software", a prank shutdown program that can force shutdown and can be released by entering specified commands.

2. Run screenshots

Then when you enter "n" you can unlock and shut down.​ 

3. Complete source code

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <conio.h>
 
int main()
{
    char input[10];
    printf("警告!您的计算机将在一分钟后关机,请保存好您的工作并退出所有程序!");
    printf("是否确认关机?(y/n): ");
    scanf("%s", input);
 
    while (1) {
        if (strcmp(input, "y") == 0 || strcmp(input, "Y") == 0) {
            // 记录用户选择到日志文件
            FILE *logFile = fopen("D:/a1.txt", "a");
            if (logFile != NULL) {
                time_t currentTime;
                struct tm *localTime;
                time(&currentTime);
                localTime = localtime(&currentTime);
                fprintf(logFile, "%04d-%02d-%02d %02d:%02d:%02d - 用户选择了关机\n",
                    localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,
                    localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
                fclose(logFile);
            }
            // 执行关机操作
            system("shutdown /r /t 60");
            // 显示倒计时
            for (int i = 60; i >= 0; i--) {
                printf("\r倒计时:%d秒", i);
                fflush(stdout);
                sleep(1);
 
                if (_kbhit()) { // 检测按键
                    char key = _getch(); // 获取按键值
                    if (key == 's' || key == 'S') { // 如果按下了's'或'S'键
                        // 记录用户选择到日志文件
                        FILE *logFile = fopen("D:/a1.txt", "a");
                        if (logFile != NULL) {
                            time_t currentTime;
                            struct tm *localTime;
                            time(&currentTime);
                            localTime = localtime(&currentTime);
                            fprintf(logFile, "%04d-%02d-%02d %02d:%02d:%02d - 用户选择了关机并取消关机\n",
                                localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,
                                localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
                            fclose(logFile);
                        }
                        // 执行取消关机操作
                        system("shutdown /a");
                        return 0;
                    }
                }
            }
            return 0;
        } else if (strcmp(input, "n") == 0 || strcmp(input, "N") == 0) {
            printf("取消关机操作。");
            return 0;
        } else {
            printf("输入错误,请重新输入");
            printf("是否确认关机?(y/n): ");
            scanf("%s", input);
        }
    }
 
    return 0;
}


The program first displays a warning message and asks the user to confirm the shutdown. If the user enters "y" or "Y", the program will record the user's selection to the log file (D:/a1.txt), then perform a shutdown operation, and shut down the computer after a 60-second countdown. If the user enters "n" or "N", the program will cancel the shutdown operation. If the user enters other characters, the program will prompt an input error and ask the user again to confirm the shutdown.
 

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/134993790