更多
当前位置: 首页 > 聚焦

焦点要闻:代码1

发布时间:2023-06-19 21:52:43 来源:哔哩哔哩

#include<stdio.h>

#include<stdlib.h>


(资料图片仅供参考)

#include<sys/wait.h>

#include<string.h>

#include<unistd.h>

#include<fcntl.h>7

#define MAX_ARGS 10

#define MAX_BUFFER 1024

int main()

{

char buffer[MAX_BUFFER];

char* args1[MAX_ARGS];

char* args2[MAX_ARGS];

char* args[MAX_ARGS];

int num_args2=0;

int num_args1=0;

int num_args=0;

pid_t pid2,pid1,pid;

int fd[2];

int background_flag=0;

while(1) {

printf("myshell>");

if(fgets(buffer,MAX_BUFFER,stdin)==NULL)

break;

buffer[strlen(buffer)-1]='\0';

char* token=strtok(buffer," ");

while(token!=NULL){

if(strcmp(token,">")==0){

char* file_name=strtok(NULL," ");

int fd = open(file_name,O_WRONLY | O_CREAT | O_TRUNC,

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

dup2(fd,STDOUT_FILENO);

close(fd);

break;

}

else if(strcmp(token,"<")==0){

char* file_name=strtok(NULL," ");

int fd=open(file_name,O_RDONLY);

dup2(fd,STDIN_FILENO);

close(fd);

break;

}

else if(strcmp(token,"|")==0){

num_args1=0;

token=strtok(NULL," ");

while(token!=NULL && strcmp(token,">")!=0 &&

strcmp(token,"<")!=0){

args1[num_args1]=token;49

num_args1++;

token = strtok(NULL," ");

}

args1[num_args1]=NULL;

if(token!=NULL){

if(strcmp(token,">")==0){

char* file_name=strtok(NULL," ");

int fd = open(file_name,O_WRONLY | O_CREAT | O_TRUNC,

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

dup2(fd,STDOUT_FILENO);

close(fd);

}

else if(strcmp(token,"<")==0){

char* file_name=strtok(NULL," ");

int fd=open(file_name,O_RDONLY);

dup2(fd,STDIN_FILENO);

close(fd);

}

}

pipe(fd);

pid1=fork();

if(pid1==0){

close(fd[0]);

dup2(fd[1],STDOUT_FILENO);

execvp(args1[0],args1);

exit(1);

}

else if(pid>0){

close(fd[1]);

num_args2=0;

token=strtok(NULL," ");

while(token!=NULL && strcmp(token,">")!=0 &&

strcmp(token,"<")!=0){

args2[num_args2]=token;

num_args2++;

token = strtok(NULL," ");

}

args2[num_args2]=NULL;

if(token!=NULL){

if(strcmp(token,">")==0){

char* file_name=strtok(NULL," ");90

int fd = open(file_name,O_WRONLY | O_CREAT | O_TRUNC,

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

dup2(fd,STDOUT_FILENO);

close(fd);

}

else if(strcmp(token,"<")==0){

char* file_name=strtok(NULL," ");

int fd=open(file_name,O_RDONLY);

dup2(fd,STDIN_FILENO);

close(fd);

}

}

pid2 =fork();

if(pid2==0){

dup2(fd[0],STDIN_FILENO);

execvp(args2[0],args2);

exit(1);

break;

}

else if(pid2>0){

waitpid(pid1,NULL,0);

waitpid(pid2,NULL,0);

}

else{

perror("fork");

exit(1);

break;

}

}

else {

perror("fork");

exit(1);break;

}

}

else if(strcmp(token,"&")==0){

background_flag =1;

break;

}

else{

args[num_args]=token;

num_args++;

}

token=strtok(NULL," ");

}

args[num_args]=NULL;

pid =fork();

if(pid==0){

execvp(args[0],args);

exit(1);

}

else if(pid>0){

if(background_flag){

background_flag=0;

}

else{  

waitpid(pid,NULL,0);

}

}

else{

perror("fork");

exit(1);

}

num_args1=0;

num_args2 =0;

memset(args1,0,sizeof(args1));

memset(args2,0,sizeof(args2));

num_args=0;

memset(args,0,sizeof(args));

}

return 0;

}

上一篇:腾讯杀入大模型赛道!提供50多个解决方案 要跟百度、阿里一决高下 每日观点

下一篇:最后一页