首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >编写我自己的shell,未声明的标识符"output“

编写我自己的shell,未声明的标识符"output“
EN

Stack Overflow用户
提问于 2018-06-03 08:40:17
回答 2查看 565关注 0票数 0

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

输出错误:使用未声明的标识符‘

’char input100;output100;

test3.c:53:15:错误:使用未声明的标识符'output‘strcpy(output,argsi+1);^

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

C99 :60:8:警告:输入中函数'open‘的隐式声明无效。隐式函数声明j= open(input,O_RDONLY,0);^

test3.c:60:20:错误:使用未声明的标识符'O_RDONLY‘j= open(input,O_RDONLY,0);^

test3.c:61:29:错误:如果((j =O_RDONLY(input,O_RDONLY,0)) < 0),则使用未声明的标识符'O_RDONLY‘。{

C99 :70:12:警告:如果((i= creat(output,0644)) < 0) {

test3.c:70:18:错误:如果((i= creat(output,0644)) < 0) {

下面是我的代码:

代码语言:javascript
复制
#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);
}
EN

回答 2

Stack Overflow用户

发布于 2018-06-03 08:44:22

char input[100];output[100];

您需要:

代码语言:javascript
复制
char input[100], output[100];

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

通常,man open (和您使用的其他函数)是您的朋友--它告诉您要添加什么#include

在你的代码中有很多潜在的bug和任意的限制。下面是一些例子:

代码语言:javascript
复制
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结尾

代码语言:javascript
复制
  input[strlen(input)-1] = '\0'; // overwrite \n with \0

如果我给这个“壳”打分,我会给它一个"F“。

票数 1
EN

Stack Overflow用户

发布于 2018-06-03 09:04:50

您的代码中有多个错误。

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

2.编译器抱怨缺少函数open和标识符O_RDONLY的定义。通过将#include "fcntl.h"添加到文件顶部的includes中,可以修复此问题。

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

代码语言:javascript
复制
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "signal.h"
#include "unistd.h"
#include "fcntl.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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50662355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档