Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624064
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: LINUX

2009-08-06 14:04:09

//history_init.c

/*

*这是我webshell工程的一小部分,“历史命令的初始化”,上下键显示历史命令。

*我用的是双向环形链表。

*/

#include "main.h"

TYPE *history_init(TYPE *tail)
{
    FILE *fp;
    int i = 0;
    TYPE *buffer = NULL;
    char temp_his_cmd[CMD_SIZE] = {""};

    if((fp = fopen("/home/skyily/temp/shellprograme/history.bak", "a+")) == NULL)
    {
        printf("cant open file!\n");
        return 0;
    }

    rewind(fp);
    
    for(i = 0; i < HIS_CMD_NUM ; i++)
    {
        buffer = (TYPE *)malloc(sizeof(TYPE));

    //    fread(buffer, sizeof(TYPE), 1, fp);

        if(fgets(temp_his_cmd, CMD_SIZE, fp) != NULL)
        {
            strcpy(buffer->his_cmd, temp_his_cmd);
            memset(temp_his_cmd, 0, CMD_SIZE);
        }
        else
        {
        //    buffer->his_cmd = NULL;
        }
       
        if(head == NULL)
        {
            tail = head = buffer;
            head->next_f = NULL;
            tail->next_b = NULL;        //

        }
        else
        {
            tail->next_b = buffer;
            buffer->next_f = tail;
            tail = buffer;
            tail->next_b = NULL;        //

        }
        buffer = NULL;
    }
    tail->next_b = head;     //
    head->next_f = tail;     //这里头尾相接

    fclose(fp);
    return head;
}

#ifndef __main_h__
#define __main_h__

#define _GNU_SOURCE            //use get_current_dir_name()

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
//============================================================
//    文件名:    main.h
//    维护记录:    2009-3-10 v1.0        by skyily
//============================================================

#define CMD_BUNCH_SIZE 50    //cmd bunch size
#define DIR_CU_SIZE 100    //dir current name size
#define PATH_BUNCH_SIZE 50    //path bunch size
#define CMD_SIZE 200        //cmd size
#define PATH_SIZE 200        //path size
#define DIR_BAK_SIZE 500    //cd -
#define TEXT_SIZE 200        //<<
#define HIS_CMD_NUM 20       //saved number of history cmd

typedef struct my_history    //history struct
{
    char his_cmd[CMD_SIZE];
    struct my_history *next_f;
    struct my_history *next_b;
}TYPE;
extern TYPE *head;            //head pointer of history chain

extern char *argv[CMD_BUNCH_SIZE];            //
extern char my_dir_bak[DIR_BAK_SIZE];        //
extern char my_dir_cu[DIR_CU_SIZE];            //
extern char *path_argv[PATH_BUNCH_SIZE];    //
extern int flag_cmd;                //variable flag of the whole represent different cmd
extern char temp_cmd[CMD_SIZE];        //veriable of the whole programe in job_fg()


extern void get_cmd(int connfd, char *buf, char *rebuf);            //
extern void my_cmd(int connfd, int p_size, int c_size);    //execut within amd without cmd
extern int cut_cmd(char *buf);                //cut cmd
extern char *cut_dir(char *buf);            //cut _current_dir
extern int environment_init(char *path_buf);//environment init
extern int cut_path(char *buf);                //cut path
extern int cmd_found(char *buf, int p_size);//search cmd
extern int cmd_differ(int c_size);            //differentiate cmd (redriect, grep,in, out)
extern void my_redirect(int temp_size, int p_size, int c_size);    //redriect
extern void my_pipe(int connfd, int temp_size, int c_size, int p_size);        //grep cmd
extern char *format_cmd(char *buf);            //format cmd
extern char *my_replace(char *str_parent, char *str_old, char *str_new);//replace str
extern TYPE *history_init(TYPE *tail);        //history init
extern TYPE *add_history(char *buf);        //add history cmd
extern TYPE *print_history();                //cmd history
extern TYPE *history_save();                //save

#endif
//全贴出来了,哈哈,

阅读(2533) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~