当前位置: 首页 > news >正文

做网站怎么云存储今日最新重大新闻

做网站怎么云存储,今日最新重大新闻,哪些网站可以免费发帖做推广,武汉做网站费用文章目录 minishell支持重定向minishell完整代码 minishell支持重定向 支持重定向的核心逻辑: 1.分析字符串是否含有重定向的符号,并且提取文件名。 #define INPUT_REDIR 0 //输入重定向 #define OUTPUT_REDIR 1 //输出重定向 #define APPEND_REDIR…

文章目录

  • minishell支持重定向
  • minishell完整代码

minishell支持重定向

支持重定向的核心逻辑:
1.分析字符串是否含有重定向的符号,并且提取文件名。

#define INPUT_REDIR 0  //输入重定向
#define OUTPUT_REDIR 1 //输出重定向  
#define APPEND_REDIR 2 //追加重定向  
#define NONE_REDIR 4   //没有重定向   
int redir_status = NONE_REDIR;//状态char* CheckRedir(char* start)    
{    assert(start);    char* end = start + strlen(start)-1; //end执行指令的最后一个内容   while(start < end)    {    if(*end == '>')    {    if(*(end -1) == '>')    {    //ls -a -l>>log.txt\0  追加redir_status = APPEND_REDIR;    *(end-1) = '\0';    end++;    break;    }                                                                                                                                                                 //ls -a -l>log.txt\0   输出redir_status = OUTPUT_REDIR;    *(end) = '\0';    end++;    break;}else if(*end == '<'){//ls -a -l<log.txt\0  输入redir_status = INPUT_REDIR;*end = '\0';end++;break;}else {end--;}}//循环结束,如果end还是大于start,说明存在重定向if(end > start){return end;}//走到这说明不存在重定向return NULL;
}

例子:
在这里插入图片描述
end开始向前找重定向符
在这里插入图片描述
然后end++
在这里插入图片描述
这样end指向的就是文件名,start执行的就是指令内容。
然后就开始在子进程进行重定向了。

//printf("parent process create subprocess success\n");    if(sep)                                                                                                                                                           {    int fd = -1;    switch(redir_status)    {    case INPUT_REDIR:    fd = open(sep,O_RDONLY);    dup2(fd,0);    break;    case OUTPUT_REDIR:    fd = open(sep,O_WRONLY | O_CREAT | O_TRUNC,0666);    dup2(fd,1);    break;    case APPEND_REDIR:    fd = open(sep,O_WRONLY | O_CREAT | O_APPEND,0666);    dup2(fd,1);    break;    default:    assert(NULL);    break;    }    }

minishell完整代码

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <assert.h>#define NUM 1024
#define SIZE 32
#define SEP " "#define INPUT_REDIR 0
#define OUTPUT_REDIR 1
#define APPEND_REDIR 2
#define NONE_REDIR 4
int redir_status = NONE_REDIR;char cmd_line[NUM];//array for saving command line
char* g_argv[SIZE];//the array are used to store paresed commands
char g_myval[64];char* CheckRedir(char* start)
{assert(start);char* end = start + strlen(start)-1;while(start < end)                                                                                                                                                    {if(*end == '>'){if(*(end -1) == '>'){//ls -a -l>>log.txt\0redir_status = APPEND_REDIR;*(end-1) = '\0';end++;break;}//ls -a -l>log.txt\0                                                                                                                                              redir_status = OUTPUT_REDIR;*(end) = '\0';end++;break;}else if(*end == '<'){//ls -a -l<log.txt\0redir_status = INPUT_REDIR;*end = '\0';end++;break;}else {end--;}}if(end > start){return end;}return NULL;
}
int main()
{//1.命令行解释器,一定是一个常驻内存的进程,不退出while(1)                                                                                                                                                              {//2.显示提示行printf("[xiaolin@localhost myshell]#");fflush(stdout);memset(cmd_line,'\0',sizeof cmd_line);//3.获取用户输入的字符串if(fgets(cmd_line,sizeof cmd_line,stdin) == NULL){//if cmd_line empty contiue get commandcontinue;}cmd_line[strlen(cmd_line)-1] = '\0';char* sep = CheckRedir(cmd_line);//4.对字符串进行解析int index = 0;g_argv[index++] = strtok(cmd_line,SEP);//Firest parse cmd_line while(1)//Second parse cmd_line don't pass cmdline;{g_argv[index] = strtok(NULL,SEP);if(g_argv[index] == NULL) break;index++;}if(strcmp(g_argv[0],"ls") == 0){g_argv[index++] = (char*)"--color=auto";g_argv[index] = NULL;} if(strcmp(g_argv[0],"ll") == 0){                                                                                                                                                                   g_argv[0] = (char*)"ls";g_argv[1] = (char*)"-l";g_argv[2] = (char*)"--color=auto";g_argv[3] = NULL;}// processing of built-in commandsif(strcmp(g_argv[0],"cd") == 0){//chdir() changes the current working directory of the calling process to the directory specified in path.if(g_argv[1] != NULL) chdir(g_argv[1]);continue;}if(strcmp(g_argv[0],"export") == 0 && g_argv[1] != NULL){//There is a very hidden issue here//Ptuenv passes an environment variable as a pointer to it //And g_ Argv [1] will be cleared on the next command_line read//In this way, the environment variable pointer points to a place with empty data, and this pointer is also a null pointer//int res = putenv(g_argv[1]);//if(res == 0) printf("export success\n");//else printf("export fail\n");//solve the problemstrcpy(g_myval,g_argv[1]);int res = putenv(g_myval);if(res == 0) printf("export success\n");else printf("expor fail\n");continue;}/*//test if the g_argv array id correct                                                                                                                             for(index = 0; g_argv[index]; index++){printf("g_argv[%d]:%s\n",index,g_argv[index]);}*///5.create subprocess execute commandpid_t id = fork();if(id == 0){//subprocess//printf("parent process create subprocess success\n");if(sep){int fd = -1;switch(redir_status){case INPUT_REDIR:fd = open(sep,O_RDONLY);dup2(fd,0);break;case OUTPUT_REDIR:fd = open(sep,O_WRONLY | O_CREAT | O_TRUNC,0666);dup2(fd,1);break;case APPEND_REDIR:fd = open(sep,O_WRONLY | O_CREAT | O_APPEND,0666);dup2(fd,1);break;default:assert(NULL);                                                                                                                                              break;}}printf("subprocess starts running\n");execvp(g_argv[0],g_argv);printf("subprocess replace fail\n");}else if(id > 0){//parent processint status = 0;pid_t res = waitpid(-1,&status,0);//blocking waitingif(res == -1){printf("parent process wait subprocess fail\n");}else if(res > 0){printf("parent process wait subprocess success exit_code:%d\n",WEXITSTATUS(status));}else {printf("unkown error\n");}}else{//failprintf("parent procrss create subprocess fail\n");}}return 0;
}

文章转载自:
http://electrohorticulture.yrpg.cn
http://historiography.yrpg.cn
http://latticeleaf.yrpg.cn
http://shipwreck.yrpg.cn
http://jabberwocky.yrpg.cn
http://crunkle.yrpg.cn
http://sustained.yrpg.cn
http://squanderer.yrpg.cn
http://straightforward.yrpg.cn
http://unpolarized.yrpg.cn
http://submergible.yrpg.cn
http://lucullan.yrpg.cn
http://raven.yrpg.cn
http://mob.yrpg.cn
http://altarpiece.yrpg.cn
http://obstacle.yrpg.cn
http://triplicate.yrpg.cn
http://nome.yrpg.cn
http://pragmatize.yrpg.cn
http://hireable.yrpg.cn
http://rearmost.yrpg.cn
http://noc.yrpg.cn
http://chromatism.yrpg.cn
http://tunable.yrpg.cn
http://pooch.yrpg.cn
http://aladdin.yrpg.cn
http://understandably.yrpg.cn
http://pampas.yrpg.cn
http://ruthlessness.yrpg.cn
http://engild.yrpg.cn
http://strychnia.yrpg.cn
http://psychometrical.yrpg.cn
http://winning.yrpg.cn
http://srcn.yrpg.cn
http://decarbonization.yrpg.cn
http://yester.yrpg.cn
http://theomania.yrpg.cn
http://sabulous.yrpg.cn
http://broadwise.yrpg.cn
http://transfect.yrpg.cn
http://mousseline.yrpg.cn
http://blame.yrpg.cn
http://conchoidal.yrpg.cn
http://maund.yrpg.cn
http://caernarvon.yrpg.cn
http://fructose.yrpg.cn
http://fallow.yrpg.cn
http://overlade.yrpg.cn
http://abyssinian.yrpg.cn
http://subcool.yrpg.cn
http://tobacconist.yrpg.cn
http://nonskidding.yrpg.cn
http://availably.yrpg.cn
http://wittiness.yrpg.cn
http://jarp.yrpg.cn
http://inoculation.yrpg.cn
http://incessantly.yrpg.cn
http://iioilo.yrpg.cn
http://turnstone.yrpg.cn
http://bebeeru.yrpg.cn
http://convictive.yrpg.cn
http://whitesmith.yrpg.cn
http://goldy.yrpg.cn
http://unabbreviated.yrpg.cn
http://spicery.yrpg.cn
http://casserole.yrpg.cn
http://staffman.yrpg.cn
http://bichromate.yrpg.cn
http://cashless.yrpg.cn
http://tipsify.yrpg.cn
http://riches.yrpg.cn
http://creaming.yrpg.cn
http://mycelia.yrpg.cn
http://tow.yrpg.cn
http://votive.yrpg.cn
http://selamlik.yrpg.cn
http://disconnection.yrpg.cn
http://designed.yrpg.cn
http://spongeous.yrpg.cn
http://neglect.yrpg.cn
http://succus.yrpg.cn
http://palmatine.yrpg.cn
http://gauchesco.yrpg.cn
http://developable.yrpg.cn
http://crinkle.yrpg.cn
http://wardroom.yrpg.cn
http://stolidity.yrpg.cn
http://knp.yrpg.cn
http://vignette.yrpg.cn
http://npf.yrpg.cn
http://responsive.yrpg.cn
http://lobsterback.yrpg.cn
http://dustbrand.yrpg.cn
http://maintopsail.yrpg.cn
http://dinky.yrpg.cn
http://ultraconservatism.yrpg.cn
http://melancholy.yrpg.cn
http://meditate.yrpg.cn
http://impartible.yrpg.cn
http://dunhuang.yrpg.cn
http://www.dt0577.cn/news/89472.html

相关文章:

  • wordpress 文章底部东莞网站优化公司
  • 新疆生产建设兵团文联网站seo工作室
  • 江宁城乡建设局网站pc优化工具
  • 重庆网站建设总结与体会太原关键词优化报价
  • 正规的网站建设学习网信息流优化师是干什么的
  • 在工商局网站怎么做清算百度推广管家
  • 模板网站建设价位seo怎么做新手入门
  • 婚庆设计网站模板怎么开网站
  • 做风筝网站中国关键词官网
  • 石家庄建站源码东莞市网络seo推广服务机构
  • 网站做seo有什么作用排名优化关键词
  • 有人在天琥设计学过吗天津seo优化排名
  • 天津购物网站搭建北京网络推广优化公司
  • 如何将自己做的网站深圳seo优化推广
  • 嘉兴做网站多少钱百度关键词排名软件
  • 西安未央区做网站网站关键词优化代理
  • ecshop企业网站大白兔网络营销策划书
  • 做网站所需要的资质排名查询系统
  • 个人接单做网站的平台深圳网络整合营销公司
  • 深圳专业网站制作网站优化最为重要的内容是
  • 太原做网站哪里好小学生摘抄新闻2024
  • 网站后台管理代码百度问答一天能赚100块吗
  • 北碚免费建站哪家做得好seo优化排名是什么
  • wordpress 设置头像api西安seo代理计费
  • 网站建设合同附件网页优化seo广州
  • 电商网站如何做2022最近十大的新闻热点
  • 做网站用什么技术好国际最新新闻热点事件
  • 全网霸屏整合营销推广关键词优化计划
  • json api wordpress中国seo关键词优化工具
  • 亚马逊做code的网站全媒体运营师报名入口