用C语言写一个自己的shell-Part Ⅲ--built-in commands

news2024/7/6 20:50:03

Part Ⅲ–Built-in commands

Just as we have mentioned in part Ⅱ,the exec family of functions can’t perform built-in commands like cd.

It’s like
在这里插入图片描述
在这里插入图片描述

The reason for this is that cd is not a system command like ls or pwd.We need to write and invoke functions to realize commands such as cd.

Imagine that if cd were a system command,the workflow would be like this:

  1. the user sends the input cd /
  2. the shell forks the current process and executes the function(s) corresponding to cd
  3. After a successful invocation,the child’s lifecycle ends and it exits.The control is returned to the parent process.
  4. The current working directory of parent has not changed,since the command was executed in the child.As a result,though the cd command is successfully carried out,the produce is not we desired.

Therefore,to successfully realize it,we have to implement it on our own.We also need to inspect if the command we input is cd(or maybe other built-in commands).If it is,we will not fork the process and move on to wait for the next input.

Nevertheless,pwd and echo are built-in commands too.Why we can successfully execute built-in commands like them but not cd?
Because cd changes the current directory but pwd and echo do not.The following explanation is given by chatGPT.

the built-in cd command changes the current working directory of the shell process, rather than launching a new process.

When a shell built-in command is executed, it is handled directly by the shell itself, without invoking a new process. In other words, the shell executes the built-in command directly in its own process, rather than creating a new process to execute the command.

The execvp() function, on the other hand, is used to create a new process and execute a new program within that process. When execvp() is called, it loads the specified executable file into the new process's memory and starts executing it.

So, since cd is a shell built-in command that changes the current working directory of the shell process itself, it cannot be implemented using execvp(), which is used to execute a new program in a new process. Instead, the cd command must be implemented as a shell built-in command.

Ops,the /usr/bin/pwd and shell built-in pwd are different.The pwd and echo we execute above are exactly not built-in commands but system call like /usr/bin/pwd and /usr/bin/echo.And actually in the majority of shell like bash,sh and zsh,pwd and echo are built-in commands.So which commands are shell built-in commands?List of Bourne shell built-in commands

cd

Fortunately,there is a chdir function available for us to inplement cd straightforward.It accepts the path as an argument and returns 0 upon a success and -1 upon a failure.

We define our function like this:

int cd(char *path) {
    return chdir(path);
}

And add a check in our main function:

	 while(1) {
        input = readline("mysh> ");//with "mysh> " to be a prompt,the function reads a line of input.
        command = split_input(input);

        if(!command[0]) {//empty commands
            free(input);
            free(command);
            continue;
        }

        if(strcmp(command[0], "cd") == 0) {
            if (cd(command[1] < 0)) {
                perror(command[1]);
            }
            continue;;
        }
     ...

Test it.

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/386012.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

云原生下最火的API网关-APISIX

文章目录一、APISIX是什么&#xff1f;二、APISIX有哪些功能&#xff1f;三、APISIX对比Spring Cloud Gateway、Zuul等其他网关有哪些优势&#xff1f;四、从0到1部署APIXSIX步骤1&#xff1a;准备环境步骤2&#xff1a;安装依赖步骤3&#xff1a;安装APISIX步骤4&#xff1a;配…

成都待慕电商:抖音虚假宣传虚构被比较价格违规细则

为了保护抖音消费者权益&#xff0c;规范创作者商品分享推广秩序&#xff0c;抖音平台制定《「虚假宣传-虚构被比较价格」违规细则》。 来看详细内容&#xff1a;一、什么是“被比较价格”&#xff1f;被比较价格&#xff1a;指创作者通过价格比较的方式宣传商品价格优惠时&…

opencv:运用cv2给视频加水印

前一段时间学了一下计算机视觉的相关知识,今天想了想,觉得可以利用cv2这个模块给视频加上水印,这样从一些方面也可以表明这个视频的原创。 1. 实现原理 小编的实现原理就是使用cv2模块读取视频文件,然后获取到每一张图片,在图片上的相应位置写上相应的字符串(小编最初打算…

MR虚拟直播是什么?

阿酷tony / 2023-3-3 / 长沙MR虚拟直播是通过机器人、虚拟人物、虚拟舞台或虚拟现实技术&#xff0c;将主播和场景实时转化的互动直播方式。MR技术&#xff08;混合现实技术&#xff09;结合大数据、人工智能等技术&#xff0c;可以在虚拟直播中实现更多的自由度和互动性&#…

认识BUG

如何描述 bug一个合格的 bug 描述应该包括以下几个部分&#xff1a;发现问题的版本开发人员需要知道出现问题的版本&#xff0c;才能够获取对应版本的代码来重现故障&#xff0c;并且版本的标识也有利于统计和分析每个版本的质量。问题出现的环境环境分为硬件环境和软件环境&am…

GUI 之 Tkinter编程

GUI 图形界面&#xff0c;Tkinter 是 Python 内置的 GUI 库&#xff0c;IDLE 就是 Tkinter 设计的。 1. Tkinter 之初体验 import tkinter as tkroot tk.Tk() # 创建一个窗口root.title(窗口标题)# 添加 label 组件 theLabel tk.Label(root, text文本内容) theLabel.p…

学习使用Android开发者者文档

Android Lint 错误信息中可看到不兼容代码所属的API级别。也可以在ANdroid开发者文档中查看各API级别特有的类和函数。 越早熟悉使用开发者文档越有利于开发&#xff0c;当然我们不可能记住所有的Android SDK中海量信息&#xff0c;因此学会查阅SDK文档&#xff0c;不断学习新的…

ContextLoaderListener监听器和SSM整合

ContextLoaderListener监听器Spring提供了监听器ContextLoaderListener&#xff0c;实现ServletContextListener接口&#xff0c;可监听ServletContext的状态&#xff0c;在web服务器的启动&#xff0c;读取Spring的配置文件&#xff0c;创建Spring的IOC容器。web应用中必须在w…

关于linux采用桥连接网络模式

关于linux&#xff08;centos&#xff09;采用桥连接网络模式 下载安装VmWare&#xff0c;并创建centos虚拟机 找到自己的虚拟机&#xff0c;点击编辑虚拟机设置-网络适配器-桥接模式 点击编辑-虚拟网络编辑器 点击更改设置-自动桥接 进入系统-修改网络配置文件 #进入到…

一个使用 react+vite3+ts+react-router-dom6v Hooks Admin搭建的轻量级后台管理模板。

react18-vite3-ts-antd4react-router-dom6v 前言 之所以搭这个模板&#xff0c;对于工作上业务需求老是变来变去&#xff0c;就觉得很烦&#xff0c;干脆搭了个admin模板&#xff0c;这样自己熟悉&#xff0c;好根据业务的需求进行一个修改。很多人会说后端管理系统模板都差不…

一文看懂REE OS、TEE OS、CA以及TA概念、架构、流程

目录 一、概念 二、使能方式 三、TEE软件框架 四、TEE软件流程 一、概念 REE&#xff08;Rich Execution Environment&#xff09;&#xff1a;比如Android系统&#xff0c;是一个开放的环境&#xff0c;容易收到恶意软件的攻击&#xff0c;比如敏感数据被窃取、数字版权被…

Mask R-cnn 代码运行报错总结

Mask R-cnn 代码运行报错总结环境版本1. 数据集下载与参数配置2. 运行报错开始报错1报错2报错3报错4报错5报错6参考文章 文章1文章2 环境版本 TensorFlow 2.1.0Python 3.7keras 2.3.1 1. 数据集下载与参数配置 下载链接 https://github.com/matterport/Mask_RCNN/releases …

MySQL存储引擎详解及对比和选择

什么是存储引擎&#xff1f; MySQL中的数据用各种不同的技术存储在文件(或者内存)中。这些技术中的每一种技术都使用不同的存储机制、索引技巧、锁定水平并且最终提供广泛的不同的功能和能力。通过选择不同的技术&#xff0c;你能够获得额外的速度或者功能&#xff0c;从而改善…

Java关键字、标识符、变量数据类型

文章目录关键字标识符标识符的命名规则标识符的命名规范变量变量的数据类型整数类型浮点类型浮点型精度字符类型布尔类型关键字 定义&#xff1a;被 Java 语言赋予了特殊含义&#xff0c;用做专门用途的字符串&#xff08;或单词&#xff09;HelloWorld 案例 中&#xff0c;出现…

7年测试工程师,裸辞掉17K的工作,想跳槽找更好的,还是太高估自己了....

14年大学毕业后&#xff0c;在老师和朋友的推荐下&#xff0c;进了软件测试行业&#xff0c;这一干就是7年时间&#xff0c;当时大学本来就是计算机专业&#xff0c;虽然专业学的一塌糊涂&#xff0c;但是当年的软件测试属于新兴行业&#xff0c;人才缺口比较大&#xff0c;而且…

南卡Neo骨传导运动耳机正式发布,打造音质最强款骨传导耳机

最近中国专业骨传导领先品牌NANK南卡发布全新Neo系列骨传导运动耳机&#xff0c;全新来袭的南卡Neo骨传导运动耳机主打音质使用体验&#xff0c;耳机配置上做到更为强劲升级优化&#xff0c;支持一体化机身&#xff0c;首发无线充设计&#xff0c;IPX6等级防水&#xff0c;升级…

Android---进程间通信机制2

Service Manager(SM)&#xff1a;大管家。管理系统服务的 Ibinder。 1 如何启动 service_manager 服务 SM注册&#xff1a; 1 binder_open(): 打开驱动(设置大小128K)&#xff0c;内存映射 2 binder_become_context_manager(): 设置 SM 为大管家 --- sm 作用&#xff1a;为…

【Python学习笔记】第二十六节 Python PyMySQL

一、什么是 PyMySQL&#xff1f;PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库。可以用它来连接Python和MySQL。如果你追求速度&#xff0c;这是一个很好的选择&#xff0c;因为它比mysql-connector-python快。PyMySQL 遵循 Python 数据库 API v2.0 规范&#x…

【存储】存储特性

存储特性精简配置技术&#xff08;SmartThin&#xff09;SmartThin主要功能容量虚拟化存储空间写时分配&#xff1a;Capacity-on-Write读写重定向&#xff1a;Direct-on-Time应用场景及配置流程存储分层技术&#xff08;SmartTier&#xff09;存储分层工作原理关键技术容量初始…

智能电子标签——商超版价签

2.1英寸TFT黑白电子价签 ★ 快速变价&#xff0c;高效运营 ★ 市场实用&#xff0c;布局物联网未来 ★ 更好客户体验 ★ 降低系统成本&#xff0c;具备竞争力 ★ 2.1英寸黑白红电子价签 ★ 电池低能耗&#xff0c;常规使用三年 ★ 穿透力强不慣障碍 ★ 2.4G载波&#x…