瀏覽代碼

3.12 commit

master
陈越 3 年之前
父節點
當前提交
99664f595d
共有 3 個檔案被更改,包括 75 行新增3 行删除
  1. +14
    -2
      fun_1.c
  2. +61
    -1
      fun_2.c
  3. 二進制
      main

+ 14
- 2
fun_1.c 查看文件

@ -2,6 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <signal.h>
//#include "fun.h"
@ -22,6 +24,10 @@ void eval(char *cmdline)
{
if((pid=fork())==0)
{
if(isBg)
{
signal(SIGCHLD, SIG_IGN);
}
if(execvp(argv[0],argv)<0)
{
printf("%s:Command not found.\n",argv[0]);
@ -30,10 +36,12 @@ void eval(char *cmdline)
}
if(!isBg)
{
wait(NULL);
wait(0);
return;
}
else
{
signal(SIGCHLD, SIG_IGN);
return;
}
}
@ -86,7 +94,7 @@ int parseline(const char *cmdline, char **argv)
argv[argc] = NULL;
if (argc == 0)
return 1;
return 1;
if ((bg = (*argv[argc-1] == '&')) != 0)
{
@ -101,6 +109,10 @@ int builtin_cmd(char **argv)
{
return 1;
}
if(Is_open(argv)==1)
{
return 1;
}
if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit"))
{
exit(0);

+ 61
- 1
fun_2.c 查看文件

@ -1,4 +1,5 @@
#include <string.h>
#include<fcntl.h>
//#include "fun.h"
static int num = 0;
@ -93,4 +94,63 @@ void shell_pip(char **prog1, char **prog2)
wait(0);
wait(0);
return 0;
}
}
int Is_open(char **argv)
{
int i = 0;
int Isopen = 0;
char *cmd_0[MAXLINE];
char *cmd_1[MAXLINE];
while (argv[i]!=NULL)
{
if (argv[i][0] == '>')
{
Isopen = 1;
}
i++;
}
if(Isopen == 0)
{
return 0;
}
i = 0;
while (argv[i][0] != '>')
{
cmd_0[i] = argv[i];
i++;
}
cmd_0[i] = NULL;
i++;
int j = 0;
while (argv[i]!=NULL)
{
cmd_1[j] = argv[i];
i++;
j++;
}
cmd_1[j] = NULL;
shell_open(cmd_0, cmd_1);
return 1;
}
void shell_open(char **prog1, char **prog2)
{
///
int fd;
if(fork() == 0)
{
if( (fd=open(prog2[0],O_RDWR | O_CREAT | O_NOCTTY | O_NDELAY))<0 )
{
perror("open");
exit(0);
}
close(1);
dup(fd);
close(fd);
execvp(prog1[0], prog1);
exit(1);
}
wait(0);
}

二進制
main 查看文件


Loading…
取消
儲存