私はシェルプログラムを作成しており、値をexecv()
に渡すときには、プログラムの名前(ls
)です。引数のcharポインタの配列の配列へのポインタが必要です。char * = "ls 0"の配列を指し示すChar *は、 "ls 0"に相当する文字列と等しくありません。
ユーザーの入力を調べて、ls
と入力した場合、char *
を印刷するとprintf()
が "ls"を出力します。だから私は正しく行を解析し、正しい情報を格納しています。これをexecv()
に渡すと、不正なパス名が表示されますが、手動でポインタをprogs[0] = "ls"
に変更すると動作します。 2つの文字列、つまりstrcmp(mypointer, "ls")
を比較すると、mypointerは"ls"
を出力しますが、それは"ls"
と等価ではありません。
誰でも知っていますか?ここで
は私のシェルプロセスコードです:
#include <string.h>
#include <stdio.h>
#include "myshell.h"
int parse(char * buff){
//Initialize all variables and pointers
int cmd = 0;
int argument = 0;
int mod = 0;
int j = 0;
int hitargs = 0;
int gotcommand = 0;
int multiarg = 0;
char ** argptr = arguments[cmd];
char * ptr1 = progs[cmd];
char * argptr2 = argptr[argument];
char * ptr2 = mode[mod];
while(buff[j] != '\0'){
switch(buff[j]){
case ';':
cmd++;
argument = 0;
multiarg = 1;
*argptr2++ = '\0';
argptr = arguments[cmd];
argptr2 = argptr[argument];
ptr1 = progs[cmd];
*ptr2 = buff[j];
mod += 2;
ptr2 = mode[mod];
case ' ':
if(gotcommand == 0){
break;
}
else{
if(hitargs == 0){
hitargs = 1;
*ptr1++ = '\0';
argument++;
argptr2 = argptr[argument];
}
else{
argument++;
argptr2 = argptr[argument];
}
break;
}
default:
if(gotcommand == 0){
*ptr1++ = (char) buff[j];
*argptr2++ = buff[j];
gotcommand = 1;
}
else if(gotcommand == 1 && hitargs == 0){
*ptr1++ = (char) buff[j];
*argptr2++ = buff[j];
}
else if(gotcommand == 1 && hitargs == 1){
*argptr2++ = buff[j];
}
}
j++;
}
*argptr2++ = '\0';
*ptr1++ = '\0';
int cmdflag = 0;
int spaceflag = 0;
int argflag = 0;
int cmdct = 1; //account for null
int argumentct = 1; //account for null termination
return 1;
}
ランダムのprintf文については申し訳ありません:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
char **progs;
char ***arguments;
char **mode;
char pathname[] = "/bin/";
int main(int argc, char ** argv){
//printf("\n %s \n", progs);
//fflush(stdout);
char buff[100];
FILE *p;
p = fdopen(0, "r"); //opens FD 0 (Stdin) as a stream
char * pathname;
while(1){
//printf("I'm at the top");
if(isatty(1) == 1){ //check to see if stdout is going to the terminal
printstart(); //if so, print
}
fgets(buff, 100, p); // Gets the input from the stdin and puts it in buff
int processid = fork(); //fork into child to complete task
if(processid == 0){
//initialize all variables
int numcmd = countcmd(buff);
int pipes = countpipes(buff);
int i;
int j;
//allocate memory for tokenization
progs = malloc(numcmd * sizeof(char *));
arguments = malloc((numcmd) * sizeof(char *));
mode = malloc((numcmd*2) * sizeof(char *));
for(i = 0; i < numcmd; i++){
progs[i] = malloc(10* sizeof(char *));
mode[i] = malloc(10 * sizeof(char *));
mode[2*numcmd-1-i] = malloc(10 * sizeof(char*));
arguments[i] = malloc(15 * sizeof(char *));
for(j = 0; j < 15; j++){
arguments[i][j] = malloc(15 * sizeof(char*));
}
}
/////////////////////////////////////////////////////////////////////
parse(buff); //parses input and places it in the static progs
for(i = 0; i < 1; i++){
printf("\n This is progs %s", arguments[0][0]);
char temp[25];
//strcpy(temp, "/bin/");
strcpy(temp, progs[0]);
//strcat(temp, ' \0');
//*progs = "ls";
char * ptr = progs[0];
for(;*ptr != '\0';){
printf("This is what pointer poitns to %c \n", *ptr++);
}
printf("This is the program: <<%s>>", progs[0]);
fflush(stdout);
char * argument[2];
argument[0] = "ls";
argument[1] = '\0';
char * hell = "l\0";
printf("This is the value of comparison %d\n", strcmp(progs[0], hell));
char **temparg = arguments[0];
//char temp[20] = progs[0];
errno = 0;
execvp("ls", *argument);
char * error = strerror(errno);
printf("This is the error %s", error);
return;
}
}
else{
int status;
waitpid(processid, &status, WIFEXITED(status));
}
}
return 0;
}
ここに私のパース()コードがあります。
while (buff[j] != '\0' && buff[j] != '\n')
これはあなたが追加しない保証します:あなたは、解析コードに以下を追加する必要がある他のバグが、「1!」問題へのi番目の点があるかもしれないので、全体のコードに見えない
コードを表示します。 –
問題をコンピュータに記述することができず、プログラムが表示されることを期待していないのと同じように、この問題と説明を表示することはできません。両方のソリューションは同じです、コードを使用! –
私が推測しなければならないのは、あなたの文字列が '' ls "ではなく' 'ls \ n" 'であると言うでしょう。幸いにも、私は推測する必要はないので、私はしません。 –