在写入文件之前检查文件中是否存在字符串/单词

标签 c linux

我有点小问题。仅当尝试写入的相同字符串/单词不存在时,我才想写入文件。在我的例子中,它是一个 IP 地址和端口,用“:”分隔。如果我手动写入文件,例如 193...:80 和 193...:22,它指示它们但仍然写入到文件。我的错误在哪里:

#ifdef DEBUG
#define INITIAL_ALLOC 2
#else
#define INITIAL_ALLOC 512
#endif


void BlockIP(char * s);
char *read_line(FILE *fin);
void *right_line(int p,char * s,int ssh,int http);



int main(int argc, char const *argv[])
{
    //struct uci_context *uci;
    //uci = uci_init();

    char *ip = "193.2.2.1 193.2.6.6 193.168.1.1 193.5.5.5"
    //int uci_port = atoi(ucix_get_option(uci, "pack_mon", "manual_blocking", "port"));

    const char s[2] = " ";
    char *token;


    token = strtok(ip, s);

    while( token != NULL ) 
    {
        BlockIP(token);
        token = strtok(NULL, s);
    }

    return(0);
}


void BlockIP(char * s){
    int ssh = 22;
    int http = 80;
    char sshbuffer[2000]= "\0";
    char httpbuffer[2000]= "\0";

    char cmd2[2000] = "\0";
    char cmd[2000] = "\0";

    snprintf(cmd, sizeof(cmd),"iptables -I INPUT 1 -p tcp -s %s --dport 80 -j REJECT",s);
    snprintf(cmd2, sizeof(cmd2),"iptables -I INPUT 1 -p tcp -s %s --dport 22 -j REJECT",s);

    snprintf(sshbuffer, sizeof(sshbuffer),"%s:%d", s,ssh);
    snprintf(httpbuffer, sizeof(httpbuffer),"%s:%d", s,http);


    FILE *fp ,*ft;
    fp = popen(cmd, "r");

    pclose(fp);

    ft = popen(cmd2, "r");

    pclose(ft);

    FILE *fin;
    char *line;


    fin = fopen("/tmp/mymonlog", "r");

    if ( fin ) {
        while ( line = read_line(fin) ) {
            if (strstr(line, sshbuffer)){
                printf("Already exist %s\n",line);
            }else{
                right_line(1,s,ssh,http);

            }
            if(strstr(line, httpbuffer)){
                printf("Already exist %s\n",line);
            }else{
                right_line(2,s,ssh,http);
            }
            free(line);
        }
    }

    fclose(fin);
}

char *read_line(FILE *fin) {
    char *buffer;
    char *tmp;
    int read_chars = 0;
    int bufsize = INITIAL_ALLOC;
    char *line = malloc(bufsize);

    if ( !line ) {
        return NULL;
    }

    buffer = line;

    while ( fgets(buffer, bufsize - read_chars, fin) ) {
        read_chars = strlen(line);

        if ( line[read_chars - 1] == '\n' ) {
            line[read_chars - 1] = '\0';
            return line;
        }

        else {
            bufsize = 2 * bufsize;
            tmp = realloc(line, bufsize);
            if ( tmp ) {
                line = tmp;
                buffer = line + read_chars;
            }
            else {
                free(line);
                return NULL;
            }
        }
    }
    return NULL;
}


void *right_line(int p,char * s,int ssh,int http){

    FILE *pFile,*tFile;

    if(p == 1){
        pFile=fopen("/tmp/mymonlog", "a");


        if(pFile==NULL) {
            perror("Error opening file.");
        }else {
            fprintf(pFile, "%s:%d\n", s,ssh);
        }

        fclose(pFile);
    }else if(p == 2){
        tFile=fopen("/tmp/mymonlog", "a");

        if(tFile==NULL) {
            perror("Error opening file.");
        }
        else {
            fprintf(tFile, "%s:%d\n", s,http);
        }

        fclose(tFile);
    }
}

最佳答案

它仍然写入文件的原因是您以逐行的方式检查文件。

假设您的文件有:

193.2.2.1:22
193.2.2.1:80

并且您想检查 193.2.2.1:22193.2.2.1:80

首先你阅读193.2.2.1:22

strstr("193.2.2.1:22", "193.2.2.1:22")

这是匹配的,所以它不会写入文件。

然后你就这样做了

strstr("193.2.2.1:22", "193.2.2.1:80")

匹配,因此它写入文件。

然后您从文件中读取下一行 193.2.2.1:80 并执行

strstr("193.2.2.1:80", "193.2.2.1:22")

匹配,因此它写入文件。

然后你做

strstr("193.2.2.1:80", "193.2.2.1:80")

这是匹配的,所以它不会写入文件。

现在你的文件是:

193.2.2.1:22
193.2.2.1:80
193.2.2.1:80
193.2.2.1:22

结论:

不要逐行检查。在决定写入文件之前,您需要检查文件中的所有行。

一个简单的修复可能是这样的:

int save_ssh = 1;
int save_http = 1;
if ( fin ) {
    while ( line = read_line(fin) ) {
        if (strstr(line, sshbuffer)){
            printf("Already exist %s\n",line);
            save_ssh = 0;
        }
        if(strstr(line, httpbuffer)){
            printf("Already exist %s\n",line);
            save_http = 0;
        }
        free(line);
    }

    fclose(fin);

    if (save_ssh) right_line(1,s,ssh,http);
    if (save_http) right_line(2,s,ssh,http);
}

关于在写入文件之前检查文件中是否存在字符串/单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43490641/

相关文章:

c - _C99的Bool数据类型

c - stat() 命令在 iOS 上有什么作用?

c - MPI - 工作/池示例

linux - 带有范围查询的 egrep 多个 AND

c++ - 在套接字编程中从服务器捕获客户端导出

linux - Awk:合并文件中的列

c - "too many arguments for format"警告

c - C 中的二分搜索 - 陷入循环

linux - 无法打开: HTTP status was '403 Forbidden' - The "sensor" parameter specified in the request must be set to either "true" or "false"

php - 禁用 wp-cron,并在多站点环境中运行 linux cron