#include <stdio.h> #include<string.h> #define CFG_MAXARGS 10 //parse_line函数参考uboot源码 static int parse_line (char *line, char *argv[]) { int nargs = 0; while (nargs < CFG_MAXARGS) { /* skip any white space */ while ((*line == ' ') || (*line == ' ')) { ++line; } if (*line == ' ') { /* end of line, no more args */ argv[nargs] = NULL; return (nargs); } argv[nargs++] = line; /* begin of argument string */ /* find end of string */ while (*line && (*line != ' ') && (*line != ' ')) { ++line; } if (*line == ' ') { /* end of line, no more args */ argv[nargs] = NULL; return (nargs); } *line++ = ' '; /* terminate current arg */ } return (nargs); } //解析命令行参数 int main() { int argc,i; char buf[1024]="set_network 2 ssid "wifiUncleWang""; char *argv[CFG_MAXARGS]; // strncpy(buf, cmd, 1024); // buf[1023] = ' '; argc = parse_line(buf, argv); for (i=0; i < argc; i++) printf(" %s ",argv[i]); return 0; }
运行结果:
set_network
2
ssid
"wifiUncleWang"