Char **功能在功能之外不能正常工作

时间:2013-02-22 03:01:17

标签: arrays string char strtok

所以我创建了一个名为myargv的char **变量,它应该像一个在每个索引处存储字符串的数组。我有一个函数,它返回一个char **到myargv,当我从内部打印所有内容时,函数似乎工作正常,但是当我尝试从main中打印myargv中的东西时,它不再起作用了.. ..任何人都可以帮助我吗?

char **findArgs(char *line)
{
int i = 0;
char **temp, *tokTemp;
char **myargv;

tokTemp = strtok(line, " ");

myargv = malloc(sizeof(*myargv));

while (tokTemp != NULL)
{
if (strcmp(tokTemp, ">") == 0 || strcmp(tokTemp, "<") == 0 || strcmp(tokTemp, ">>") == 0)
{
    break;
}
else
{
    myargv[i] = malloc(strlen(tokTemp) + 1);
    //myargv[i] = malloc(sizeof(char)*strlen(tokTemp));
    strcpy(myargv[i], tokTemp);
    //printf("myargv[%d] = %s \n", i, myargv[i]);

    temp = realloc(myargv, (i+2)*sizeof(*myargv));    
    if (temp != NULL)
    {
        myargv = temp;
    }
    tokTemp = strtok(NULL, " ");
    i++;
}
}
myargv[i] = NULL;
//printf("myargv[0] = %s\n", myargv[0]);
return myargv;
}


int main(int argc, char *argv[], char *env[])
{
int cmdInt, pid, status, i = 0, ioNumber = 0;
char input[64], lineBUFFER[64], lineBUFFER2[64], lineBUFFER3[64], lineBUFFER4[64], homePath[64], fileName[64], cmdPathFINAL[64];
char *cmd;
char **myargv, **cmdPath; 
int myFile;

while(1)
{

 printf("command: ");
 gets(input);

strcpy(lineBUFFER, input);
strcpy(lineBUFFER2, input);
strcpy(lineBUFFER3, input);  
strcpy(lineBUFFER4, input);  

cmd = strtok(lineBUFFER3, " ");
cmdInt = findCommand(lineBUFFER2);

ioNumber = ioCheck(lineBUFFER4, fileName);
//printf("ioCheck = %d \n", ioNumber);
//printf("Filename: %s \n", fileName);

myargv = findArgs(lineBUFFER);
//printf("myargv[0] = %s\n", myargv[0]);

findHome(env, homePath);
//printf("Home path = %s\n", homePath);

switch(cmdInt)
      { 
          case 0 : 

            if (myargv[1] == NULL)
    {
        chdir(homePath);
    }
    else
    {
        //printf("1st argument: %s, 2nd argument: %s, 3rd argument: %s \n", myargv[1], myargv[2], myargv[3]);
        chdir(myargv[1]);
    }

              break;
          case 1 :

              exit(1);   

              break;
          default :

            pid = fork();
    if (pid == 0)
    {
    //printf("Parent %d waits for child %d to die. \n", getpid(), pid);
    pid = wait(&status);
    printf("dead child = %d, how = %04x \n", pid, status);
    exit(100);
    }
    else
    {
        ioNumber = ioCheck(lineBUFFER4, fileName);
            //printf("ioCheck = %d \n", ioNumber);
            //printf("Filename: %s \n", fileName);
        cmdPath = getPath2(env);
            //printf("cmdPath[0] = %s\n", cmdPath[0]);

            findPath(cmd, cmdPath, cmdPathFINAL);
            printf("Command Path = %s\n", cmdPathFINAL);

        if (ioNumber == 1)
        {
            close(0);
            myFile = open(fileName, O_RDONLY);
        }   
        else if (ioNumber == 2)
        {
            close(1);
            myFile = open(fileName, O_WRONLY|O_CREAT, 0644); 
        }
        else if (ioNumber == 3)
        {
            close(1);
            myFile = open(fileName, O_WRONLY|O_APPEND);
        }

        execve(cmdPathFINAL, myargv, env);
        //printf("child %d dies by exit () \n", getpid());
        exit(100);
    }

              break;
      }
}

if (ioNumber == 1 || ioNumber == 2 || ioNumber == 3)
{
close(myFile);
}   

return 0;
}

1 个答案:

答案 0 :(得分:0)

以下适用于Window和SuSE linux 10.4 x86_64内核2.6.16。解析器有两个版本:getArgs和getArgs2。没有给代码一个彻底的审查,但希望它会帮助你。

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

int getArgs(const char *line, char *args[], int argc)
{
    int len, i = 0;
    const char* delims = " ";
    char *ltok = strdup(line);
    char *tok = strtok(ltok, delims);

    for (; tok != NULL; i++) {

        if (strchr("><|", *tok) != NULL) /* not sure why this test is needed for CLI */
            break;

        if (args != NULL && i < argc) {
            args[i] = strdup(tok);
        }

        tok = strtok(NULL, delims);
    }

    free(ltok);

    return i;
}

int getArgs2(const char *line, char **args, int argc)
{
    int len, i = 0;
    const char *p, *q;
    const char *delims = " ";

    p = line;
    while (strchr(delims, *p) != NULL && *p != '\0')
        p++;

    q = p;
    while (strchr(delims, *q) == NULL && *q != '\0')
        q++;

     for (; q > p; i++) {

        if (strchr("><|", *p) != NULL) /* not sure why this test is needed for CLI */
            break;

        len = q-p;

        if (args != NULL && i < argc) {
            args[i] = (char *)malloc(len + 1);
            strncpy(args[i], p, len)[len] = '\0';
        }

        p = q;
        while (strchr(delims, *p) != NULL && *p != '\0')
            p++;

        q = p;
        while (strchr(delims, *q) == NULL && *q != '\0')
            q++;

    }

    return i;
}


int main(int argc, char *argv[])
{
    char line[256], *q;
    char **args;
    int count, n;

    for(;;) {

        printf("command: ");
        if (fgets(line, sizeof(line)/sizeof(line[0]), stdin) == NULL)
            break;

        q = line;
        while (isspace(*q))
            q++;

        if (*q == '\0')
            break;

        while (*q != '\0' && *q != '\n' && *q != '\r')
            q++;

        *q = '\0';

        for(count = 0, q = line; *q != '\0'; q++)
            if (*q == ' ')
                count++;

        printf("Input command: %d character(s), %d space(s) for \"%s\"\n", strlen(line), count, line);

        count = getArgs(line, NULL, 0);

        if (count > 0) {
            args = (char **)malloc(sizeof(char *)*count);
            count = getArgs(line, args, count);
        } else {
            args = NULL;
        }

        printf("Parsed %d arg(s) from line\n", count);
        printf("--------------------------------------\n");

        for (n = 0; n < count; n++) {
            printf("%d: %s\n", n+1, args[n]);
            free(args[n]);
        }

        free(args);

    }

    return 0;

}