最近刷抖音上头,为了防止自己继续沉迷下去,决定写点代码,让电脑永远不能访问抖音网站,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HOSTS_PATH "C:\\Windows\\System32\\drivers\\etc\\hosts"
#define BACKUP_PATH "C:\\Windows\\System32\\drivers\\etc\\hosts.bak"
#define BUFFER_SIZE 1024
// 函数声明
int BackupHosts(const char* backupPath);
int RestoreHosts(const char* originalPath, const char* backupPath);
// 辅助函数,用于复制临时文件到hosts文件
int fcopy(FILE* src, FILE* dest) {
char buffer[BUFFER_SIZE];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, src)) > 0) {
if (fwrite(buffer, 1, bytesRead, dest) != bytesRead) {
return -1;
}
}
return 0;
}
int AddHostsEntry(const char* domain) {
FILE* hostsFile, * tempFile;
char buffer[BUFFER_SIZE];
const char* ip = "127.0.0.1";
int added = 0;
// 打开原hosts文件和临时文件
hostsFile = fopen(HOSTS_PATH, "r+");
if (hostsFile == NULL) {
perror("Error opening hosts file");
return -1;
}
// 创建临时文件
tempFile = tmpfile();
if (tempFile == NULL) {
fclose(hostsFile);
perror("Error creating temporary file");
return -1;
}
// 读取原hosts文件内容,检查并添加新条目
while (fgets(buffer, BUFFER_SIZE, hostsFile) != NULL) {
if (strstr(buffer, domain) != NULL) {
added = 1; // 已经存在该域名的条目
}
fputs(buffer, tempFile); // 复制到临时文件
}
// 如果没有找到该域名的条目,则添加新的条目
if (!added) {
fprintf(tempFile, "%s %s\n", ip, domain);
}
// 替换原hosts文件
rewind(tempFile); // 重置临时文件的文件指针到开始位置
if (fcopy(tempFile, hostsFile) == -1) {
fclose(hostsFile);
fclose(tempFile);
return -1;
}
// 关闭文件
fclose(hostsFile);
fclose(tempFile);
return 0;
}
int main() {
// 首先备份原始的hosts文件
if (BackupHosts(BACKUP_PATH) != 0) {
fprintf(stderr, "Failed to backup the original hosts file.\n");
system("pause");
return 1;
}
// 这里可以添加修改hosts文件的代码
// 例如:AddHostsEntry("example.com");
char domain[] = "www.douyin.com"; // 需要禁止访问的域名
if (AddHostsEntry(domain) == 0) {
printf("Hosts file updated successfully.\n");
system("pause");
}
else {
perror("Failed to update hosts file");
system("pause");
}
//return 0;
//
system("pause");
// 程序结束前,恢复原始的hosts文件
/*
if (RestoreHosts(HOSTS_PATH, BACKUP_PATH) != 0) {
fprintf(stderr, "Failed to restore the original hosts file.\n");
// 可以选择删除修改过的hosts文件,恢复备份文件
// remove(HOSTS_PATH); // 删除修改过的hosts文件
// rename(BACKUP_PATH, HOSTS_PATH); // 将备份文件重命名为hosts
}
*/
return 0;
}
// 备份原始的hosts文件
int BackupHosts(const char* backupPath) {
if (remove(backupPath) == 0) {
printf("Removed existing backup file.\n");
}
if (copyfile(HOSTS_PATH, backupPath) != 0) {
return -1;
}
return 0;
}
// 恢复原始的hosts文件
int RestoreHosts(const char* originalPath, const char* backupPath) {
return copyfile(backupPath, originalPath);
}
// 辅助函数,用于复制文件内容
int copyfile(const char* srcPath, const char* destPath) {
FILE* srcFile, * destFile;
char buffer[1024];
size_t bytesRead, bytesWritten;
srcFile = fopen(srcPath, "rb");
if (srcFile == NULL) {
perror("Error opening source file");
return -1;
}
destFile = fopen(destPath, "wb");
if (destFile == NULL) {
perror("Error opening destination file");
fclose(srcFile);
return -1;
}
while ((bytesRead = fread(buffer, 1, sizeof(buffer), srcFile)) > 0) {
bytesWritten = fwrite(buffer, 1, bytesRead, destFile);
if (bytesRead != bytesWritten) {
perror("Error writing to destination file");
fclose(srcFile);
fclose(destFile);
return -1;
}
}
fclose(srcFile);
fclose(destFile);
return 0;
}