编写我自己的shell,未声明的标识符"输出"

时间:2018-06-03 00:40:17

标签: c linux shell unix

这是我的代码,对于我自己在C中的shell。编译时出现错误:使用未声明的标识符'输出'。以下是编译时的一些错误示例:

  

错误:使用未声明的标识符'输出'烧焦   输入[100];输出[100];

     

test3.c:53:15:错误:使用未声明的标识符'输出'          的strcpy(输出,ARGS [I + 1]);                 ^

     

test3.c:53:15:错误:使用未声明的标识符'输出'

     

test3.c:60:8:警告:隐含的功能声明'打开'是   在C99中无效         [-Wimplicit-function-declaration] j = open(输入,O_RDONLY,0);          ^

     

test3.c:60:20:错误:使用未声明的标识符' O_RDONLY' j =   打开(输入,O_RDONLY,0);                      ^

     

test3.c:61:29:错误:使用未声明的标识符' O_RDONLY'          if((j = open(输入,O_RDONLY,0))< 0){

     

test3.c:70:12:警告:隐含的功能声明' creat'是   无效的         C99 [-Wimplicit-function-declaration] if((i = creat(output,0644))< 0){

     

test3.c:70:18:错误:使用未声明的标识符'输出'如果((i =   creat(输出,0644))< 0){

这是我的代码:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "signal.h"
#include "unistd.h"

void prompt(char*);
void execute( char* );
char** parse( char* );

int main( int ac, char* av[] )
{
  char input[255]; // buffer for supporting command

  signal( SIGINT, SIG_IGN ); // ignore ctrl-c

  while(1)
  {
    prompt(input);
    execute( input );
  }
};

void execute( char* str)
{
  int fork_result, status, i = 0,j=0,in=0,out=0;
  char input[100];output[100];
  char** args = parse( str ); // splits the user command into arguments

  fork_result = fork(); // attempt to fork
  if ( fork_result == -1 ) // failure
  {
    perror("Failed to fork\n");
    exit(1);
  }
  else if ( fork_result == 0 ) // I'm the child
  {
    for(i=0;args[i]!='\0';i++)
    {
      if(strcmp(args[i],"<")==0)
      {   
        args[i]=NULL;
        strcpy(input,args[i+1]);
        in=2;   
      }   
      if(strcmp(args[i],">")==0)
      {
        args[i]=NULL;
        strcpy(output,args[i+1]);
        out=2;
      }   
    }

    if (in) 
    {
      j = open(input, O_RDONLY, 0);
      if ((j = open(input, O_RDONLY, 0)) < 0) 
      {
        perror("Couldn't open input file");
        exit(0);
      }
      dup2(j, 0);
      close(j);
    }

    if (out) 
    {
      if ((i= creat(output , 0644)) < 0) 
      {
        perror("Couldn't open the output file");
        exit(0);
      }
      dup2(i, STDOUT_FILENO);
      close(i);
    }

    execvp( args[0], args );
    perror("failed to exec\n");
    exit(2);
  }
  else // I'm the parent
  {
    // wait here
    wait(&status); // wait for child to finish
    free( args ); // free dynamic memory
  }
}

char** parse( char* str )
{
  char** args = malloc( 256 );
  int i = 0;

  args[i] = strtok( str, " " );

  while( args[i] )
  {
    i++;
    args[i] = strtok( NULL, " " );
  }

  return args;
}

void prompt(char* input)
{
  printf("$ "); // print prompt
  fgets( input, 255, stdin );

  input[strlen(input)-1] = '\0'; // overwrite \n with \0

  if ( strcmp( input, "exit" ) == 0 ) // shell command
    exit(0);
}

2 个答案:

答案 0 :(得分:2)

  

char input[100];output[100];

你想:

char input[100], output[100];

同时添加:#include <fcntl.h>

一般情况下,man open(以及您使用的其他功能)是您的朋友 - 它会告诉您要添加的#include

您的代码中存在更多潜在错误和任意限制。一些例子:

void execute( char* str)
{
  char input[100], output[100];
  ...
  if(strcmp(args[i],"<")==0)
  {   
    args[i]=NULL;
    strcpy(input,args[i+1]);  // possible stack buffer overflow.

  if(strcmp(args[i],">")==0)
  {
    args[i]=NULL;
    strcpy(output,args[i+1]);  // possible stack buffer overflow


char** parse( char* str )
{
  char** args = malloc( 256 );  // limit of 256/sizeof(char*) parameters.
  // on a 64-bit system, if more than 32 parameters are supplied ...

         args[i] = strtok( NULL, " " );  // ... possible heap buffer overflow.


  fgets( input, 255, stdin );  // arbitrary limit of 254 characters on command line.

无法保证字符串以\n结尾:

  input[strlen(input)-1] = '\0'; // overwrite \n with \0

如果我评价这个&#34; shell&#34;,我会给它一个&#34; F&#34;。

答案 1 :(得分:0)

您的代码中存在多个错误。

1。在第27行,您需要使用逗号而不是分号char input[100], output[100];来分隔输入和输出的两个变量定义,或者指定类似{{1}的输出类型你已经在上面的行中完成了这个。

2。编译器抱怨缺少函数char input[100]; char output[100];和标识符open的定义。这可以通过在文件顶部的包含中添加O_RDONLY来解决。

在这些更改之后,代码编译对我来说很好(使用gcc 5.4.0):

#include "fcntl.h"