用C语言写一个自己的shell-Part Ⅱ--execute commands

news2024/9/20 23:26:31

Part Ⅱ–execute commands

Exec

This brings us to the exec family of functions. Namely, it has the following functions:

  • execl
  • execv
  • execle
  • execve
  • execlp
  • execvp

For our needs,we will use execvp whose signature looks like this

int execvp(const char *file, char *const argv[]);

execvp function indicates that,it accepts the name of a file,for which it will search for $PATH variable of the system and an array of arguments to be executed.

A few things to note about the execvp function:

  1. The first argument is the name of the command
  2. The second argument consists of the name of the command and the arguments passed to the command itself. It must also be terminated by NULL.
  3. It also swaps out the current process image with that of the command being executed, but more on that later.

execvp.c

#include <unistd.h>

int main() {
    char *argv[] = {"ls","-l","-h","-a",NULL};
    execvp(argv[0],argv);

    return 0;
}

If you compile and execute the execvp.c, you will see an output similar to the following:

total 32K
drwxrwxr-x 2 marco marco 4.0K  3月  1 22:07 .
drwxrwxr-x 5 marco marco 4.0K  3月  1 22:04 ..
-rwxrwxr-x 1 marco marco  17K  3月  1 22:07 execvp
-rw-rw-r-- 1 marco marco  123  3月  1 22:07 execvp.c

Which is exactly the same if you manually executels -l -h -a

readline

https://tiswww.case.edu/php/chet/readline/rltop.html

With execvp function,we can perform commands in $PATH but how to accept the string of commands as stdin?

This brings us to the Readline Library.

#include <stdio.h>
#include <readline/readline.h>
char * readline (const char *prompt);

However,when I used this function,error occured.

Ubuntu that I used doesn’t have the library by default.Hence,the library need to be installed.

sudo apt-get install libreadline-dev

Meanwhile,we should add an another argument to link the library when compiling the File.c ,otherwise you may see an error like

“undefined reference to `readline’
collect2: error: ld returned 1 exit status”.

gcc File.c -o File -lreadline

And here’s the code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <readline/readline.h>
#include <sys/wait.h>

char **get_input(char *);

int main() {
    //in C,ptr can be used as array.
    char **command;
    char *input;
    pid_t child_pid;
    int stat_loc;

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

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

        child_pid = fork();
        if(child_pid == 0) {
            //child process
            execvp(command[0],command);
        } else {
            waitpid(child_pid, &stat_loc, WUNTRACED);
        }

        free(input);
        free(command);
    }

    return 0;
}

char **get_input(char *input) {
    char **command = malloc(8 * sizeof(char *));
    char *separator = " ";
    char *parsed;
    int index = 0;

    parsed = strtok(input, separator);
    while (parsed != NULL) {
        command[index] = parsed;
        index++;

        parsed = strtok(NULL, separator);
    }

    command[index] = NULL;
    return command;
}

Let’s test it.

在这里插入图片描述

However,the exec family of function can’t perform built-in commands like cd.

在这里插入图片描述

The code with weak robustness should be revised to be more robust and stronger.

  1. Dynamic memory allocation - in char ** get_input(char *input);

    The command variable only malloc 8 sizeof(char *).It’s limited,

    so you will see the following error:

在这里插入图片描述

To handle the error,commandshould malloc dynamic memories.

  1. fork failed - If the OS runs out of memory or reaches the maxmum number of allowed processes,a child process will not be created.We add the following segment to our code:

    		if(child_pid < 0) {
                perror(command[0]);
                exit(1);
            }
    
  2. exev failed - the exev function may fail.We modify the following block to our code:

    			//child process
                if (execvp(command[0], command) < 0) {
                    perror(command[0]);
                    exit(1);
                }
    

The revised code is here,written by chatGPT.The AI is amazing!!!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <readline/readline.h>
#include <sys/wait.h>

char **split_input(char *);

int main() {
    //in C,ptr can be used as array.
    char **command;
    char *input;
    pid_t child_pid;
    int stat_loc;

    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;
        }

        //fork failed.
        child_pid = fork();
        if(child_pid < 0) {
            perror(command[0]);
            exit(1);
        }

        if(child_pid == 0) {
            //child process
            if (execvp(command[0], command) < 0) {
                perror(command[0]);
                exit(1);
            }
        } else {
            waitpid(child_pid, &stat_loc, WUNTRACED);
        }

        free(input);
        free(command);
    }

    return 0;
}
char **split_input(char *input) {
    int capacity = 10;
    char **command = malloc(capacity * sizeof(char *));
    if (command == NULL) {
        fprintf(stderr, "Error: memory allocation failed\n");
        exit(EXIT_FAILURE);
    }

    char *token = strtok(input, " ");
    int i = 0;
    while (token != NULL) {
        if (i == capacity - 1) {  // resize the array if it is full
            capacity *= 2;
            char **new_command = realloc(command, capacity * sizeof(char *));
            if (new_command == NULL) {
                fprintf(stderr, "Error: memory allocation failed\n");
                exit(EXIT_FAILURE);
            }
            command = new_command;
        }

        command[i] = malloc((strlen(token) + 1) * sizeof(char));
        if (command[i] == NULL) {
            fprintf(stderr, "Error: memory allocation failed\n");
            exit(EXIT_FAILURE);
        }
        strcpy(command[i], token);
        token = strtok(NULL, " ");
        i++;
    }
    command[i] = NULL;  // terminate the array with NULL

    return command;
}

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

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

相关文章

【数据库专题】数据库Mongodb之深入认知云计算三种服务方式、mongodb特点、mongodb重要进程 mongod、mongo、其他进程区别

文章目录一、什么是云计算1. IaaS:基础设施即服务2. SaaS:软件即服务3. PaaS:平台即服务二、大数据与云计算关系三、什么是MongoDB四、大数据与MongoDB五、MongoDB特点六、安装MongoDB七、重要进程介绍7.1 mongod进程7.2 mongo进程7.3 其他进程7.3.1 mongodump重建数据库7.3.2 …

解决封号 Walmart最全申诉步骤

最近龙哥听说不少平台的账号都被封掉&#xff0c;登不上去了。所以龙哥赶紧就把这篇Walmart申诉教程提上日程&#xff0c;以防这个不时之需哈。当时Walmart的这个封号申诉规则和社交平台的还是有很大区别的&#xff0c;今天龙哥就从封号原因和申诉流程两方面展开&#xff0c;让…

210 裸机程序烧录

一、驱动安装 1.1 dnw驱动安装 禁用win10驱动程序强制签名 设置 -> 更新和安全 -> 恢复 -> 立即重启 -> 疑难解答 -> 高级选项 -> 启动设置 -> 重启 -> 按提示输入“F7”硬件设备正常上电工作&#xff0c;插入USB线连接电脑&#xff0c;设备管理器识…

源表测试软件下载安装教程

软件&#xff1a;源表测试软件NS-SourceMeter 语言&#xff1a;简体中文 环境&#xff1a;NI-VISA 安装环境&#xff1a;Win10以上版本&#xff08;特殊需求请后台私信联系客服&#xff09; 硬件要求&#xff1a;CPU2GHz 内存4G(或更高&#xff09;硬盘500G(或更高&#xf…

BGP之BGP联邦综合实验

目录 BGP联邦综合实验 实验图 网段划分 基础配置 路由配置 启动AS2中的ospf--- IGP协议 检测IGP 启动AS之间的BGP 测试 发布R1路由 修改&#xff1a;下一跳解决上述问题 解决水平分割 发布R8路由 测试 AS2内部环回路由信息互相访问 配置空接口 发布静态路由 测试…

经典蓝牙Sniff Mode

文章目录IntroductionApplicationSniff Sub-ratingReferenceIntroduction Sniff mode为两个已连接的经典蓝牙设备提供了有效的降低功耗的方法。我们知道&#xff0c;当没有数据需要传输的时候&#xff0c;两个已连接的蓝牙设备之间也需要每两个slots完成一次POLL packet - NUL…

系列九、视图/存储过程/存储函数/触发器

一、视图 1.1、概述 视图&#xff08;View&#xff09;是一种虚拟存在的表。视图中的数据并不在数据库中实际存在&#xff0c;行和列数据来自定义视图的查询中使用的表&#xff0c;并且是在使用视图时动态生成的。 通俗的讲&#xff0c;视图只保存了查询的SQL逻辑&#xff0c;…

pb插入ole控件点击insert+control时自动关闭解决办法

pb插入ole控件点击insert control时pb应用程序自动关闭解决思路 {F2F7F2A6-E582-11D1-89AC-00C04FCAF6E3} {F515306D-0156-11d2-81EA-0000F87557DB} 1、问题现象:当在powerbuilder中插入OLE控件点击insert control时,应用程序自动关闭,如图 在网上查询时有人说是其他…

深度剖析:伊朗钢铁厂入侵路径推测及对钢企数字化安全转型启示

2022年6月27日&#xff0c;名为Gonjeshke Darande的黑客组织声称对隶属于伊朗革命卫队&#xff08;IRGC&#xff09;和伊朗巴斯杰民兵组织&#xff08;Basij&#xff09;的Khouzestan、Mobarakeh、Hormozgan三家钢铁公司开展了网络攻击&#xff0c;致使Khouzestan钢铁厂一台重型…

【论文阅读】Attributed Graph Clustering with Dual Redundancy Reduction(AGC-DRR)

【论文阅读】Attributed Graph Clustering with Dual Redundancy Reduction&#xff08;AGC-DRR&#xff09; 文章目录【论文阅读】Attributed Graph Clustering with Dual Redundancy Reduction&#xff08;AGC-DRR&#xff09;1. 来源2. 动机3. 模型框架4. 方法介绍4.1 基本符…

Linux端口开通

Linux端口开开启有两种方式 firewall方式&#xff08;centos7.*&#xff09;修改iptables&#xff08;centos6.*&#xff09; 一、firewall方式 查看防火墙状态firewall-cmd --state如果返回的是 “not running”&#xff0c;那么需要先开启防火墙&#xff1b; 开启防火墙sy…

Java02 变量和运算符

Java02 变量和运算符 2.1 Java代码的基本格式 //类的定义 修饰符 class 类名{public static void main(String[] args){// 代码System.out.println("Hello World&#xff01;");} }现阶段&#xff0c;可以将一个类理解成Java程序&#xff08;.java文件&#xff09;…

html部分codewhy网课学习笔记

day1 网页显示的过程和服务器 浏览器内核,也称为渲染引擎 head标签描述网页的原数据&#xff0c;如title shifttab是向前缩进 div>ul>li可以快速生成 <div> <ul> <li></li> </ul> </div> 在早期,单标签如<input>也可写为&l…

字节前端架构组工程化代码片段

如何使用交互式的命令行工具下载项目模板 这部分代码实现了一个用户交互的 GitHub 模板下载工具。首先你需要在github上创建一个项目&#xff0c;然后使用下面介绍的代码就可以用命令行拉取到本地&#xff0c;并解压了。 它使用 enquirer 库提示用户输入仓库的创建者、名称、…

npm版本过低造成的各种问题汇总及升级方法

npm does not support Node.js 这个是因为npm和Node.js的版本不合适&#xff0c;版本对照表在这里&#xff1a;https://nodejs.org/zh-cn/download/releases/ 查看node和npm的版本&#xff1a; node -v npm -v试图装一个npm&#xff0c;结果报错&#xff1a;TypeError: Canno…

C++面向对象之多态性

文章目录C面向对象之多态性1.静态多态2.动态多态3.多态的好处3.1使用方法4.纯虚函数5.虚析构与纯虚析构5.1问题5.2解决6.其他知识点7.代码8.测试结果8.1父类中无虚函数&#xff0c;父类的指针指向子类对象&#xff0c;将调用父类中的函数&#xff0c;无法调用子类中的重写函数&…

Qt入门基础

目录 一 学习Qt的动机 二 创建第一个Qt程序 关于main.cpp QApplication Widget 关于对象树 关于Widget常用的API return a.exec() 一 学习Qt的动机 我在学习了C之后&#xff0c;想做图形化界面。实现前后端的交互。这时候就可以利用Qt进行编程。 二 创建第一个Qt程序 Qt…

【RocketMQ】消息的存储

Broker对消息的处理 BrokerController初始化的过程中&#xff0c;调用registerProcessor方法注册了处理器&#xff0c;在注册处理器的代码中可以看到创建了处理消息发送的处理器对象SendMessageProcessor&#xff0c;然后将其注册到远程服务中&#xff1a; public class Brok…

哨声吹响,与世界杯相关的欺诈也在激增

2022 年世界杯已经开始&#xff0c;通过虚假流媒体网站与彩票针对足球迷的诈骗激增。近日&#xff0c;Zscaler 发现与世界杯相关的新注册域名有所增加&#xff0c;尽管并非都是恶意的&#xff0c;也是值得警惕的。 流量趋势 随着世界杯的开赛&#xff0c;从 11 月 21 日流媒体…

Python set集合全部操作方法

文章目录一. 介绍1. 创建set集合2. 判断元素是否在集合内3. 集合推导式(Set comprehension)二. 集合基本操作1. add&#xff08;&#xff09;添加单个元素2. update&#xff08;&#xff09;添加列表&#xff0c;元组&#xff0c;字典等整体数据3. remove ( ) 移除元素&#xf…