【发布时间】:2015-02-13 02:37:13
【问题描述】:
所以我试图从命令行解析一个命令,例如:
cd /mnt/cdrom
来到
- name = "cd"
- argc = 2
- argv = {"cd", "/mnt/cdrom", NULL}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
void parse(char* line, command_t* command) {
// TODO: Check if string is empty or null
while (line[i] != ' ' && line[i] != '\0')
i++;
if (line[i] == ' ') {
}
// TODO: Split the string on whitespace -> should become an array of char
// TODO: The first string is the command, the length of tokens[1:] is the length of the arguments, and tokens[1:] are the arguments
// TODO: Create/fill-in the command struct with the data from above
}
就我所做的而言,我不确定在没有字符串函数的情况下如何在这一点上拆分它。
【问题讨论】:
-
1) 为什么没有
string.h? 2)你到底想解析什么?如果您的命令行类似于my cd /mnt/cdrom,或者甚至带有空格my cd "/home/my directory",您将收到正确的参数argv[0] = my, argv[1] = cd, argv[2] = /home/my directory。 -
对不起,我不清楚我正在制作自己的 shell,我需要解析用户输入的命令。
标签: c