You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用strsep()去除空白字符并分割命令行输入?

问题解决方案

你的核心需求是将命令行输入(比如ls -la /tmp)分割为无空白干扰的字符指针数组,同时避免空行/全空白行引发的崩溃,以下是针对你现有代码的改进方案:

1. 修复string_split函数

现有代码的问题是会把连续分隔符产生的空字符串存入数组(比如输入" ls -la "会生成空字符串、ls、空字符串这类无效元素),且未做数组边界检查,容易触发段错误。改进后的代码如下:

#include <string.h>
#include <stdio.h>

int string_split(char *input, char *commands[], int max_args) {
    int i = 0;
    const char *delim = " \t\r\n\a";
    
    // 去除换行符(避免后续分割出空元素)
    input[strcspn(input, "\n")] = '\0';
    
    char *token;
    while ((token = strsep(&input, delim)) != NULL) {
        // 跳过连续分隔符产生的空字符串
        if (*token == '\0') {
            continue;
        }
        // 检查数组容量,防止越界
        if (i >= max_args - 1) {
            fprintf(stderr, "参数数量超出数组最大限制\n");
            break;
        }
        commands[i++] = token;
    }
    // 可选:给数组末尾加NULL,适配exec类系统调用的参数要求
    commands[i] = NULL;
    return i;
}

2. 修复调用逻辑

原代码仅处理了仅含换行的空行,未覆盖全空格、全制表符的情况,同时调用时需传入数组最大容量以确保安全:

#include <stdlib.h>
#include <stdio.h>

int main() {
    char *input = NULL;
    size_t input_size = 0;
    char *command_line[10]; // 最多存储9个有效参数 + 1个NULL
    int res;

    while (1) {
        res = getline(&input, &input_size, stdin);
        
        if (res == -1) {
            free(input);
            exit(0);
        }
        
        // 判断是否为全空白行
        int is_empty = 1;
        for (int j = 0; j < res; j++) {
            if (input[j] != ' ' && input[j] != '\t' && input[j] != '\n' && input[j] != '\r') {
                is_empty = 0;
                break;
            }
        }
        if (is_empty) {
            continue;
        }
        
        // 调用分割函数,传入数组最大容量
        int input_length = string_split(input, command_line, sizeof(command_line)/sizeof(command_line[0]));
        
        // 测试输出验证结果
        printf("分割得到%d个参数:\n", input_length);
        for (int j = 0; j < input_length; j++) {
            printf("参数%d: %s\n", j, command_line[j]);
        }
        
        // 后续处理逻辑...
    }
}

关键改进说明

  • 过滤strsep分割出的空字符串,确保数组仅保留有效参数
  • 增加数组边界检查,避免参数过多导致的内存越界
  • 完善空行判断逻辑,覆盖所有全空白场景
  • 可选在数组末尾添加NULL,适配execvp等系统调用的参数格式要求

内容的提问来源于stack exchange,提问作者Endown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:10:33