aboujouj83
IS-IT--Management
I am implementing a routine that takes a string as input and has to replace any variable in it by its value. A variable in unix starts by the dollar sign $. This routine is used in a parseLine routine for my unix shell program.
For the purpose of just testing the replacement routine, I am trying the code below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void replaceVariables(char*);
int main (void){
char line []="/dgddg/$dfdf";
replaceVariables(line);
printf("%s\n",line);
return 0;
}
void replaceVariables(char *line){
char *temptk[32];
char tempLine[32];
char newLine[32];
int i = 0;
int j;
/* backup the line */
strcpy (tempLine, line);
/* tokenize the line using the / delimiter */
temptk = strtok(line,"/"
while(temptk != NULL) {
/* check if it starts with a $ sign */
if (temptk[0] == '$')
/* check for possible variable susbstitution */
/*if (strcmp(findVar(env, temptk + 1), "notfound" != 0)*/
/* here the variable is found */
{
/* replace the variable by its value */
/* FOR THE PURPOSE OF TESTING, I ALWAYS REPLACE IT BY "changed"*/
strcpy (temptk, "changed"
}
i++;
temptk = strtok (NULL, "/"
}
/* creates the new line */
strcpy (newLine, temptk[0]);
for (j = 1; j < i; j ++){
strcat (newLine, "/"
printf("creating new Line/n"
strcat (newLine, temptk[j]);
}
/* deals with the case where the original line starts with a "/" */
if (tempLine [0] == '/'){
strcpy (line, "/"
strcat (line, newLine);
}
else
strcpy (line, newLine);
}
IT IS GIVING ME A SEGMENTATION FAULT. WHAT IS THE PROBLEM? AND AM I USING THE BEST WAY TO SOLVE SUCH PROBLEM? ANY HELP IS VERY APPRECIATED.
THANKS
For the purpose of just testing the replacement routine, I am trying the code below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void replaceVariables(char*);
int main (void){
char line []="/dgddg/$dfdf";
replaceVariables(line);
printf("%s\n",line);
return 0;
}
void replaceVariables(char *line){
char *temptk[32];
char tempLine[32];
char newLine[32];
int i = 0;
int j;
/* backup the line */
strcpy (tempLine, line);
/* tokenize the line using the / delimiter */
temptk = strtok(line,"/"
while(temptk != NULL) {
/* check if it starts with a $ sign */
if (temptk[0] == '$')
/* check for possible variable susbstitution */
/*if (strcmp(findVar(env, temptk + 1), "notfound" != 0)*/
/* here the variable is found */
{
/* replace the variable by its value */
/* FOR THE PURPOSE OF TESTING, I ALWAYS REPLACE IT BY "changed"*/
strcpy (temptk, "changed"
}
i++;
temptk = strtok (NULL, "/"
}
/* creates the new line */
strcpy (newLine, temptk[0]);
for (j = 1; j < i; j ++){
strcat (newLine, "/"
printf("creating new Line/n"
strcat (newLine, temptk[j]);
}
/* deals with the case where the original line starts with a "/" */
if (tempLine [0] == '/'){
strcpy (line, "/"
strcat (line, newLine);
}
else
strcpy (line, newLine);
}
IT IS GIVING ME A SEGMENTATION FAULT. WHAT IS THE PROBLEM? AND AM I USING THE BEST WAY TO SOLVE SUCH PROBLEM? ANY HELP IS VERY APPRECIATED.
THANKS