瀏覽代碼

3.10 commit

master
陈越 3 年之前
父節點
當前提交
52c3b99c49
共有 6 個檔案被更改,包括 85 行新增14 行删除
  1. +4
    -1
      fun.h
  2. +25
    -11
      fun_1.c
  3. +52
    -1
      fun_2.c
  4. 二進制
      hello
  5. 二進制
      main
  6. +4
    -1
      main.c

+ 4
- 1
fun.h 查看文件

@ -1,8 +1,11 @@
#ifndef FUN_H_INCLUDED
#define FUN_H_INCLUDED
#define MAXLINE (80)
#include "fun_1.c"
#include "fun_2.c"
void history(char *cmdline);
void print_his(char *argv);

+ 25
- 11
fun_1.c 查看文件

@ -1,7 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fun.h"
#include <wait.h>
//#include "fun.h"
void eval(char *cmdline)
@ -10,10 +11,8 @@ void eval(char *cmdline)
char buf[MAXLINE];
int isBg;
int pid;
strcpy(buf,cmdline);
isBg = parseline(cmdline,argv);
if(argv[0] == NULL)
{
return;
@ -21,28 +20,23 @@ void eval(char *cmdline)
if(!builtin_cmd(argv))
{
/*
if((pid=fork())==0)
{
setpgid(0,0);
if(execvp(argv[0],argv)<0)
{
printf("%s:Command not found.\n",argv[0]);
exit(0);
}
}
if(!isBg)
{
waitfg(pid);
wait(NULL);
}
else
{
printf("[%d] (%d) %s",pid2jid(pid),pid,cmdline);
return;
}
*/
}
return;
}
int parseline(const char *cmdline, char **argv)
@ -103,6 +97,10 @@ int parseline(const char *cmdline, char **argv)
int builtin_cmd(char **argv)
{
if(Is_pipe(argv)==1)
{
return 1;
}
if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit"))
{
exit(0);
@ -112,7 +110,23 @@ int builtin_cmd(char **argv)
print_his(argv[1]);
return 1;
}
if(!strcmp(argv[0],"jobs"))
if(!strcmp(argv[0],"cd"))
{
if (argv[1] == NULL)
{
return 1;
}
else
{
if (chdir(argv[1]) != 0)
{
perror("Error: ");
}
return 1;
}
}
if(!strcmp(argv[0],"mytop"))
{
return 1;
}

+ 52
- 1
fun_2.c 查看文件

@ -1,5 +1,5 @@
#include <string.h>
#include "fun.h"
//#include "fun.h"
static int num = 0;
static char storage[MAXLINE][MAXLINE];
@ -20,4 +20,55 @@ void print_his(char *argv)
}
for(; i<num; i++)
puts(storage[i]);
}
int Is_pipe(char **argv)
{
int i = 0;
char *cmd_0[MAXLINE];
char *cmd_1[MAXLINE];
while (argv[i]!=NULL)
{
cmd_0[i] = argv[i];
if(!strcmp(argv[i],"|"))
{
cmd_0[i] = NULL;
i++;
while (argv[i]!=NULL)
{
cmd_1[i] = argv[i];
i++;
}
cmd_1[i] = NULL;
shell_pip(cmd_0, cmd_1);
return 1;
}
i++;
}
return 0;
}
void shell_pip(char **cmd_0, char **cmd_1)
{/*
int pid;
int fd[2];
pipe(fd);
pid = fork();
if (pid == 0)
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
// mysys_simple(&Cmd[1]);
execvp(cmd_1[0],cmd_1);
exit(0);
}
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
// mysys_simple(&Cmd[0]);
execvp(cmd_0[0],cmd_0);
*/
}

二進制
hello 查看文件


二進制
main 查看文件


+ 4
- 1
main.c 查看文件

@ -2,16 +2,19 @@
#include <stdlib.h>
#include "fun.h"
int main()
{
char cmdline[MAXLINE];
while(1)
{
int bufsize = MAXLINE;
printf("COMMAND->");
fgets(cmdline, MAXLINE, stdin);
fgets(cmdline, MAXLINE, stdin);
eval(cmdline);
history(cmdline);
}
return 0;
}

Loading…
取消
儲存