我正在用C语言进行shell程序。我正在研究" !!"获取上一个命令并执行它的命令。我试图将* currentargv []内容复制到* prevargv [],以便保存刚刚给出的命令供将来使用。我试图使用strcpy但不确定它是否适用于多维数组。这是我的代码的一部分:
/*We are going to save in 'buffer' the words (separated by the null char)
*given by user which are going to be processed by parse(). 'buffer' is going
* to be reused on the next prompt and previous contents are going to be
* overwritten, that is why i want to copy its contents to another place.
*/
char buffer[STORAGE];
/*They only point to contents in buffer*/
char *outputfile;
char *inputfile;
char *errfile;
/*cmd.newargv is going to store the program name in cmd.newargv[0];
* the other blocks of memory are going to store the arguments to the
*given to the program i.e 'echo hello world'. All this pointers are
*fetched from buffer
*/
struct Command{
int indx;
char **newargv;
}cmd;
struct Save {
int indx;
char *newargv[STORAGE];
char outfile[STORAGE];
char infile[STORAGE];
char errfile[STORAGE];
}prevCmd;
//main()
//prompt for user for input
//parse input
//call saveCommand "explicitly used for the '!!' command"
//execute command (fork(), execvp(), etc.)
void saveCommand {
strcpy(prevCmd.inputfile, inputfile);
strcpy(prevCmd.outputfile, outputfile);
strcpy(prevCmd.errfile, errfile);
//Here want to copy contents in cmd.newargv to prevCmd.newargv.
}
为了简单起见,我在saveCommand上省略了很多代码。此外,我不确定这是否是实施" !!"的最佳方式。命令功能。如果你知道实现这个的另一种方式,我真的很想知道它。感谢