废话不说,直接上代码
shell.h
/********************************************************
* @author Airead Fan <[email protected]> *
* @date 2011 9月 08 12:44:18 CST *
********************************************************
* after studying C 52 days *
* after studying APUE 17 days *
********************************************************/
#ifndef SHELL_H
#define SHELL_H
#define SHELL_CD 1
#define SHELL_FORK 2
#define SHELL_EMPTY 3
int read_char(char *str);
int parse_args(char *args[], char *arg);
int isquit(char *args);
char *shell_prompt(char *promptbuf);
int dealarg(char *arg);
int shell_cd(char *args[]);
int shell_fork(char *args[]);
int get_cmdnum(char *cmd);
int mutifork(char *args[]);
#endif
shell.c
/********************************************************
* @author Airead Fan <[email protected]> *
* @date 2011 9月 09 20:00:47 CST *
********************************************************
* after studying C 53 days *
* after studying APUE 18 days *
********************************************************/
/*
* This program implement shell
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "shell.h"
#define DEBUG 1
#define BUFSIZE 1024
#define CMDNUMBER 100
int main(int argc, char *argv[])
{
char arg[BUFSIZE];
ssize_t readbytes;
char str_prompt[BUFSIZE + 1];
while(1){
fprintf(stdout, "%s", shell_prompt(str_prompt));
fflush(stdout);
//void *memset(void *s, int c, size_t n);
memset(arg, 0, sizeof(arg));
//ssize_t read(int fd, void *buf, size_