Hello.
I'm writing a program in C that should ask for a line of input, and extract the process name and parameters. This doesn't have to be totally bulletproof at this point. An example input could be:
"Hello these are parameters". On this example "Hello" would be the string i want as process name, and the other words are parameters. I'm using getline (http://linuxmanpages.com/man3/getline.3.php) to get input from stdin.
I don't allocate more memory for the individual words but instead store pointers of the main string, and replace spaces with null characters. The program worked before, but after I made some changes I get this segmentation error which I can not get rid of. (Neither can i find what changes were made since the working version) I also tried allocating memory for the "buffer" variable in the acquireInput function myself instead of letting getline do it, but the result was the same.
The part of the program where my code crashes looks as follows:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
struct Command
{
char* process; //First word contained in buffer.
char** parameters; //Array of memory addresses contained in buffer that are the beginning of parameters. (Dynamically allocated)
unsigned int parameterAmount; //Variable that holds that amount of parameters.
};
char* acquireInput(struct Command *input, unsigned int *parameterAmount);
int isWhitespace(char character);
void extractWords(char *process, char *parameters[], unsigned int *parameterAmount, char *buffer, unsigned int bufferSize);
int main(int argc, char *argv[])
{
char *buffer = NULL; //Buffer that will contain the entire inputstring. (Dynamically allocated)
struct Command input = { NULL, NULL, 0 };
do
{
//Pointers will get overwritten each iteration.
//Free the allocated memory.
free(buffer);
free(input.parameters);
buffer = acquireInput(&input, &input.parameterAmount);
}
while(1); //temporary
printf("\n");
return 0;
}
char* acquireInput(struct Command *input, unsigned int *parameterAmount)
{
char *buffer = NULL;
size_t bufferSize = 0;
unsigned int charactersRead = 0;
printf("INPUT: ");
charactersRead = getline(&buffer, &bufferSize, stdin); //crash happens at this point.
input->parameters = (char**) malloc(((charactersRead / 2) + 1) * sizeof(char*));
extractWords(input->process, input->parameters, parameterAmount, buffer, charactersRead + 1);
return buffer;
}
void extractWords(char *process, char *parameters[], unsigned int *parameterAmount, char *buffer, unsigned int bufferSize)
//TODO: Words between quotes are 1 word, even if there's a whitespace between them.
{
int i = 0;
*parameterAmount = 0;
*process = 0;
//As long as the string-terminating NULL character hasn't been reached.
while(i < bufferSize - 1)
{
//move to the next word.
while (isWhitespace(buffer[i])) i++;
//Add the word to the words array if it's not the final NULL character..
if (buffer[i] != '\0')
{
if (*parameterAmount == 0)
process = buffer + i;
else
parameters[(*parameterAmount)++] = buffer + i;
}
//Move to the end of the word and mark it with a null character.
while (!isWhitespace(buffer[i]) && buffer[i] != '\0') i++;
//if the end of this word isn't the NULL character, place one move 1 character further.
if (buffer[i] != '\0')
buffer[i++] = '\0';
}
}
int isWhitespace(char character)
{
return (character == '\n' || character == '\t' || character == ' ');
}
I'm running Fedora 12 (Constantine) as a Virtual Machine in VMWare, using Code::Blocks as IDE and GNU GCC as compiler with the -Wall option enabled.
I hope anyone could give me some feedback on what could cause this problem.
Thanks in advance,
Gonbe.
-edit-
I forgot to add the output of the program (though it's not exactly informative):
INPUT:
Segmentation fault (core dumped)
This is when I hit enter during the input-moment. (The result is the same when entering text)