summaryrefslogtreecommitdiff
path: root/hosts.c
blob: fc0db3435021f529a96a1c42b2793b8c6425d76c (plain)
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
#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;
}