c语言编写简单shell解释器

在windows开发环境下写一个简单shell解释器
1、输入一个exe可执行文件路径或命令后能启动该程序
2、输入txt文本文件的路径,能打开该文本文件中所指定的若干exe程序路径及命令打开程序

首先要弄清shell解释器其实就是一个exe文件,在这个程序输入正确
命令,就有相应操作执行。

接着启动程序其实就是在系统中创建进程,
用win32的api CreateaProcess()函数借口实现。
该函数原型如下

CreateProcess(

LPCWSTR lpszImageName, //指向可执行的模块的指针

LPCWSTR lpszCmdLine, //指向可执行命令行字符串的指针

LPSECURITY_ATTRIBUTES lpsaProcess, //CE 不支持

LPSECURITY_ATTRIBUTES lpsaThread, //CE 不支持

BOOL fInheritHandles, //CE 不支持

DWORD fdwCreate, //创建标志

LPVOID lpvEnvironment, //CE 不支持

LPWSTR lpszCurDir, //CE 不支持

LPSTARTUPINFOW lpsiStartInfo, //CE 不支持

LPPROCESS_INFORMATION lppiProcInfo //指向进程信息结构体的指针

);

该函数最重要的两个参数是lpszImageName和lpszCmdLine。
lpszImageName代表应用名
可以直接设为null,只需设定lpszCmdLine为正确的命令行参数即可(即在cmd上运行该程序时需要的命令)。

当输入字符串后
该min_shell.exe首先判断是否是直接打开exe可执行文件,还是需通过txt文本内容打开程序,接着创建程序就好。

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
//判断输入字符串为txt还是执行exe命令
int check(char *s, int len) {
    int j = 0, flag = 0;
    for (int i = 0; i < len; i++) {
        if (s[i] == '.' && len - i > 3 && s[i + 1] == 'e' && s[i + 2] == 'x' && s[i + 3] == 'e')
            flag = 1;
    }
    return flag;
}
//创建程序
void CreateProc(char* s) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    if (!CreateProcess(NULL, s, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        fprintf(stderr, "creat Process Failed.\n");
    }
    else {
        fprintf(stderr, "Successfully created.\n");
    }
    WaitForSingleObject(pi.hProcess, 0);
}
//读取txt文本内容,并创建程序
void ReadTxt(char * argv) {
    // get file
    FILE * pFile;
    fopen_s( &pFile, argv,"rb");
    if (pFile == NULL) {
        fputs("File error", stderr);
        exit(1);
    }

    // obtain file size:
    fseek(pFile, 0, SEEK_END);
    long lSize = ftell(pFile);
    rewind(pFile);

    // allocate memory to contain the whole file:
    char *buffer = (char*)malloc(sizeof(char)*lSize);
    if (buffer == NULL) {
        fputs("Memory error", stderr);
        exit(2);
    }

    // copy the file into the buffer:
    size_t result = fread(buffer, 1, lSize, pFile);
    if (result != lSize) {
        fputs("Reading error", stderr);
        exit(3);
    }

    char *pBegin = buffer;
    char *pEnd = strchr(pBegin, '\r');//每行都以\r\n结尾
    pBegin = pBegin + 3;
    int lens = 0;
    while (pEnd) {
        char s[2010];
        lens = 0;
        while (pBegin != pEnd) {
            s[lens++] = *pBegin;
            pBegin++;
        }
        s[lens++] = '\0';
        CreateProc(s);
        pBegin = pEnd + 2;//每行都以\r\n结尾所以+2
        pEnd = strchr(pBegin, '\r');
    }
}
int main() {
    while (true) {
        printf("$min_shell>");
        char s[2010];
        gets_s(s);
        int len = strlen(s);
        int flag = check(s, len);//判断输入字符串为txt 还是exe
        if (flag == 1) {
            CreateProc(s);
        }
        else {
            ReadTxt(s);
        }
    }
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值