gpt4 book ai didi

C 语言实现的 cd 命令

转载 作者:行者123 更新时间:2023-11-30 17:37:47 26 4
gpt4 key购买 nike

I have edited my code now and cd is working to a certain point. 

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>


#define MAX_COMMAND_SIZE 80
#define MAX_ARGS 9
#define HIS_SIZE 100


typedef struct
{
int argument; // userCom arguments
char *arg[MAX_ARGS + 1]; // userCom arguments array
char *input; // hold iniut file
char *output; // hold outiut file
} Command;

int main()
{
Command userCom = {0}; //holds userCom struct
const char *whitespace = " \n\r\t\v\f"; // userCom delimiting chars
char* username = getenv("USER"); //Get user name
char* curDirect = getenv("HOME"); //get cwd
char* token[MAX_ARGS];
char* cwd;
char* buf;
char buffer[MAX_COMMAND_SIZE + 1]; //hold userCom line
int tok = 0;
int new;
long size;
int in = 0;
int i;
struct stat buff; //holds file information



size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL)
cwd = getcwd(buf, (size_t)size);

while(1){


//prints users prompt
printf("\n%s@myshell:%s>", username,cwd);

//gets string from userCom line
fgets(buffer, MAX_COMMAND_SIZE + 1, stdin);


//parses tokens and looks for delimiters
token[tok] = strtok(buffer,whitespace);
while(token[tok])
{
++tok;
if(tok == MAX_ARGS)
printf("Reached MAX userCom arguments");
break;

token[tok] = strtok(NULL, whitespace);
}

i =0;
for (;i<tok;++i)

{
if(!strcmp(token[i], "<"))
{
userCom.output = token[++i];
}
else if(!strcmp(token[i], ">"))
{
userCom.input = token[++i];
}
else if (token[i][0] == '$')
{
char* toktok = getenv((const char*)&token[i][1]);

if (!toktok)
{
printf("%s: ERROR: variable.\n", token[i]);
return 0;
}
else
{
userCom.arg[userCom.argument] = toktok;
++(userCom.argument);
}
}

else
{
userCom.arg[userCom.argument] = token[i];
++(userCom.argument);

}
}
tok = 0;
userCom.arg[userCom.argument] = 0;


if((strcmp(userCom.arg[0],"cd") == 0))
{
if (userCom.argument > 2)
printf("cd: Too many arguments\n");

// change directories if valid target and update cwd

else if (userCom.argument == 1)
{
new = chdir(cwd);
if (new != 0)
printf("%s: No such file or directory\n");

// if no argument is given, new directory should be $HOME

else
{
new = chdir(curDirect);

// get the new current working directory

size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL)
cwd = getcwd(buf, (size_t)size);
}
}
}//end "cd" function

cd 的第一个条目是正确的,但是当我第二次输入 cd 时,它停留在 sgraham,而它应该在类里面。 “结果”

sgraham@myshell:/home/class/sgraham/proj1>cd ..

sgraham@myshell:/home/class/sgraham>cd ..

sgraham@myshell:/home/class/sgraham>cd ..cd:参数太多

最佳答案

所有userCom.Listcomm[i]都初始化为NULL,并且在用于strcmp之前不再更改,这会导致段错误。

关于C 语言实现的 cd 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286708/

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