Browse Source

init

master
陈越 3 years ago
parent
commit
a11bf5d643
4 changed files with 181 additions and 0 deletions
  1. +12
    -0
      fun.h
  2. +129
    -0
      fun_1.c
  3. +23
    -0
      fun_2.c
  4. +17
    -0
      main.c

+ 12
- 0
fun.h View File

@ -0,0 +1,12 @@
#ifndef FUN_H_INCLUDED
#define FUN_H_INCLUDED
#define MAXLINE (80)
void history(char *cmdline);
void print_his(char *argv);
void eval(char *cmdline);
int parseline(const char *cmdline, char **argv);
#endif // FUN_H_INCLUDED

+ 129
- 0
fun_1.c View File

@ -0,0 +1,129 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fun.h"
void eval(char *cmdline)
{
char *argv[MAXLINE];
char buf[MAXLINE];
int isBg;
int pid;
strcpy(buf,cmdline);
isBg = parseline(cmdline,argv);
if(argv[0] == NULL)
{
return;
}
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);
}
else
{
printf("[%d] (%d) %s",pid2jid(pid),pid,cmdline);
}
*/
}
return;
}
int parseline(const char *cmdline, char **argv)
{
static char array[MAXLINE];
char *buf = array;
char *delim;
int argc;
int bg;
strcpy(buf, cmdline);
buf[strlen(buf)-1] = ' ';
while (*buf && (*buf == ' '))
buf++;
argc = 0;
if (*buf == '\'')
{
buf++;
delim = strchr(buf, '\'');
}
else
{
delim = strchr(buf, ' ');
}
while (delim)
{
argv[argc++] = buf;
*delim = '\0';
buf = delim + 1;
while (*buf && (*buf == ' '))
{
buf++;
}
if (*buf == '\'')
{
buf++;
delim = strchr(buf, '\'');
}
else
{
delim = strchr(buf, ' ');
}
}
argv[argc] = NULL;
if (argc == 0)
return 1;
if ((bg = (*argv[argc-1] == '&')) != 0)
{
argv[--argc] = NULL;
}
return bg;
}
int builtin_cmd(char **argv)
{
if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit"))
{
exit(0);
}
if(!strcmp(argv[0],"history"))
{
print_his(argv[1]);
return 1;
}
if(!strcmp(argv[0],"jobs"))
{
return 1;
}
if(!strcmp(argv[0],"bg")||!strcmp(argv[0],"fg"))
{
return 1;
}
if(!strcmp(argv[0], "&"))
{
return 1;
}
return 0;
}

+ 23
- 0
fun_2.c View File

@ -0,0 +1,23 @@
#include <string.h>
#include "fun.h"
static int num = 0;
static char storage[MAXLINE][MAXLINE];
void history(char *cmdline)
{
strcpy(storage[num], cmdline);
num++;
}
void print_his(char *argv)
{
int i = num - atoi(argv);
if(i < 0)
{
i = 0;
}
for(; i<num; i++)
puts(storage[i]);
}

+ 17
- 0
main.c View File

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

Loading…
Cancel
Save