⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myshell.c

📁 这是个在Linux系统下用C编写的一个shell程序
💻 C
📖 第 1 页 / 共 2 页
字号:
        }    }}/* * open vi editor * * vi & */void my_vi(options)    char **options;{    pid_t pid;    char *argv[11] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};    int arg_index = 0;    struct sigaction sa;    if ((pid = fork()) < 0)    {        my_err_handler("fork error");        return;    }    else if (pid == 0)    {        setpgid(pid, pid);        argv[arg_index ++] = vi_path;        if (*(options + 1) == NULL)        {            my_err_handler(unsupport_cmd);            exit(0);         }        else if (*(options + 1)[0] == '&')        {            if (execv(argv[0], argv) < 0)            {                my_err_handler("execv error");                exit(0);             }        }        else        {            my_err_handler(unsupport_cmd);            exit(0);        }    }    else    {        setpgid(pid, pid);        int tty = open("/dev/tty", O_RDWR);        if (tty == -1)        {            my_err_handler("open error");            return;        }        pid_t fg = getpgrp();        if (tcsetpgrp(tty, fg) == -1)        {            my_err_handler("tcsetpgrp error");            return;        }    }}/* * add serveral numbers read from stdin, no matter it is decimal or hexadecimal */void my_add(numbers)    char **numbers;{    long sum = 0;    long value = 0;    int i;    for (i = 1; *(numbers + i) != NULL; i ++)    {        fprintf(stdout, "%s ", *(numbers + i));        if (*(numbers + i + 1) != NULL)            fprintf(stdout, "+ ");        else            fprintf(stdout, "= ");        value =  get_number(*(numbers + i));        if (value == -1)        {            sum = -1;            return;        }        else            sum += value;    }    fprintf(stdout, " %d\n", sum);}/* * translate string value to decimal */long get_number(str_value)    char *str_value;{    long value = 0;    int length = strlen(str_value);    if (str_value[0] == '0' && (str_value[1] == 'x' || str_value[1] == 'X'))    {       int i;       for (i = 2; i < length; i ++)       {           if (isxdigit(str_value[i]))           {               switch(str_value[i])               {                   case '0':                       value += 0 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '1':                       value += 1 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '2':                       value += 2 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '3':                       value += 3 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '4':                       value += 4 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '5':                       value += 5 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '6':                       value += 6 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '7':                       value += 7 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '8':                       value += 8 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case '9':                       value += 9 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case 'a':                   case 'A':                       value += 10 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case 'b':                   case 'B':                       value += 11 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case 'c':                   case 'C':                       value += 12 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case 'd':                   case 'D':                       value += 13 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case 'e':                   case 'E':                       value += 14 * (long)pow((double)16, (double)length - 1 - i);                       break;                   case 'f':                   case 'F':                       value += 15 * (long)pow((double)16, (double)length - 1 - i);                       break;                   default:                       break;               }               return value;           }           else           {               fprintf(stdout, "%s\t", str_value[i]);               my_err_handler("Wrong format of hexadecimal number!");               return -1;           }       }   }   else   {       int i;       for (i = 0; i < length; i ++)       {           if (isdigit(str_value[i]))           {               switch(str_value[i])               {                   case '0':                       value += 0 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '1':                       value += 1 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '2':                       value += 2 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '3':                       value += 3 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '4':                       value += 4 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '5':                       value += 5 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '6':                       value += 6 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '7':                       value += 7 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '8':                       value += 8 * (long)pow((double)10, (double)length - 1 - i);                       break;                   case '9':                       value += 9 * (long)pow((double)10, (double)length - 1 - i);                       break;                   default:                       break;               }               return value;           }           else           {               //fprintf(stdout, "%s\t", str_value[i]);               my_err_handler("Wrong format of decimal number!");               return -1;           }       }   }}/* * count arguments and list them */void my_args(args)    char **args;{    int argc = 0;    int i = 0;       fprintf(stdout, "args = ");    if (args[1] != NULL)    {        fprintf(stdout, "%s", args[1]);        i ++;    }    for (; *(args + i + 1) != NULL; i ++)        fprintf(stdout, ", %s", args[i + 1]);    argc = i;    fprintf(stdout, "\targc = %d\n", argc);}/* * display history commands */void my_history(options)    char **options;{    if (*(options + 1) != NULL)    {        my_err_handler(unsupport_cmd);        return;    }    int index = history_cmd_index;    int i, j;    if (strlen(history_cmds[index][0]) == 0)    {        int temp = index;        while (strlen(history_cmds[temp][0]) == 0)            temp  = ++temp % 10;        for (i = 0; temp != index; temp = ++temp % 10, i ++)        {            fprintf(stdout, "%d\t", i);            for (j = 0; strlen(history_cmds[temp][j]) != 0; j ++)                fprintf(stdout, "%s ", history_cmds[temp][j]);            fprintf(stdout, "\n");        }    }    else    {        for (i = 0; i < 10; i ++)        {            fprintf(stdout, "%d\t", i);            for (j = 0; strlen(history_cmds[index][j]) != 0; j ++)                fprintf(stdout, "%s ", history_cmds[index][j]);            fprintf(stdout, "\n");            index = ++ index % 10;        }    }}/* * execute one history method use "! cmd_no" */void my_ex_hcmd(number)    char **number;{    if ((number + 1) == NULL)    {        my_err_handler("Please enter a history commond number!");        return;    }    int index = (int)get_number(*(number + 1));    if (index > 9)    {        my_err_handler("Wrong history commond number!");        return;    }    int j;    fprintf(stdout, "%d\t", index);    for (j = 0; strlen(history_cmds[index][j]) != 0; j ++)        fprintf(stdout, "%s ", history_cmds[index][j]);    fprintf(stdout, "\n");}/* * add current cmd to history array */void add_history_cmd(cmd)    char **cmd;{    int i;    for (i = 0; *(cmd + i) != NULL; i ++)    {        memcpy(history_cmds[history_cmd_index][i], cmd[i], 10);    }    history_cmd_index = ++ history_cmd_index % 10;}/* * Error handler, display the error information to user. */void my_err_handler(err_str)    const char *err_str;{    fprintf(stderr, "%s\n", err_str);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -