-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytelook.c
201 lines (175 loc) · 7.1 KB
/
bytelook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define CYAN "\033[1;36m"
#define RED "\033[1;31m"
#define RESET "\033[0m"
// Function to print size in a human format
void print_size(unsigned long size_in_bytes) {
double size_in_mb = size_in_bytes / (1024.0 * 1024.0);
char buffer[50];
if (size_in_mb > 1024) {
snprintf(buffer, sizeof(buffer), "%.2f GB", size_in_mb / 1024.0);
} else {
snprintf(buffer, sizeof(buffer), "%.2f MB", size_in_mb);
}
printf("%s\n", buffer);
}
// Function to print disk usage information
void print_disk_usage(const char **paths, int num_paths, int no_home) {
printf("%s┌───────────────────────────────────┐%s\n", CYAN, RESET);
for (int i = 0; i < num_paths; ++i) {
struct statvfs stat;
// Check if statvfs succeeds
if (statvfs(paths[i], &stat) != 0) {
// Print error message if statvfs fails
printf("%s│ Path: %-29s %s\n", CYAN, paths[i], RESET);
printf("%s│ Error: Unable to access path %s\n", CYAN, RESET);
if (i < num_paths - 1) {
printf("%s│ %s\n", CYAN, RESET);
}
continue;
}
// Print disk usage display
printf("%s│ Path: %-29s %s\n", CYAN, paths[i], RESET);
printf("%s│ Total: ", CYAN);
print_size(stat.f_blocks * stat.f_frsize);
printf("%s│ Used: ", CYAN);
print_size((stat.f_blocks - stat.f_bfree) * stat.f_frsize);
printf("%s│ Available: ", CYAN);
print_size(stat.f_bavail * stat.f_frsize);
if (i < num_paths - 1) {
printf("%s│ %s\n", CYAN, RESET);
}
}
// Print message if the user does not have a home directory
if (no_home) {
printf("%s│ %s\n", CYAN, RESET);
printf("%s│ User doesn't have a home directory %s\n", CYAN, RESET);
}
printf("%s└───────────────────────────────────┘%s\n\n", CYAN, RESET);
}
// Function to update bytelook
void update_current_file() {
if (getuid() != 0) {
printf("%sError: Permission denied. Please run as root.%s\n", RED, RESET);
return;
}
printf("Performing update...\n");
if (system("curl -s -o temp_update_file.c https://raw.githubusercontent.com/Panonim/bytelook/main/bytelook.c") != 0 ||
system("gcc -o temp_executable temp_update_file.c") != 0 ||
system("mv temp_executable /usr/local/bin/bytelook") != 0 ||
system("rm temp_update_file.c") != 0) {
printf("%sError: Update failed.%s\n", RED, RESET);
return;
}
printf("Update completed successfully.\n");
}
void toggle_auto_update(const char *action, const char *program_path) {
if (getuid() != 0) {
printf("%sError: Permission denied. Please run as root.%s\n", RED, RESET);
return;
}
char cron_command[256];
if (strcmp(action, "on") == 0) {
// Construct the cron command to add the entry
sprintf(cron_command, "(crontab -l 2>/dev/null; echo \"0 0 */3 * * %s update\") | crontab -", program_path);
// Execute the command
if (system(cron_command) != 0) {
printf("%sError: Failed to enable auto-update.%s\n", RED, RESET);
return;
}
printf("Auto Update: on\n");
} else if (strcmp(action, "off") == 0) {
// Construct the cron command to remove the entry
sprintf(cron_command, "(crontab -l | grep -v '%s update') | crontab -", program_path);
// Execute the command
if (system(cron_command) != 0) {
printf("%sError: Failed to disable auto-update.%s\n", RED, RESET);
return;
}
printf("Auto Update: off\n");
} else {
printf("Invalid action. Use '--auto-update on' or '--auto-update off'.\n");
}
}
int main(int argc, char *argv[]) {
if (argc > 1 && strcmp(argv[1], "update") == 0) {
update_current_file();
return 0;
}
const char *paths[4] = {"/media", "/home", "/"};
int num_paths = 3;
// Check if the home directory exists
const char *home_dir = getenv("HOME");
int no_home = 0;
// Add home directory to paths if it exists
if (home_dir) {
paths[num_paths++] = home_dir;
} else {
no_home = 1;
}
// Get the path to the bytelook
char program_path[256];
ssize_t len = readlink("/proc/self/exe", program_path, sizeof(program_path)-1);
if (len != -1) {
program_path[len] = '\0';
} else {
printf("Error: Unable to determine the path to bytelook\n");
return 1;
}
// Function to check the current auto-update status
int check_auto_update_status(const char *program_path) {
FILE *cron_file = popen("crontab -l", "r");
if (cron_file == NULL) {
return 0; // Auto-update is off
}
char line[256];
while (fgets(line, sizeof(line), cron_file) != NULL) {
if (strstr(line, program_path) != NULL && strstr(line, "update") != NULL) {
pclose(cron_file);
return 1; // Auto-update is on
}
}
pclose(cron_file);
return 0; // Auto-update is off
}
// Parse command-line arguments
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0) {
printf("Usage: %s [OPTIONS]\n", argv[0]);
printf("Options:\n");
printf(" --help Display this help message\n");
printf(" -v Display version information\n");
printf(" update Updates a program to the newest version\n");
printf(" --auto-update on/off Current status: %s\n", check_auto_update_status(program_path) ? "on" : "off");
return 0;
} else if (strcmp(argv[i], "-v") == 0) {
printf("ByteLook version 2.0.2\n");
return 0;
} else if (strcmp(argv[i], "--auto-update") == 0) {
if (i + 1 < argc) {
toggle_auto_update(argv[i + 1], program_path);
return 0;
} else {
printf("Missing argument for --auto-update. Use 'on' or 'off'.\n");
return 0;
}
}
}
if (argc > 1) {
printf("Invalid command. Use '--help' for more information.\n");
return 0;
}
printf("%s┌───────────────────────────────────┐\n", CYAN);
printf("%s│ │\n", CYAN);
printf("%s│ ByteLook │\n", CYAN);
printf("%s│ │\n", CYAN);
printf("%s└───────────────────────────────────┘%s\n", CYAN, RESET);
// Print disk usage information
print_disk_usage(paths, num_paths, no_home);
return 0;
}