fgets strlen etc

    FILE *wordFile = fopen( argv[1], "r");
    char word[10];

    while (fgets(word, 10, wordFile)) {

        word[ strlen(word) - 1 ] = 0;
        NSLog(@"%s", word );
    }
    
    return (0);


今天在看object c, 看到了fgets方法,发现自己竟然给忘记了, 那不行呀, 赶紧找回来。


其中fgets会读取一个文件描述符的一行,当遇到\n或者缓冲区到达n-1个结束,当然也包含\n,同时最长只能读取n-1个,最后一个会被置为NULL.

strlen则计算长度到\0, 并不包括\0



bool upnp_bind = false; #define MAX_LINE_LENGTH 256 char* get_eth0_ip() { FILE *fp; static char ip[16] = ""; char line[MAX_LINE_LENGTH]; int eth0_detected = 0; if ((fp = popen("ifconfig", "r")) == NULL) { DBG_ERR("Failed to execute ifconfig command"); return NULL; } while (fgets(line, sizeof(line), fp) != NULL) { if (strstr(line, "eth0") && !isspace(line[0])) { eth0_detected = 1; DBG_ERR("eth0 interface detected"); continue; } if (eth0_detected) { DBG_ERR("Parsing eth0 details: %s", line); char *inet_ptr = strstr(line, "inet addr:"); if (!inet_ptr) inet_ptr = strstr(line, "inet "); if (inet_ptr) { inet_ptr += (strstr(line, "inet addr:")) ? strlen("inet addr:") : strlen("inet "); char *end = strchr(inet_ptr, ' '); if (end) *end = '\0'; strncpy(ip, inet_ptr, sizeof(ip)-1); DBG_ERR("Found eth0 IP: %s", ip); break; } if (strlen(line) > 0 && !isspace(line[0])) { DBG_ERR("Leaving eth0 section"); break; } } } if (strlen(ip) == 0) { DBG_ERR("eth0 IP not found"); } pclose(fp); return (strlen(ip) > 0) ? ip : NULL; } int execute_upnpc_command(const char* ip, int internal_port, int external_port, const char* protocol) { char command[256]; snprintf(command, sizeof(command), "upnpc -a %s %d %d %s", ip, internal_port, external_port, protocol); DBG_ERR("Executing UPnP command: %s", command); int status = system(command); if (WIFEXITED(status)) { int exit_code = WEXITSTATUS(status); DBG_ERR("Command exited with code: %d", exit_code); if (exit_code == 0) { return 0; } else { return exit_code; } } else { DBG_ERR("Command execution failed "); return -1; } } int delete_upnpc_mapping(int external_port, const char* protocol) { char command[256]; snprintf(command, sizeof(command), "upnpc -d %d %s", external_port, protocol); DBG_ERR("Deleting UPnP mapping: %s", command); int status = system(command); if (WIFEXITED(status)) { int exit_code = WEXITSTATUS(status); DBG_ERR("Delete command exited with code: %d", exit_code); return exit_code; } else { DBG_ERR("Delete command execution failed"); return -1; } } static void timer_cb(struct uloop_timeout *timeout) { if(!upnp_bind){ DBG_ERR("Attempting UPnP binding"); char *p = get_eth0_ip(); DBG_ERR("get_eth0_ip() returned: %s", p ? p : "NULL"); if(!p) { return; } int del_res = delete_upnpc_mapping(19443, "tcp"); if (del_res != 0) { DBG_ERR("Failed to delete existing port mapping for 19443/tcp"); } int res = execute_upnpc_command(p, 443, 19443, "TCP"); int res1 = execute_upnpc_command(p, 437, 19437, "TCP"); int res2 = execute_upnpc_command(p, 8088, 18088, "TCP"); int res3 = execute_upnpc_command(p, 21443, 21443, "TCP"); DBG_ERR("execute_upnpc_command() returned: %d", res); if(res != 0||res1 !=0||res2 !=0||res3 !=0) { } else { upnp_bind = true; DBG_ERR("UPnP bind successful"); } } else { DBG_ERR("UPnP already bound, skipping"); } } static struct uloop_timeout timer = { .cb = timer_cb, }; 增加一个功能,执行upnpc -l获取映射信息,和/etc/config/upnpc中的配置信息进行比较,如果不一样则执行reconfigure。
10-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值