arepaconcafe commited on
Commit
7e7e923
·
verified ·
1 Parent(s): 5a35818

Upload mate-monitor.c

Browse files
Files changed (1) hide show
  1. mate-monitor.c +112 -0
mate-monitor.c ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //COMPILE With gcc -o mate_monitor mate-monitor.c
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+ #include <dirent.h>
6
+ #include <sys/types.h>
7
+ #include <sys/stat.h>
8
+ #include <unistd.h>
9
+ #include <signal.h>
10
+ #include <time.h>
11
+
12
+ #define CHECK_INTERVAL 5
13
+
14
+ int silent_mode = 0;
15
+
16
+ void signal_handler(int sig) {
17
+ if (!silent_mode) printf("\nMonitor detenido.\n");
18
+ exit(0);
19
+ }
20
+
21
+ int count_desktop_files(const char *path) {
22
+ DIR *dir = opendir(path);
23
+ if (!dir) return 0;
24
+
25
+ int count = 0;
26
+ struct dirent *entry;
27
+ while ((entry = readdir(dir)) != NULL) {
28
+ char *ext = strrchr(entry->d_name, '.');
29
+ if (ext && strcmp(ext, ".desktop") == 0) {
30
+ count++;
31
+ }
32
+ }
33
+ closedir(dir);
34
+ return count;
35
+ }
36
+
37
+ void execute_pkill() {
38
+ if (!silent_mode) printf("Ejecutando pkill mate-panel...\n");
39
+
40
+ pid_t pid = fork();
41
+ if (pid == 0) {
42
+ // Proceso hijo
43
+ setsid();
44
+ if (silent_mode) {
45
+ // Modo silencioso
46
+ execlp("pkill", "pkill", "mate-panel", NULL);
47
+ } else {
48
+ // Modo normal
49
+ execlp("pkill", "pkill", "mate-panel", NULL);
50
+ }
51
+ exit(0);
52
+ }
53
+ }
54
+
55
+ int main(int argc, char *argv[]) {
56
+ // Verificar argumento silencioso
57
+ if (argc > 1 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--silent") == 0)) {
58
+ silent_mode = 1;
59
+ }
60
+
61
+ if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
62
+ printf("Uso: %s [-s|--silent]\n", argv[0]);
63
+ printf("Monitoriza cambios en archivos .desktop y ejecuta pkill mate-panel\n");
64
+ return 0;
65
+ }
66
+
67
+ if (!silent_mode) {
68
+ printf("Monitor de archivos .desktop (pkill version)\n");
69
+ printf("Monitoreando cambios cada %d segundos...\n", CHECK_INTERVAL);
70
+ printf("Presiona Ctrl+C para salir\n\n");
71
+ }
72
+
73
+ signal(SIGINT, signal_handler);
74
+
75
+ char home_path[256];
76
+ snprintf(home_path, sizeof(home_path), "%s/.local/share/applications", getenv("HOME"));
77
+
78
+ int old_local_count = count_desktop_files(home_path);
79
+ int old_usr_count = count_desktop_files("/usr/share/applications");
80
+
81
+ if (!silent_mode) {
82
+ printf("Archivos iniciales: \n");
83
+ printf(" %s: %d archivos .desktop\n", home_path, old_local_count);
84
+ printf(" /usr/share/applications: %d archivos .desktop\n\n", old_usr_count);
85
+ }
86
+
87
+ while (1) {
88
+ sleep(CHECK_INTERVAL);
89
+
90
+ int new_local_count = count_desktop_files(home_path);
91
+ int new_usr_count = count_desktop_files("/usr/share/applications");
92
+
93
+ if (new_local_count != old_local_count || new_usr_count != old_usr_count) {
94
+ if (!silent_mode) {
95
+ time_t now = time(NULL);
96
+ struct tm *tm_info = localtime(&now);
97
+ char time_str[20];
98
+ strftime(time_str, sizeof(time_str), "%H:%M:%S", tm_info);
99
+ printf("[%s] Cambios detectados!\n", time_str);
100
+ printf(" Local: %d -> %d archivos\n", old_local_count, new_local_count);
101
+ printf(" Usr: %d -> %d archivos\n", old_usr_count, new_usr_count);
102
+ }
103
+
104
+ old_local_count = new_local_count;
105
+ old_usr_count = new_usr_count;
106
+
107
+ execute_pkill();
108
+ }
109
+ }
110
+
111
+ return 0;
112
+ }