summaryrefslogtreecommitdiff
path: root/hosts.c
diff options
context:
space:
mode:
authormarlonivo <email@marlonivo.xyz>2025-04-14 09:15:53 +0000
committermarlonivo <email@marlonivo.xyz>2025-04-14 09:15:53 +0000
commita8313835d576ed22514eea689fa185ce9fcdf824 (patch)
treecc35147411b3ea27e6340635b87a704130b3735f /hosts.c
initialHEADmaster
Diffstat (limited to 'hosts.c')
-rw-r--r--hosts.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/hosts.c b/hosts.c
new file mode 100644
index 0000000..fc0db34
--- /dev/null
+++ b/hosts.c
@@ -0,0 +1,113 @@
+#include <ncurses.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAX_LINES 1024
+#define MAX_LINE_LEN 512
+
+char *lines[MAX_LINES];
+int total_lines = 0;
+int current_line = 0;
+
+void load_hosts_file() {
+ FILE *fp = fopen("/etc/hosts", "r");
+ if (!fp) exit(1);
+
+ char buffer[MAX_LINE_LEN];
+ while (fgets(buffer, sizeof(buffer), fp) && total_lines < MAX_LINES) {
+ lines[total_lines] = strdup(buffer);
+ total_lines++;
+ }
+
+ fclose(fp);
+}
+
+void cleanup() {
+ for (int i = 0; i < total_lines; i++) {
+ free(lines[i]);
+ }
+}
+
+void draw_screen() {
+ clear();
+ int max_y, max_x;
+ getmaxyx(stdscr, max_y, max_x);
+
+ for (int i = 0; i < max_y && (i + current_line) < total_lines; i++) {
+ mvprintw(i, 0, "%s", lines[i + current_line]);
+ }
+
+ refresh();
+}
+
+int main() {
+ load_hosts_file();
+
+ initscr();
+ noecho();
+ cbreak();
+ keypad(stdscr, TRUE);
+
+ int ch;
+ draw_screen();
+ while ((ch = getch()) != 'q') {
+ switch (ch) {
+ case 'j':
+ if (current_line < total_lines - 1) current_line++;
+ break;
+ case 'k':
+ if (current_line > 0) current_line--;
+ break;
+ case 'c': {
+ for (int i = 0; i < total_lines; i++) {
+ if (lines[i][0] == '\n' || lines[i][0] == '#') {
+ free(lines[i]);
+ lines[i] = NULL;
+ }
+ }
+ int new_total_lines = 0;
+ for (int i = 0; i < total_lines; i++) {
+ if (lines[i] != NULL) {
+ lines[new_total_lines++] = lines[i];
+ }
+ }
+ total_lines = new_total_lines;
+ draw_screen();
+ break;
+ case 'a': {
+ echo();
+ char url[256];
+ mvprintw(LINES - 1, 0, "URL oder Pfad: ");
+ getnstr(url, 255);
+ noecho();
+
+ char tmpfile[] = "/tmp/hosts_tmp_XXXXXX";
+ int fd = mkstemp(tmpfile);
+ close(fd);
+
+ char cmd[512];
+ snprintf(cmd, sizeof(cmd), "curl -fsSL '%s' -o %s", url, tmpfile);
+ if (system(cmd) == 0) {
+ FILE *fp = fopen(tmpfile, "r");
+ if (fp) {
+ char buffer[MAX_LINE_LEN];
+ while (fgets(buffer, sizeof(buffer), fp) && total_lines < MAX_LINES) {
+ lines[total_lines++] = strdup(buffer);
+ }
+ fclose(fp);
+ }
+ }
+ unlink(tmpfile);
+ draw_screen();
+ break;
+ }
+ }
+ draw_screen();
+ }
+
+ endwin();
+ cleanup();
+ return 0;
+}
+