Reputation: 21
I'm trying to parse a given string into an array of NULL terminated commands, since I'm designing a C shell. So my desired command structure is :
// Null terminated commands
char** command1 = {"ls", "-l", NULL};
char** command2 = {"wc", NULL};
// Final NULL terminated array of commands
char*** cmd = {command1, command2, NULL};
And my code is :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int lastPipe = 0; // To track the last position of the "|" symbol
int pipeCount = 0; // Count of the pipes
char*** commands = (char***) calloc (10, sizeof(char**));
for (int i=0; i<10; i++)
{
commands[i] = (char**) calloc (10, sizeof(char*));
for (int j=0; j<10; j++)
{
commands[i][j] = (char*) calloc (10, sizeof(char));
}
}
int a = 0;
char* argVector[] = {"ls", "|", "wc", NULL};
// argVector is the parsed version of the input string
// For instance, argVector = {"ls", "|", "wc", NULL};
for (int i=0; argVector[i] != NULL; i++)
{
if (strcmp(argVector[i], "|") == 0)
{
if (lastPipe == 0)
{
for (a=0; a<i; a++)
strcpy(commands[pipeCount][a], argVector[a]);
// Make NULL terminated command
commands[pipeCount][a] = NULL;
// Update Pipe location
lastPipe = i;
pipeCount++;
}
else
{
for (a = lastPipe+1; a<i; a++)
{
strcpy(commands[pipeCount][a-lastPipe-1], argVector[a]);
}
// Make NULL terminated command
commands[pipeCount][a-lastPipe-1] = NULL;
// Update Pipe location
lastPipe = i;
pipeCount++;
}
}
if (pipeCount > 0)
{
for(a=lastPipe + 1; a<=i; a++)
{
if (strcmp(argVector[a], "|") != 0)
// This line gives the Segmentation Fault
strcpy(commands[pipeCount][a-lastPipe-1], argVector[a]);
}
commands[pipeCount][a-lastPipe-1] = NULL;
}
}
// Now, I must have a NULL terminated array of Commands
commands[pipeCount][a] = NULL;
commands[pipeCount] = NULL;
// Print the commands
for (int i=0; commands[i]!=NULL; i++)
{
for(int j=0; commands[i][j]!=NULL; j++)
{
printf("Commands[%d][%d] = %s\n", i, j, commands[i][j]);
}
}
}
When I run this, I get a segmentation fault, and when I looked at valgrind, it showed me :
==12458== Invalid write of size 1
==12458== at 0x483BDC8: strcpy (vg_replace_strmem.c:512)
==12458== by 0x109E4B: main (pipe.c:251)
==12458== Address 0x0 is not stack'd, malloc'd or (recently) free'd
Apparently, the last strcpy has a problem, but I'm at my wits end. How can I solve this? Not sure how I'm getting these errors
EDIT : Posted full code so that you can reproduce the errors
Upvotes: 1
Views: 133
Reputation: 21
Like @DavidRanieri said, the problem stems with me overwriting the elements with NULL
, due to my incorrect indexing. So I simply changed those two lines with :
commands[pipeCount+1][a] = NULL;
commands[pipeCount+1] = NULL;
And now, everything works as expected.
Upvotes: 1