Hello!
I'm writing my own shell, but I'm getting a strange error. I read a number of paths from a file when I start the shell, but for some bizarr reason I realised that any threechar commands in /usr/bin are impossible to run. All other commands work fine.
So I tried some things and figured out there is a strange bug in my concat function which I can't figure out. I add two strings together, and it works fine, but whenever the total size of the string is 12 chars it becomes 15 chars. Below is the concat-function along with a main for testing it. The output when running this program is:
Size 1: 9
Size 2: 3
Sum: 12
The parts: /usr/bin/ - zip
The concat string: /usr/bin/zip|
Sum 15: /usr/bin/zip
The program (concat test program that is..):
Code:
char* concat(char* strang1, char* strang2)
{
int x=0;
int y=0;
int cx=0;
char* newStr;
int size1;
int size2;
int sum;
while(strang1[x]!=0)
x++;
size1=x;
x=0;
while(strang2[x]!=0)
x++;
size2=x;
x=0;
sum=size1+size2;
printf("Size 1: %d\nSize 2: %d\nSum: %d\n",size1,size2,sum);
// newStr=(char*)malloc(sum*sizeof(char));
printf("The parts: %s - %s\n",strang1,strang2);
newStr=(char*)calloc(sum,sizeof(char));
for (x=0;x<size1;x++)
newStr[x]=strang1[x];
for (cx=0;cx<size2;cx++)
newStr[x+cx]=strang2[cx];
printf("The concat string: %s|\n",newStr);
return(newStr);
}
int main()
{
char* part1="/usr/bin/";
char* part2="zip";
char* total=concat(part1,part2);
int x=0;
while(total[x]!=0)
x++;
printf("Sum %d: %s\n",x,total);
free(total);
exit(0);
}
Does anyone have any idea why this happens? I can't figure it out.