提交 7dd18118 编写于 作者: H handyohos

feat: write command support multiple args

1)write command support multiple args
Signed-off-by: Nhandyohos <zhangxiaotian@huawei.com>
Change-Id: Ib021745e8fdfc965176329ff24bd65e197a5585b
上级 2ced3fab
...@@ -93,9 +93,9 @@ const struct CmdArgs *GetCmdArg(const char *cmdContent, const char *delim, int a ...@@ -93,9 +93,9 @@ const struct CmdArgs *GetCmdArg(const char *cmdContent, const char *delim, int a
struct CmdArgs *ctx = (struct CmdArgs *)calloc(1, sizeof(struct CmdArgs) + sizeof(char *) * (argsCount + 1)); struct CmdArgs *ctx = (struct CmdArgs *)calloc(1, sizeof(struct CmdArgs) + sizeof(char *) * (argsCount + 1));
INIT_ERROR_CHECK(ctx != NULL, return NULL, "Failed to malloc memory for arg"); INIT_ERROR_CHECK(ctx != NULL, return NULL, "Failed to malloc memory for arg");
ctx->argc = 0; ctx->argc = 0;
char *p = (char *)cmdContent; const char *p = cmdContent;
char *end = (char *)cmdContent + strlen(cmdContent); const char *end = cmdContent + strlen(cmdContent);
char *token = NULL; const char *token = NULL;
do { do {
// Skip lead whitespaces // Skip lead whitespaces
while (isspace(*p)) { while (isspace(*p)) {
...@@ -463,6 +463,21 @@ static void DoMount(const struct CmdArgs *ctx) ...@@ -463,6 +463,21 @@ static void DoMount(const struct CmdArgs *ctx)
} }
} }
static int DoWriteWithMultiArgs(const struct CmdArgs *ctx, int fd) {
char buf[MAX_CMD_CONTENT_LEN];
/* Write to proc files should be done at once */
buf[0] = '\0';
strcat_s(buf, sizeof(buf), ctx->argv[1]);
int idx = 2;
while (idx < ctx->argc) {
strcat_s(buf, sizeof(buf), " ");
strcat_s(buf, sizeof(buf), ctx->argv[idx]);
idx++;
}
return write(fd, buf, strlen(buf));
}
static void DoWrite(const struct CmdArgs *ctx) static void DoWrite(const struct CmdArgs *ctx)
{ {
// format: write path content // format: write path content
...@@ -475,11 +490,17 @@ static void DoWrite(const struct CmdArgs *ctx) ...@@ -475,11 +490,17 @@ static void DoWrite(const struct CmdArgs *ctx)
} else { } else {
fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR); fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR);
} }
if (fd >= 0) { if (fd < 0) {
size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1])); return;
INIT_CHECK_ONLY_ELOG(ret >= 0, "DoWrite: write to file %s failed: %d", ctx->argv[0], errno); }
close(fd); size_t ret;
if (ctx->argc > 2) {
ret = DoWriteWithMultiArgs(ctx, fd);
} else {
ret = write(fd, ctx->argv[1], strlen(ctx->argv[1]));
} }
INIT_CHECK_ONLY_ELOG(ret >= 0, "DoWrite: write to file %s failed: %d", ctx->argv[0], errno);
close(fd);
} }
static void DoRmdir(const struct CmdArgs *ctx) static void DoRmdir(const struct CmdArgs *ctx)
...@@ -557,7 +578,7 @@ static const struct CmdTable g_cmdTable[] = { ...@@ -557,7 +578,7 @@ static const struct CmdTable g_cmdTable[] = {
{ "export ", 2, 2, DoExport }, { "export ", 2, 2, DoExport },
{ "rm ", 1, 1, DoRm }, { "rm ", 1, 1, DoRm },
{ "rmdir ", 1, 1, DoRmdir }, { "rmdir ", 1, 1, DoRmdir },
{ "write ", 2, 2, DoWrite }, { "write ", 2, 10, DoWrite },
{ "stop ", 1, 1, DoStop }, { "stop ", 1, 1, DoStop },
{ "reset ", 1, 1, DoReset }, { "reset ", 1, 1, DoReset },
{ "copy ", 2, 2, DoCopy }, { "copy ", 2, 2, DoCopy },
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册