编写Linux的shell [英] writing linux shell

查看:183
本文介绍了编写Linux的shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要学习Unix C和做一些练习练习。我目前正在写我自己的外壳,工作原理类似于Linux的bash shell的。

I am trying to learn Unix C and doing some exercises for practice. I am currently working on writing my own shell that works similar to the linux bash shell.

在code我有以下提供一个相当基本的外壳。现在提供I / O重定向。
我试图增加对管道的支持。最初,我只想补充了单管的支持。

The code I have below provides for a fairly basic shell. It now provides I/O redirection. I am trying to add support for piping. Initially, I just want to add support for a single pipe.

我曾试图去通过一些在线教程,但不能完全弄清楚哪里开始。

I have tried to go through some tutorials online but can't quite figure out where to start.

目前,下面的外壳可以处理命令,命令,如以下。
LS> ABC,猫<文件1>文件2,等等。

Currently, the shell below can handle commands commands such as the following. ls > abc, cat< file1 > file2, etc.

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>

#define TRUE 1

int main(void)
{
   char *arg_list[10];

   int status;

   int counter = 0;

   int counter2 = 0;

   pid_t pid;
   char buf[100];

   char inFile[10];
   char outFile[10];

   int fdIn, fdOut;
   while(TRUE)
   {
      printf("> ");
      if (!fgets(buf, 100, stdin))
      return 0;

      pid = fork();
      switch(pid)
      {
      case -1:
      return 1;

      case 0:
      {
      arg_list[counter] = strtok(buf, " \n");

      counter = 0;

      while(arg_list[counter] != NULL)
      {
       counter++;
       arg_list[counter] = strtok(NULL, " \n");
      }

      counter2 = 0;

      while(arg_list[counter2] != NULL)
     {
       if(!strcmp(arg_list[counter2], "<"))
       {
         if(arg_list[counter2+1] != NULL)
         {
           fdIn = open(arg_list[counter2+1], O_RDONLY);
           dup2(fdIn, STDIN_FILENO);
         }
         else
         {
           printf("No input file specified");
         }
         arg_list[counter2] = 0;
       }
       else if(!strcmp(arg_list[counter2], ">"))
       {
         if(arg_list[counter2+1] != NULL)
         {
         fdOut = open(arg_list[counter2+1], O_CREAT | O_WRONLY | O_TRUNC, 0666);
           dup2(fdOut, STDOUT_FILENO);
         }
         else
         {
           printf("No output file specified");
         }
         arg_list[counter2] = 0;
       }
       counter2++;
     }

    execvp(arg_list[0], arg_list);
    break;
  }

  default:
  waitpid(-1, &status, 0);
  break;
  }
}

  return 0;
}

如果有人能在正确的方向指向我,这将是AP preciated。

If somebody can point me in the right direction, that would be appreciated.

推荐答案

在使用后 dup2()(成功),你有两个文件描述符打开一个文件。你需要关闭原来的文件描述符;在执行过程中不应该有额外的文件描述符打开。

After you use dup2() (successfully), you have two file descriptors open for the one file. You need to close the original file descriptor; the executed process should not have the extra file descriptors open.

您也将需要使用前打开在适当的方式将文件<​​code> dup2()。除其他事项外,这意味着的strtok()是不是一个很好的选择,因为它blats出来的分隔符,但你需要知道打开读取和写入的文件)。

You are also going to need to open the files in the appropriate way before using dup2(). Amongst other things, that means strtok() is not a good choice because it blats out the delimiter but you need to know which file to open for reading and which for writing).

您也将需要临界状态值的参数列表;它应该只包含命令名称和空指针,而不是两个文件名。

You are also going to need to fettle the argument list; it should contain just the command name and a null pointer, not the two file names.

这篇关于编写Linux的shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆