Capturing a variable length string from the command-line in C

后端 未结 4 1597
耶瑟儿~
耶瑟儿~ 2021-01-16 06:03

I\'ve looked everywhere for an answer to my question, but I have yet to find a solid answer to my problem.

I\'m currently in the process of writing a program in C, s

相关标签:
4条回答
  • 2021-01-16 06:19

    If you are using a gnu system, use the gnu getline extension to the c library, it does all the dynamic sizing for you.

    e.g. (though I haven't tested)

    void get_command()
    {
        /*
         * Reads in a command from the user, outputting the correct response
         */
    
        size_t buffer_size = 0;   
        char *command = NULL;
    
        ssize_t len = getline(&command, &buffer_size, stdin);
    
        if(len < 0)
        {
            perror("Error reading input");
        }
        else if (command[len - 1] == '\n')
        {
            puts("It's inside the buffer.");
        }
        else
        {
            puts("It's not inside the buffer.");
        }
    
        free(command);
    }
    
    0 讨论(0)
  • 2021-01-16 06:20

    I think the answer about assuming a maximum command length is the correct one: typically you'll want to keep commands within a reasonable length.

    But if you really can't make assumptions about the maximum length of a command, then you'll need to buffer.

    Keep:

    • a fixed buffer that you always fgets the same number of characters into, and
    • a command that you append to, and realloc when necessary.

    The following code probably lacks some error handling:

    #define BUFFER_SIZE 20
    #define COMMAND_BLOCK_SIZE 50
    
    void get_command()
    {
        char *buffer = malloc(sizeof(char) * (BUFFER_SIZE + 1));
    
        char *command = malloc(sizeof(char) * (COMMAND_BLOCK_SIZE + 1));
        int commandSize = 50;
    
        // tmp pointer for realloc:
        char *tmp = NULL;
        char *retval = NULL;
    
        if ((buffer == NULL) || (command == NULL))
            return_error("Error allocating memory");
    
        retval = fgets(buffer, BUFFER_SIZE, stdin);
    
        while (retval != NULL)
        {
            if (strlen(buffer) + strlen(command) > commandSize)
            {
                tmp = realloc(command, commandSize + (COMMAND_BLOCK_SIZE + 1));
                if (tmp == NULL)
                    return_error("Error allocating memory");
                else
                {
                     command = tmp;
                     commandSize += COMMAND_BLOCK_SIZE;
                }
            }
    
            // not using strncat because the check above should guarantee that
            //    we always have more than BUFFER_SIZE more bytes in command 
            strcat(command, buffer);
    
            if (buffer[strlen(buffer) - 1] == '\n')
                break;
    
            retval = fgets(buffer, BUFFER_SIZE, stdin);
        }
    
        printf("COMMAND: %s\n", command);
        free(buffer);
    }
    

    Also note that:

    • we don't do anything useful with command there, you'll probably want to pass in a char ** so that you can get command out of this function, and free it in the calling code, e.g. in your main loop.
    • that the '\n' is retained in command: you may want to discard that.
    0 讨论(0)
  • 2021-01-16 06:29

    You dont need do any realloc, just add 1 byte more than the maximum command length for \0 and forget \n because you wont get a \n always the user type enter. If the user input exceed the length then your string will be truncated without the \n. So your condition after fgets is not correct and based in a wrong assumption.

    something like:

       int buffer_size = MAX_COMMAND_LENGTH + 1;
    

    About memory allocation: you should use stack instead heap avoiding malloc/free in this case. So your code will be simpler and less error prone:

        char command[buffer_size];
        ...
        // free(command) <-- you dont need this anymore
    

    Note that your command will be freed after function return. So if you will process it into get_command its ok but if you want return it to caller you shall receive a buffer from caller.

    0 讨论(0)
  • 2021-01-16 06:31

    Use getline(3) if you really need. It's POSIX.1-2008. And be aware that unlimited length lines are an easy attack vector for DOS-attacks (OOM). So consider making up a reasonable line length limit, and using fgets(3).

    0 讨论(0)
提交回复
热议问题