c - cronology 功能可访问最近插入到 C 语言中的 shell Linux 中的命令
问题描述
我正在尝试在我的简单 shell 中创建一个功能,它可以跟踪用户输入的最后 10 个命令。问题是当我在圆形数组中输入命令时,它会被最后输入的命令填充。我不明白为什么,提前谢谢。
在 p[0] 我有命令
主功能提示
void prompt();
void history(char *p[]);
void showBuffer();
char* cbuffer[10] = {0};
int idx = 0;
int main (int argc, char* argv[])
{
//Set the PWD to the Home Directory
chdir(getenv("HOME"));
printf("Shell is running..\n");
printf("\n");
char com[MAX_STRING];
char *pars[MAX_ARGS],buff[MAX_ARGS];
int w, pid, s;
while (TRUE) {
prompt();
w = read_command(com, pars);
//check for the background or foreground implementation
//w=0 -> background
//w=1 foreground
printf("%d \n",w);
// check if no command was read
if (strlen(com) == 0)
continue;
// com are the commands.. /bin/cd..
//pars[0] it' the first token(command) .. pars[1] the second token
pid = fork();
if (pid < 0) { // fork FAILED!
printf("ERROR!: impossible to execute the command: %s", com);
continue;
}
if (pid != 0) // FATHER
if (w)
waitpid(-1, &s, 0);
else continue;
else execv(com, pars); // CHILD
}
printf("terminated");
return 0;
}
void prompt()
{
char hostname[MAX_STRING];
char buff[MAX_ARGS];
gethostname(hostname,MAX_STRING);
printf("@%s",hostname);
printf(": %s",getcwd(buff,MAX_STRING));
printf(" >> ");
}
填充和显示数组的函数
//function that populates the array
void history(char *p[])
{
cbuffer[idx] = p[0];
printf("comando nel BUFFER-------------->> %s \n", cbuffer[idx]);
printf("%d indice, idx \n", idx);
idx++;
if(idx == 10)
{
showBuffer();
idx = 0;
}
}
//show buffer
void showBuffer()
{
for(int i = 0; i < 10; i++) printf("%s e`il comando contenuto\n", cbuffer[i]);
}
函数读取命令和调用历史(char *p[])
int read_command(char *c, char *p[])
{
char tmp[MAX_STRING],copy_path[MAX_STRING], *s;
const char *path=getenv("PATH");
int i;
struct stat buf;
// it reads from the stdin in a SECURE way
get_string(tmp, MAX_STRING);
// the user just push the carriage return
if (strlen(tmp) == 0) {
c[0] = '\0';
return 0;
}
// extract the command name (first token)
s = strtok(tmp, " ");
strcpy(c,s);
//Pass a tmp path for don't change the getenv("PATH");
strcpy(copy_path,path);
// PARAMETER ARRANGEMENT
i=0;
while (s != NULL) {
if (p[i] == NULL)
p[i] = (char *) malloc(MAX_STRING); // MEMORY ALLOCATION
strcpy(p[i++], s);
s = strtok (NULL, " ");
}
p[i] = NULL;
/******* i try to save command in array************/
if(p[0]!=NULL && strcmp(p[0],"!!!") !=0)
{
history(p);
}
else if(strcmp(p[0], "!!!")==0)
{
for(int i = 0; i <= 10; i++){
printf("%d --> %s \n", i, cbuffer[i]);
}
showBuffer();
}
//Foreground
return 1;
}
输出 :
comando nel BUFFER--------->> cd // last command insert, the above commands are different from "cd"
9 indice, idx
cd command
cd command
cd command
cd command
cd command
cd command
cd command
cd command
cd command
cd command
解决方案
推荐阅读
- sql-server - 设置 Azure SQL 数据同步的同步组
- java - 有没有办法通过为变量分配百分比来对数组中的变量进行排序(即按阅读完成进度对书籍进行排序)?
- reactjs - 在 react-native 中的 useEffect 之前获取用户位置
- javascript - 将值限制为 Node.js 中的变量
- python - 如何使用 python 多次重复此脚本
- sql - 循环卡在 Trigger postgresql 上
- google-apps-script - 外出 G Suite 脚本
- vba - MS Access 一个表单以选择表值并用于填充其他表单上的多个字段?
- phpmyadmin - 如何将 phpMyAdmin 连接到 Ampps 中的数据库?
- exoplayer2.x - Exoplayer 离线播放与 DRM