1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6
7
8char *str_trim(char *s) {
9    char *start, *last, *bk;
10    int len;
11
12    start = s;
13    while (isspace(*start))
14        start++;
15
16    bk = last = s + strlen(s) - 1;
17    while (last > start && isspace(*last))
18        last--;
19
20    if ((s != start) || (bk != last)) {
21        len = last - start + 1;
22        strncpy(s, start, len);
23        s[len] = '\0';
24    }
25    return s;
26}
27
28int exec_with_result_line(char *cmd, char *result, int len)
29{
30    FILE *fp = NULL;
31	if (!cmd || !result || !len)
32		return -1;
33    fp = popen(cmd, "r");
34    if (!fp)
35        return -1;
36    fgets(result, len, fp);
37    str_trim(result);
38    pclose(fp);
39	return 0;
40}
41
42
43#include <stdio.h>
44#include <string.h>
45#include <stdlib.h>
46#include <arpa/inet.h>
47
48int check_same_network(char *ip1, char *netmask, char *ip2) {
49    struct in_addr addr1, addr2, mask;
50
51    if (inet_pton(AF_INET, ip1, &addr1) != 1) {
52        printf("Invalid IP address: %s\n", ip1);
53        return -1;
54    }
55    if (inet_pton(AF_INET, netmask, &mask) != 1) {
56        printf("Invalid netmask: %s\n", netmask);
57        return -1;
58    }
59    if (inet_pton(AF_INET, ip2, &addr2) != 1) {
60        printf("Invalid IP address: %s\n", ip2);
61        return -1;
62    }
63
64    if ((addr1.s_addr & mask.s_addr) == (addr2.s_addr & mask.s_addr)) {
65        return 1;
66    } else {
67        return 0;
68    }
69}
70