为什么会出现段故障?

时间:2017-02-14 21:12:19

标签: c linux

此代码的目标是将输入拆分为“ls | wc”,并将其显示为argv [0] = ls,argv 1 = wc 它适用于我的MacBook,但在Linux机器上失败。它显示了段故障。 我仔细检查了一个早晨,但仍然不知道为什么。 有人能帮助我吗?

Linux机器上的结果是: enter image description here

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SUB_COMMANDS 5
#define MAX_ARGS 10
struct SubCommand{
    char *line;
    char *argv[MAX_ARGS];
};
struct Command{
    struct SubCommand sub_commands[MAX_SUB_COMMANDS];
    int num_sub_commands;
};
void PrintArgs(char** argv){
    int i = 0;
    while (argv[i] != NULL){
        printf ("argv[%d] = '%s' \n", i , argv[i]);
        i++;
    }
    return;
}

void ReadArgs(char *in, char **argv, int size){
    if (size <= 0){ // in case of size is negative or 0//
        fprintf(stderr,"Please enter a positive size.\n");
        return;
    }
    if (size == 1){ // if size is 1, directly return//
        *argv = NULL;
        return;
    }
    char *p;
    int length = strlen(in);
    //in[length - 1] = '\0';//
    int count = 1; //count the number of element, it is 1 since NULL must be included//
    p = strtok(in, " ");
    char *buff = strdup(p);
    count++;
    *argv = p;
    argv++;
    free(buff);
    if (p == NULL){
        *argv = NULL;
        return; // we need just output one element//
    }
    while ((p=strtok(NULL, " "))!= NULL && (count <= size - 1)) {
            buff = strdup(p);
            *argv = p;
            argv++;
            count++;
            free(buff);
    }
    argv++;
    *argv = NULL;
    return;
}

int get_args(char *in, char **argv, int max_args){
    char *p;
    int length = strlen(in);
    in[length - 1] = '\0';
    p = strtok(in, "|");
    char *buff = strdup(p);
    *argv = p;
    argv++;
    free(buff);
    if (p == NULL){
        return 1; // we need just output one array //
    }
    int count = 1; // if p is not null, it means at lease we have one array//
    while ((p=strtok(NULL, "|"))!= NULL && (count <= max_args - 1)) {
        buff = strdup(p);
        *argv = p;
        argv++;
        count++;
        free(buff);
    }
    return count;
}
void ReadCommand(char *line, struct Command *command){
    /*Split the line by "|", and store them in argv*/
    int i;
    char *argv[MAX_SUB_COMMANDS];
    int number = get_args(line, argv, MAX_SUB_COMMANDS);
    /*End of Split procedure*/
    /*Stored into sub-command's line*/
    if (number > MAX_SUB_COMMANDS){
        number = MAX_SUB_COMMANDS;
    }
    for (i = 0; i < number; i++){
        command->sub_commands[i].line = argv[i];
        command->num_sub_commands = i;
        ReadArgs(command->sub_commands[i].line, command->sub_commands[i].argv, MAX_ARGS); //populate all argv in SubCommand//
    }
    return;
}
void PrintCommand(struct Command *command){
    int i;
    for (i = 0; i < MAX_SUB_COMMANDS; i++){
        PrintArgs(command->sub_commands[i].argv);
    }
}

int main(){
    char s[200];
    char *argv[10];
    int argc;
    printf("Enter a string: ");
    fgets(s, sizeof s, stdin);
    struct Command a;
    struct Command *command;
    command = &a;
    ReadCommand(s, command);
    PrintCommand(command);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

为什么不尝试使用argvargc?它太干净了

#include<stdio.h>

int main(int argc, char *argv[]){
    int i;
    for(i=0;i<argc;i++){
        printf("Argv[%d] = %s\n", i, argv[i]);
    }
    return 0;
}

答案 1 :(得分:0)

<property>
     <name>hadoop.proxyuser.server.hosts</name> 
     <value>*</value> 
</property> 
<property>
     <name>hadoop.proxyuser.server.groups</name>
     <value>*</value>
</property>

您真的需要查找strtok的文档。上面的行将导致未定义的行为,因为您以前没有使用指向可写内存的有效指针调用strtok。