首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >execvp和读取命令参数

execvp和读取命令参数
EN

Stack Overflow用户
提问于 2014-02-14 21:02:39
回答 1查看 959关注 0票数 0

我正在进行一个项目,在这个项目中,我必须用C语言为linux编写一个命令shell。到目前为止,它正在处理没有输入的命令(也就是说,shell将运行'date‘命令和'ls’命令。)

但是,需要输入的命令,似乎将每个输入读入为单独的命令。例如,对于命令:

代码语言:javascript
复制
% gcc -o testFile testFile.c

似乎shell运行gcc作为自己的命令,然后运行-o、testFile和testfile.c,当它应该以gcc作为命令时,其他三个条目作为输入。

我不明白代码中发生了什么--它可能来自于不完全理解execvp函数(我从几个来源了解了它,但我仍然不理解它--我以为我理解了!)

execvp调用在函数执行中。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include "commandStorage.c"

#define MAX_ARGUMENTS 10


void parseLine(char *command, char **args) {

const char split = '\0';
int i;
char *ptrToken;

while(*command != '\0') {
    while ((*command == '\n') || (*command == '\t')
        || (*command == ' ')) {
        *(command++) = '\0';
        } // end 'while'
    *args++ = command;
    printf("%s\n", command);
    while ((*command != '\n') && (*command != '\t')
        && (*command != '\0') && (*command != ' ')) {
        command++;
        } // end 'while'

    } // end 'while'
*args = '\0';
} // end parseLine

void execute(char **arrayOfPtrs) {
/*
CURRENT BUG:
The function is taking the array of pointers, and executing each
input from the array seperately.

The execvp function is being misused.
*/
pid_t pid;
int waitStatus;

switch (pid = fork()) {
    case 0:  // Child process
        if (execvp(arrayOfPtrs[0], arrayOfPtrs) < 0) {
            perror("THE COMMAND FAILED TO EXECUTE!");
            break;

            } // end 'if

    case -1:  // Fork failed
        perror("THE PROCESS FAILED TO FORK");
        break;

    default: // Parent process
        while ((wait(&waitStatus) != pid)) {};
        break;
} // end 'switch'
return;
} // end 'execute

void clearPointerArray(char **args){

while (*args != NULL) {
    *(args++) = NULL;
}
}


int main() {

int i;
char command[MAX_STRING_LENGTH]; // unparsed command
pid_t pid;
char *args[MAX_ARGUMENTS]; // Argument vector.


while(1) {
    clearPointerArray(args);
    printf("%s", "TimsShell--> ");
    scanf("%s", command);

    parseLine(command, args);

    if ((strcmp(args[0], "exit") == 0) || (strcmp(args[0], "quit") == 0)) {
        printf("%s\n", "Goodbye!");
        return 0;
    }

    execute(args);

    }// while loop
return 0;

} // main

下面是命令行的一些输出:

代码语言:javascript
复制
% gcc -o mainShell mainShell.c
% ./mainShell
TimsShell--> date
date
Fri Feb 14 15:50:28 EST 2014
TimsShell--> ls
ls
change.log      doLocalConf.xml   license.txt   notepad++.exe  session.xml               testFile.c
commandStorage.c  functionList.xml  localization  plugins     shortcuts.xml         themes
config.model.xml  langs.model.xml   mainShell      readme.txt     stylers.model.xml    updater
config.xml      langs.xml        mainShell.c   SciLexer.dll     stylers.xml          user.manual
TimsShell--> gcc -o testFile testFile.c
gcc
gcc: fatal error: no input files
compilation terminated.
TimsShell--> -o
THE COMMAND FAILED TO EXECUTE!: No such file or directory
TimsShell--> testFile
THE COMMAND FAILED TO EXECUTE!: No such file or directory
TimsShell--> testFile.c
: not found 2: testFile.c: 
testFile.c: 3: testFile.c: Syntax error: "(" unexpected
TimsShell--> ^C
EN

回答 1

Stack Overflow用户

发布于 2014-02-14 21:13:05

要了解发生这种情况的原因,请参考“手册”页面中的scanf。问题在于您的格式字符串:

%s 匹配非空白字符序列;下一个指针必须是指向字符数组的指针,该指针足够长,足以容纳输入序列和自动添加的终止空字节('\0')。输入字符串以空白或最大字段宽度(以先发生者为准)停止。

试一试:

代码语言:javascript
复制
scanf("%[^\n]s", command);
getchar();

代码语言:javascript
复制
scanf(" %[^\n]s", command);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21789461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档