I've been looking every sscanf
post here and I can't find an exact solution suitable for my problem. I was implementing my own Shell and one of the characteristics is that if I find the dollar sign $
, I got to replace what is exactly behind with the environmental variable:
cd $HOME
should actually be replaced by cd /home/user
before I even execute the cd.
My question is what is the code to use sscanf
to take out the dollar sign and simply get HOME on the same string? I've been struggling with some null pointers trying this:
char * change;
if (strcmp(argv[1][0],'$')==0){
change = malloc(strlen(argv[y]));
sscanf(argv2[y]+1,"%[_a-zA-Z0-9]",change);
argv2[y]=getenv(change);
}
But this seems to be failing, I'm having a segmentation fault core. (If needed i put more code, my question is specially focused on the sscanf).
Quick explanation argv
is an array of pointers to the lines entered and parsed, so actually the content of argv[0] = "cd"
and argv[1]="$HOME"
. I also know that the variable I'm going to receive after the $
has the format %[_a-zA-Z0-9]
.
Please ignore the non failure treatment.