我正在研究C中的一个项目。它是一个原始的文件系统shell,其中一个命令可以在它之后接收一个字符串。以下是我从ftp
读取命令的方法。 (这种方式由我的导师指定):
stdin
因此fsys* f = newFileSystem();
char cmd[21];
char path[1001];
while(scanf("%s", cmd) > 0){
if(strcmp(cmd, "ls") == 0){
// this is probably the wrong way to go about it but...
if(fgets(path, 1001, stdin)){ // 1001 is max path length
strtok(path, "\n"); // doesn't remove if path is only \n
// fgets reads in \n character and it affects output
// later, so getting rid of it now
strtok(path, " "); // doesn't remove space between ls and path name
fs_ls(f, path);
}
}
else if(strcmp(cmd, "pwd") == 0){
// prints path to cwd beginning at root
fs_pwd(f);
// some other commands in here
else{
printf("Command not recognized.");
}
}
可以不接受任何路径(默认为当前工作目录),也可以接收路径并列出该位置的文件和目录。
我可以在if语句中写什么" if(strcmp(cmd," ls")== 0)"系统检查ls之后是否有路径并正确执行ls?