0

我有一个函数,它从标准输入中获取一行并创建一个锯齿状数组,我将使用它来构建我正在构建的 shell。这个想法是我提示输入(如 shell)并读取输入的行(以及参数),然后我将使用 fork 调用 execvp(,) 来执行从标准输入输入的命令。我已经编写了一个函数来解析输入的字符串并将其分解为一个锯齿状数组,我正在寻找一个类似的函数来进行比较,或者寻求关于如何使函数更高效的建议。这是代码:

int makearg( char *s, char ***args )
{
  int numArgs = 0, i=0, j=0, rows=0, cols=0;

  numArgs = getNumArgs( s );

  // Allocate memory according to number of arguments +1                                                                                                               
  (*args) = (char**)calloc( (numArgs+1), sizeof(char*) );

  while( *(s+i) != '\0' ) {
    // if a space is found, the end of a word is found: copy to ragged array                                                                                           
    if( *(s+i) == ' ' || *(s+i) == '\t' ) {
      // Allocate memory for word                                                                                                                                      
      (*args)[rows] = (char*)malloc( (j+1)*sizeof( char ) );
      // set j-index to the beginning of the word                                                                                                                      
      j = i-j;
      // continue until the end of the word ( i ) is reached                                                                                                           
      while( j < i ) {// copy characters one at a time                                                                                                                 
        (*args)[rows][cols] = *(s+j);
        j++;
        cols++;
      }
      // add a \0 terminator to the word                                                                                                                               
      (*args)[rows][cols] = '\0';
      // reset j - tracks word length                                                                                                                                  
      j = 0;
      // move to next row                                                                                                                                              
      rows++;
      // move to beginning column of that row                                                                                                                          
      cols = 0;
      while( *(s+i+1) == ' ' )
        i++;
    }
    else {
      // if a word end was reached we dont want j incremented                                                                                                          
      j++;
    }
    // continue moving throught the string                                                                                                                             
    i++;
  }
  // When the while loop above finds the \0 at the end of the                                                                                                          
  // string it exits the loop, with one more word to be added.                                                                                                         
  // Add the final word to the ragged array here - same logic.                                                                                                         
  (*args)[rows] = (char*)calloc( (j+1), sizeof( char ) );
  j = i-j;
  while( j < i ) {
    (*args)[rows][cols] = s[j];
    j++;
    cols++;
  }
  (*args)[rows][cols] = '\0';
  rows++;
  cols = 0;
  // here, we add the single element to the final row of                                                                                                               
  // the ragged array. It contains only a \0.                                                                                                                          
  (*args)[rows] = (char*)calloc( 1, sizeof( char ) );
  (*args)[rows][0] = '\0';

  // return the number of arguments.                                                                                                                                   
  return numArgs;
}
4

1 回答 1

2

如果你利用strtok(),它更容易理解并且你也消除getNumArgs了。

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

int makearg( char *s, char ***args ) // modifies s in place
{
        int n = 0;
        char *copy = strdup(s);
        for(char *p = strtok(copy, " \t"); p; p = strtok(0, " \t")) {
                n++;
        }
        free(copy);

        char **ap = calloc(n+1, sizeof(char *));

        if(n) {
            ap[0] = strtok(s, " \t");
            for(int i = 1; i < n; i++) {
                ap[i] = strtok(0, " \t");
            }
        }
        *args = ap;
        return n;
}
于 2013-10-01T23:55:31.450 回答