- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是使用 C 实现 cd 系统调用的代码。此代码的问题是它没有进入 if
条件 if(strcmp(buffer,"cd") == 0)
我不明白为什么。
#include<sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include<dirent.h>
#include<error.h>
#define BUFFERSIZE 20
int main(){
char *args[80];
char buffer[BUFFERSIZE];
char *prompt = "OS";
char *a = ">";
printf("%s%s",prompt,a);
fgets(buffer, BUFFERSIZE, stdin);
char *tok;
tok = strtok (buffer," ");
while(buffer != NULL){
buffer[strlen(buffer)-1] = '\0';
pid_t pid;
pid = fork();
if(pid < 0){
fprintf(stderr, "Fork failed");
return 1;
}
else if(pid == 0){
if(strcmp(buffer,"cd") == 0){
tok = strtok(NULL,"\n");
cd(tok);
}
printf("%s%s",prompt,a);
fgets(buffer, BUFFERSIZE, stdin);
}
else{
wait(NULL);
}
}
return 0;
}
int cd(char *pth){
char path[1000];
strcpy(path,pth);
static char *prompt = "OS";
static char *a = ">";
char *token;
char cwd[256];
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/");
strcat(cwd,path);
chdir(cwd);
printf("%s-%s%s",prompt,path,a);
return 0;
}
最佳答案
根据其他人的建议更新了逻辑。
这里不需要子进程。如果你想要多任务处理,那么使用线程。 在后台运行的进程可能需要子进程。
以下程序对我有用:
#include <stdio.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
//#include <error.h>
int hasPrefix(char const *, char const *);
int cd(char *pth);
#define BUFFERSIZE 200
int main(){
char buffer[BUFFERSIZE];
char *prompt = "OS";
char *a = ">";
char *tok;
tok = strtok (buffer," ");
while(buffer != NULL){
bzero(buffer, BUFFERSIZE);
printf("%s%s",prompt,a);
fgets(buffer, BUFFERSIZE, stdin);
if(hasPrefix(buffer,"cd") == 0){
tok = strchr(buffer,' '); //use something more powerful
if(tok) {
char *tempTok = tok + 1;
tok = tempTok;
char *locationOfNewLine = strchr(tok, '\n');
if(locationOfNewLine) {
*locationOfNewLine = '\0';
}
cd(tok);
}
}else{
system("ls"); //for testing the CWD/PWD
}
}
return 0;
}
int hasPrefix(char const *p, char const *q)
{
int i = 0;
for(i = 0;q[i];i++)
{
if(p[i] != q[i])
return -1;
}
return 0;
}
int cd(char *pth){
char path[BUFFERSIZE];
strcpy(path,pth);
char cwd[BUFFERSIZE];
if(pth[0] != '/')
{// true for the dir in cwd
getcwd(cwd,sizeof(cwd));
strcat(cwd,"/");
strcat(cwd,path);
chdir(cwd);
}else{//true for dir w.r.t. /
chdir(pth);
}
return 0;
}
关于c - 使用 C-if 条件实现 cd 系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16094814/
我尝试按照这些说明返回到前一个目录,但结果是“-bash CD:OLDPWD未设置”。。。我怎么能做我想做的事呢?。谢谢。
我认为在 IPython 中使用 ! 前缀的 shell 命令会使系统 shell 实际执行该命令,但似乎并非如此。考虑以下情况,我从 /home/Documents/Rx 开始,启动 IPython
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
有些 Live CD 可以启动操作系统并允许您无需安装即可使用它。 我想知道是否有一种简单的方法来自定义 Live CD,以便它只加载运行一个应用程序所需的内容,然后运行该应用程序。 最佳答案 对于
当我从 Live CD 启动 Linux Mint 时,我能够将文件保存到“文件系统”。但这些文件保存在哪里呢?不可能是光盘,因为它是 CDR。我不认为它存储在 RAM 中,因为它只能保存这么多数据,
创建了以下类,暂时没有接口(interface)/抽象类: 测试 - 用于创建测试数据 音乐 - 应存储所有 CD CD - 应存储艺术家和 CD 标题 + 所有歌曲 艺术家 - 仅包含字符串名称 歌
在 Git 提交和推送之后,Go 管道有时需要一段时间才能启动。即使在手动按下开始按钮后,也可能需要很长时间。有没有一种设置或方法可以让它更快地获取更改并更快地启动?我不介意它是否在服务器上使用更多资
这可能吗? 我的第一个猜测是: C:> Get-WmiObject Win32_CDROMDrive 但是当我尝试这个时,它只告诉我 Caption , Drive , Manufacturer ,
我正在运行 gitlab 作业,在作业中我试图取消设置在 CI/CD 设置中的某些变量。 例如,我有SOME_VARIABLE设置为 然后,在工作定义中,我正在尝试 variables: SOM
我遇到的问题是 cd不在窗口节点中工作: node("Window-node") { stage('unstash'){ echo
我尝试关注these instructions返回到上一个目录,但这会导致“-bash cd: OLDPWD not set”。 我怎样才能做我想做的事? 谢谢。 最佳答案 我也遇到了同样的问题。为
我在postgresql有点情况 我有两个表 t1 和 t2,并且都有一个列具有相同类型的数据“col_data”,col_data 可能只有以下数据 ('ab', 'cd', 'ab,cd', 'c
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我如何在 PowerShell 中编写函数或别名命令,以便 cd 可以做两件事: cd 给定的目录 ls 目录内容 编辑: 我需要的是这个: function ccd { param($pat
我有一个表格,其中填充了某些值,例如 | CODE | NAME | NB: THIS IS A VERY BASIC EXAMPLE | zygnc | oscar alpha |
所以表结构是这样的 CD id | Artist id 我想返回在此表中多次出现的艺术家,例如不止一张 CD。 我使用的是 Mysql,那么执行此操
This question already has answers here: Reference - What does this regex mean?
如果我们使用 cd bin,我们应该能够导航到 bin 文件夹。 那么bin目录前使用$的目的是什么?“cd $bin_dir” 请解释一下 最佳答案 $bin_dir 是环境变量(请阅读此处了解更多
我正在使用express generator 。运行生成器后,它输出以下内容: install dependencies: $ cd . && npm install cd .有什么意
我有一个旧程序可以从磁盘读取/翻录 CD/音乐数据。它是一个自定义程序,可以读取轨道数据并将其转换为 wma/wpa/wav 格式。然后用户输入如何获得许可,程序将数据和音乐文件提取到我们的 LAN。
我是一名优秀的程序员,十分优秀!