繁体   English   中英

execvp和读取命令参数

[英]execvp and reading command arguments

我正在一个项目中,必须在C中为Linux编写命令外壳。到目前为止,它正在处理没有输入的命令(即,外壳将很好地运行“ date”命令和“ ls”命令)。 )

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

% gcc -o testFile testFile.c

好像外壳程序正在运行gcc作为其自己的命令,然后是-o,然后是testFile和testfile.c(应将gcc作为命令,其他三个条目作为输入)。

我不了解代码中发生了什么-可能是由于不完全了解execvp函数(我从多个来源阅读了该信息,但我仍然不明白我的意思-我以为!)。

execvp调用在函数execute中。

#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

这是命令行的一些输出:

% 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

请参阅scanf的手册页以了解发生这种情况的原因。 问题出在您的格式字符串中:

%S

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

尝试以下方法:

scanf("%[^\n]s", command);
getchar();

要么

scanf(" %[^\n]s", command);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM