gpt4 book ai didi

c - execvp 和读取命令参数

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:07 25 4
gpt4 key购买 nike

我正在做一个项目,我必须在 C 中为 linux 编写命令 shell。到目前为止,它正在为没有输入的命令工作(即,shell 将运行“date”命令和“ls”命令很好。)

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

% gcc -o testFile testFile.c

似乎 shell 正在将 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

Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.

试试这个:

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

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

关于c - execvp 和读取命令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21789461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com