【问题标题】:Parsing command line for execve()解析 execve() 的命令行
【发布时间】:2013-05-11 12:01:14
【问题描述】:

我正在编写一个程序,它需要一个命令行然后解析它,以便在输入中打印每个 argv 的字符串数组。

代码给我一个分段错误(核心转储)!

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

char  **parse(int position ,char *argv[]  ) ;
int main(int argc ,char *argv[])
 {

    int i=1;
    int f=argc;
    argc--;
    while( i<f) 
   {
     char commands[10];
     char **argument=parse(argc,argv);
     //parse(i ,argv ,commands ,argument) ;
     printf("the argument[ %i ] is :%s \n",i,argument[i]);

     argc-- ;
     i++;
   }
  }

char **parse(int position ,char *argv[])
 {  
   // char *commands;
    char** arguments;
    char *result ;
    char buffer [30] ;
    int count =0;

    arguments = calloc(1, sizeof (char *));

    strcpy(buffer,argv[position-1]); //copy the current argv to the buffer

    result =strtok(buffer," ");
   // strcpy(commands,result); 
    //result =strtok(buffer," ");
    while(result !=NULL )
      {

        arguments[count] =result ;
        ++count;
        arguments = realloc(arguments, sizeof (char *) * (count + 1));            
        result=strtok(NULL," ");
      }
   arguments[count] = NULL; //in order to call the execvp 



   return arguments;

  } 

感谢您的帮助。

【问题讨论】:

    标签: c strtok


    【解决方案1】:

    您可以使用 argv[][] array 访问每个参数。 argc 为您提供了许多参数。这包括程序名称本身。 例如:

    c:\>test.exe arg1 arg2
    

    这里argc 将是 3 和

    argv[0]="test.exe";
    argv[1]="arg1";
    argv[2]="arg2";
    

    或者如果你想要更多交互的命令行解析检查这个tclap

    【讨论】:

    • 访问 argv[3] 会导致分段错误。
    • 参见 C 规范(例如 open-std.org/jtc1/sc22/wg14/www/docs/n1539.pdf 的草稿 - 你必须为最终版本付费)部分“5.1.2.2.1 程序启动”说“argv[argc] 应为空指针”。所以argv[3]=NULL是有标准保证的。
    【解决方案2】:

    我不知道你想达到什么目标,但是:

    int main(int argc ,char **argv)
    {
       int i;
    
       for( i=1; i<argc; ++i )
       {
           printf("the argument[ %i ] is :%s \n",i,argv[i]);
       }
       return 0;
    }
    

    【讨论】:

    • 你永远不能voidmain()。
    【解决方案3】:

    可能是这样的

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    char  **parse(int position , char *argv[], char *outputbuff);
    int main(int argc ,char *argv[]){
        int i;
        for(i=1;i<argc;++i) {
            char commands[30];
            char **argument;
            argument=parse(i ,argv ,commands);
            {
                int j;
                for(j=0;argument[j]!=NULL;++j)
                    printf("the argument[ %i ] is :%s \n", j, argument[j]);
            }
            free(argument);
        }
        return 0;
    }
    
    char **parse(int position ,char *argv[], char *commands){  
        char **arguments;
        char *result ;
        int count =0;
    
        arguments = calloc(1, sizeof (char *));
        strcpy(commands, argv[position]);
        result =strtok(commands," ");
        while(result !=NULL ){
            arguments[count] =result ;
            ++count;
            arguments = realloc(arguments, sizeof(char*)*(count + 1));            
            result=strtok(NULL," ");
        }
        arguments[count] = NULL;
    
        return arguments;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 2010-11-27
      • 1970-01-01
      • 2013-11-18
      • 2012-05-09
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多